summaryrefslogtreecommitdiff
path: root/res
diff options
context:
space:
mode:
authorKinsey Moore <kmoore@digium.com>2013-11-22 20:10:46 +0000
committerKinsey Moore <kmoore@digium.com>2013-11-22 20:10:46 +0000
commitd9015a5356dfff70ce15ed2ea5726325de71d9e3 (patch)
treeb4b253dcc96a4cbc27f54ee294dae45264e12dbb /res
parent1c45a32ee861fa427e0243abe03c729966fa4436 (diff)
ARI: Don't leak implementation details
This change prevents channels used as implementation details from leaking out to ARI. It does this by preventing creation of JSON blobs of channel snapshots created from those channels and sanitizing JSON blobs of bridge snapshots as they are created. This introduces a framework for excluding information from output targeted at Stasis applications on a consumer-by-consumer basis using channel sanitization callbacks which could be extended to bridges or endpoints if necessary. This prevents unhelpful error messages from being generated by ast_json_pack. This also corrects a bug where BridgeCreated events would not be created. (closes issue ASTERISK-22744) Review: https://reviewboard.asterisk.org/r/2987/ Reported by: David M. Lee ........ Merged revisions 403069 from http://svn.asterisk.org/svn/asterisk/branches/12 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@403070 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res')
-rw-r--r--res/ari/resource_bridges.c8
-rw-r--r--res/ari/resource_channels.c22
-rw-r--r--res/ari/resource_endpoints.c22
-rw-r--r--res/res_stasis.c44
-rw-r--r--res/stasis/app.c51
5 files changed, 124 insertions, 23 deletions
diff --git a/res/ari/resource_bridges.c b/res/ari/resource_bridges.c
index 7c4fed29b..e09bea6b5 100644
--- a/res/ari/resource_bridges.c
+++ b/res/ari/resource_bridges.c
@@ -605,7 +605,7 @@ void ast_ari_bridges_get(struct ast_variable *headers,
}
ast_ari_response_ok(response,
- ast_bridge_snapshot_to_json(snapshot));
+ ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
}
void ast_ari_bridges_destroy(struct ast_variable *headers,
@@ -656,7 +656,9 @@ void ast_ari_bridges_list(struct ast_variable *headers,
while ((obj = ao2_iterator_next(&i))) {
RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
struct ast_bridge_snapshot *snapshot = stasis_message_data(msg);
- if (ast_json_array_append(json, ast_bridge_snapshot_to_json(snapshot))) {
+ struct ast_json *json_bridge = ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+
+ if (!json_bridge || ast_json_array_append(json, json_bridge)) {
ast_ari_response_alloc_failed(response);
return;
}
@@ -689,5 +691,5 @@ void ast_ari_bridges_create(struct ast_variable *headers,
}
ast_ari_response_ok(response,
- ast_bridge_snapshot_to_json(snapshot));
+ ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
}
diff --git a/res/ari/resource_channels.c b/res/ari/resource_channels.c
index dc0058fa5..75d56d924 100644
--- a/res/ari/resource_channels.c
+++ b/res/ari/resource_channels.c
@@ -593,7 +593,7 @@ void ast_ari_channels_get(struct ast_variable *headers,
ast_assert(snapshot != NULL);
ast_ari_response_ok(response,
- ast_channel_snapshot_to_json(snapshot));
+ ast_channel_snapshot_to_json(snapshot, NULL));
}
void ast_ari_channels_hangup(struct ast_variable *headers,
@@ -639,6 +639,7 @@ void ast_ari_channels_list(struct ast_variable *headers,
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
struct ao2_iterator i;
void *obj;
+ struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
cache = ast_channel_cache();
if (!cache) {
@@ -661,14 +662,23 @@ void ast_ari_channels_list(struct ast_variable *headers,
return;
}
- i = ao2_iterator_init(snapshots, 0);
- while ((obj = ao2_iterator_next(&i))) {
+ for (i = ao2_iterator_init(snapshots, 0);
+ (obj = ao2_iterator_next(&i)); ao2_cleanup(obj)) {
RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
- int r = ast_json_array_append(
- json, ast_channel_snapshot_to_json(snapshot));
+ int r;
+
+ if (sanitize && sanitize->channel_snapshot
+ && sanitize->channel_snapshot(snapshot)) {
+ continue;
+ }
+
+ r = ast_json_array_append(
+ json, ast_channel_snapshot_to_json(snapshot, NULL));
if (r != 0) {
ast_ari_response_alloc_failed(response);
+ ao2_cleanup(obj);
+ ao2_iterator_destroy(&i);
return;
}
}
@@ -769,7 +779,7 @@ void ast_ari_channels_originate(struct ast_variable *headers,
stasis_app_subscribe(args->app, uris, 1, NULL);
}
- ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot));
+ ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
ast_channel_unref(chan);
}
diff --git a/res/ari/resource_endpoints.c b/res/ari/resource_endpoints.c
index 14f9e0576..c37f4968e 100644
--- a/res/ari/resource_endpoints.c
+++ b/res/ari/resource_endpoints.c
@@ -31,6 +31,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/astobj2.h"
#include "asterisk/stasis.h"
+#include "asterisk/stasis_app.h"
#include "asterisk/stasis_endpoints.h"
#include "asterisk/channel.h"
@@ -69,8 +70,15 @@ void ast_ari_endpoints_list(struct ast_variable *headers,
while ((obj = ao2_iterator_next(&i))) {
RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
- int r = ast_json_array_append(
- json, ast_endpoint_snapshot_to_json(snapshot));
+ struct ast_json *json_endpoint = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+ int r;
+
+ if (!json_endpoint) {
+ return;
+ }
+
+ r = ast_json_array_append(
+ json, json_endpoint);
if (r != 0) {
ast_ari_response_alloc_failed(response);
return;
@@ -121,14 +129,20 @@ void ast_ari_endpoints_list_by_tech(struct ast_variable *headers,
while ((obj = ao2_iterator_next(&i))) {
RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
+ struct ast_json *json_endpoint;
int r;
if (strcasecmp(args->tech, snapshot->tech) != 0) {
continue;
}
+ json_endpoint = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+ if (!json_endpoint) {
+ continue;
+ }
+
r = ast_json_array_append(
- json, ast_endpoint_snapshot_to_json(snapshot));
+ json, json_endpoint);
if (r != 0) {
ast_ari_response_alloc_failed(response);
return;
@@ -151,7 +165,7 @@ void ast_ari_endpoints_get(struct ast_variable *headers,
return;
}
- json = ast_endpoint_snapshot_to_json(snapshot);
+ json = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
if (!json) {
ast_ari_response_alloc_failed(response);
return;
diff --git a/res/res_stasis.c b/res/res_stasis.c
index e5fe1f691..e21941210 100644
--- a/res/res_stasis.c
+++ b/res/res_stasis.c
@@ -627,6 +627,7 @@ static int send_start_msg(struct app *app, struct ast_channel *chan,
struct ast_json *json_args;
int i;
+ struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
ast_assert(chan != NULL);
@@ -636,11 +637,16 @@ static int send_start_msg(struct app *app, struct ast_channel *chan,
return -1;
}
+ if (sanitize && sanitize->channel_snapshot
+ && sanitize->channel_snapshot(snapshot)) {
+ return 0;
+ }
+
msg = ast_json_pack("{s: s, s: o, s: [], s: o}",
"type", "StasisStart",
"timestamp", ast_json_timeval(ast_tvnow(), NULL),
"args",
- "channel", ast_channel_snapshot_to_json(snapshot));
+ "channel", ast_channel_snapshot_to_json(snapshot, NULL));
if (!msg) {
return -1;
}
@@ -665,6 +671,7 @@ static int send_end_msg(struct app *app, struct ast_channel *chan)
{
RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+ struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
ast_assert(chan != NULL);
@@ -674,10 +681,15 @@ static int send_end_msg(struct app *app, struct ast_channel *chan)
return -1;
}
+ if (sanitize && sanitize->channel_snapshot
+ && sanitize->channel_snapshot(snapshot)) {
+ return 0;
+ }
+
msg = ast_json_pack("{s: s, s: o, s: o}",
"type", "StasisEnd",
"timestamp", ast_json_timeval(ast_tvnow(), NULL),
- "channel", ast_channel_snapshot_to_json(snapshot));
+ "channel", ast_channel_snapshot_to_json(snapshot, NULL));
if (!msg) {
return -1;
}
@@ -1153,6 +1165,34 @@ static int unload_module(void)
return 0;
}
+/* \brief Sanitization callback for channel snapshots */
+static int channel_snapshot_sanitizer(const struct ast_channel_snapshot *snapshot)
+{
+ if (!snapshot || !(snapshot->tech_properties & AST_CHAN_TP_INTERNAL)) {
+ return 0;
+ }
+ return 1;
+}
+
+/* \brief Sanitization callback for channel unique IDs */
+static int channel_id_sanitizer(const char *id)
+{
+ RAII_VAR(struct ast_channel_snapshot *, snapshot, ast_channel_snapshot_get_latest(id), ao2_cleanup);
+
+ return channel_snapshot_sanitizer(snapshot);
+}
+
+/* \brief Sanitization callbacks for communication to Stasis applications */
+struct stasis_message_sanitizer app_sanitizer = {
+ .channel_id = channel_id_sanitizer,
+ .channel_snapshot = channel_snapshot_sanitizer,
+};
+
+struct stasis_message_sanitizer *stasis_app_get_sanitizer(void)
+{
+ return &app_sanitizer;
+}
+
static int load_module(void)
{
apps_registry = ao2_container_alloc(APPS_NUM_BUCKETS, app_hash, app_compare);
diff --git a/res/stasis/app.c b/res/stasis/app.c
index 99af6ee57..433d3adb5 100644
--- a/res/stasis/app.c
+++ b/res/stasis/app.c
@@ -276,7 +276,7 @@ static void sub_default_handler(void *data, struct stasis_subscription *sub,
}
/* By default, send any message that has a JSON representation */
- json = stasis_message_to_json(message);
+ json = stasis_message_to_json(message, stasis_app_get_sanitizer());
if (!json) {
return;
}
@@ -295,10 +295,16 @@ static struct ast_json *simple_channel_event(
struct ast_channel_snapshot *snapshot,
const struct timeval *tv)
{
+ struct ast_json *json_channel = ast_channel_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+
+ if (!json_channel) {
+ return NULL;
+ }
+
return ast_json_pack("{s: s, s: o, s: o}",
"type", type,
"timestamp", ast_json_timeval(*tv, NULL),
- "channel", ast_channel_snapshot_to_json(snapshot));
+ "channel", json_channel);
}
static struct ast_json *channel_created_event(
@@ -312,12 +318,18 @@ static struct ast_json *channel_destroyed_event(
struct ast_channel_snapshot *snapshot,
const struct timeval *tv)
{
+ struct ast_json *json_channel = ast_channel_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+
+ if (!json_channel) {
+ return NULL;
+ }
+
return ast_json_pack("{s: s, s: o, s: i, s: s, s: o}",
"type", "ChannelDestroyed",
"timestamp", ast_json_timeval(*tv, NULL),
"cause", snapshot->hangupcause,
"cause_txt", ast_cause2str(snapshot->hangupcause),
- "channel", ast_channel_snapshot_to_json(snapshot));
+ "channel", json_channel);
}
static struct ast_json *channel_state_change_event(
@@ -353,6 +365,7 @@ static struct ast_json *channel_dialplan(
const struct timeval *tv)
{
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+ struct ast_json *json_channel;
/* No Newexten event on cache clear or first event */
if (!old_snapshot || !new_snapshot) {
@@ -368,12 +381,17 @@ static struct ast_json *channel_dialplan(
return NULL;
}
+ json_channel = ast_channel_snapshot_to_json(new_snapshot, stasis_app_get_sanitizer());
+ if (!json_channel) {
+ return NULL;
+ }
+
return ast_json_pack("{s: s, s: o, s: s, s: s, s: o}",
"type", "ChannelDialplan",
"timestamp", ast_json_timeval(*tv, NULL),
"dialplan_app", new_snapshot->appl,
"dialplan_app_data", new_snapshot->data,
- "channel", ast_channel_snapshot_to_json(new_snapshot));
+ "channel", json_channel);
}
static struct ast_json *channel_callerid(
@@ -382,6 +400,7 @@ static struct ast_json *channel_callerid(
const struct timeval *tv)
{
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+ struct ast_json *json_channel;
/* No NewCallerid event on cache clear or first event */
if (!old_snapshot || !new_snapshot) {
@@ -392,13 +411,18 @@ static struct ast_json *channel_callerid(
return NULL;
}
+ json_channel = ast_channel_snapshot_to_json(new_snapshot, stasis_app_get_sanitizer());
+ if (!json_channel) {
+ return NULL;
+ }
+
return ast_json_pack("{s: s, s: o, s: i, s: s, s: o}",
"type", "ChannelCallerId",
"timestamp", ast_json_timeval(*tv, NULL),
"caller_presentation", new_snapshot->caller_pres,
"caller_presentation_txt", ast_describe_caller_presentation(
new_snapshot->caller_pres),
- "channel", ast_channel_snapshot_to_json(new_snapshot));
+ "channel", json_channel);
}
static channel_snapshot_monitor channel_monitors[] = {
@@ -448,10 +472,16 @@ static struct ast_json *simple_endpoint_event(
struct ast_endpoint_snapshot *snapshot,
const struct timeval *tv)
{
+ struct ast_json *json_endpoint = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+
+ if (!json_endpoint) {
+ return NULL;
+ }
+
return ast_json_pack("{s: s, s: o, s: o}",
"type", type,
"timestamp", ast_json_timeval(*tv, NULL),
- "endpoint", ast_endpoint_snapshot_to_json(snapshot));
+ "endpoint", json_endpoint);
}
static void sub_endpoint_update_handler(void *data,
@@ -489,10 +519,15 @@ static struct ast_json *simple_bridge_event(
struct ast_bridge_snapshot *snapshot,
const struct timeval *tv)
{
+ struct ast_json *json_bridge = ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
+ if (!json_bridge) {
+ return NULL;
+ }
+
return ast_json_pack("{s: s, s: o, s: o}",
"type", type,
"timestamp", ast_json_timeval(*tv, NULL),
- "bridge", ast_bridge_snapshot_to_json(snapshot));
+ "bridge", json_bridge);
}
static void sub_bridge_update_handler(void *data,
@@ -521,7 +556,7 @@ static void sub_bridge_update_handler(void *data,
if (!new_snapshot) {
json = simple_bridge_event("BridgeDestroyed", old_snapshot, tv);
} else if (!old_snapshot) {
- json = simple_bridge_event("BridgeCreated", old_snapshot, tv);
+ json = simple_bridge_event("BridgeCreated", new_snapshot, tv);
}
if (!json) {