Skip to content

Commit 9bd8462

Browse files
alejandro-perezsimo5
authored andcommitted
JSON strings need to be escaped (i.e. replace " with \")
Reviewed-by: Simo Sorce <simo@redhat.com> Closes #125
1 parent eb8ed98 commit 9bd8462

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/environ.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,37 @@ static void mag_set_env_name_attr(request_rec *req, struct mag_conn *mc,
8787
}
8888
}
8989

90+
static char* mag_escape_display_value(request_rec *req, gss_buffer_desc disp_value)
91+
{
92+
/* This function returns a copy (in the pool) of the given gss_buffer_t where every
93+
* occurrence of " has been replaced by \". This string is NULL terminated */
94+
int i = 0, j = 0, n_quotes = 0;
95+
char *escaped_value = NULL;
96+
char *value = (char*) disp_value.value;
97+
98+
// count number of quotes in the input string
99+
for (i = 0, j = 0; i < disp_value.length; i++)
100+
if (value[i] == '"')
101+
n_quotes++;
102+
103+
// if there are no quotes, just return a copy of the string
104+
if (n_quotes == 0)
105+
return apr_pstrndup(req->pool, value, disp_value.length);
106+
107+
// gss_buffer_t are not \0 terminated, but our result will be
108+
escaped_value = apr_palloc(req->pool, disp_value.length + n_quotes + 1);
109+
for (i = 0,j = 0; i < disp_value.length; i++, j++) {
110+
if (value[i] == '"') {
111+
escaped_value[j] = '\\';
112+
j++;
113+
}
114+
escaped_value[j] = value[i];
115+
}
116+
// make the string NULL terminated
117+
escaped_value[j] = '\0';
118+
return escaped_value;
119+
}
120+
90121
static void mag_add_json_name_attr(request_rec *req, bool first,
91122
struct name_attr *attr, char **json)
92123
{
@@ -106,8 +137,8 @@ static void mag_add_json_name_attr(request_rec *req, bool first,
106137
attr->value.length);
107138
}
108139
if (attr->display_value.length != 0) {
109-
len = attr->display_value.length;
110-
value = (const char *)attr->display_value.value;
140+
value = mag_escape_display_value(req, attr->display_value);
141+
len = strlen(value);
111142
}
112143
if (attr->number == 1) {
113144
*json = apr_psprintf(req->pool,

0 commit comments

Comments
 (0)