matrix-org / matrix-org/purple-matrix
glib json empty string bug: 'invalid response from homeserver'
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 223
- Forks
- 44
- PR merge metrics
- No merged PRs in 30d
Description
(This is a placeholder for anyone else unfortunate enough to hit it)
We're suffering from json-glib bug https://bugzilla.gnome.org/show_bug.cgi?id=747279
Where it doesn't like empty keys in objects, but that's valid json and on a really bad day Matrix can generate it.
The case I've seen is a bogus read receipt for an empty event name - matrix doesn't bother sanity checking the event name (nothing in the spec says it should).
If you're stuck then you can try the following gratuitously horrible unsafe hack that seems to work:
Author: Dr. David Alan Gilbert <dave@treblig.org>
Date: Sun Feb 19 17:46:19 2017 +0000
Hack to work around json-glib parser bug
diff --git a/matrix-api.c b/matrix-api.c
index d7b772c..4b39086 100644
--- a/matrix-api.c
+++ b/matrix-api.c
@@ -215,6 +215,27 @@ static int _handle_body(http_parser *http_parser, const char *at,
(int)length, at);
if(strcmp(response_data->content_type, "application/json") == 0) {
+ char *empty_string_key = strstr(at,"\"\":");
+ if (empty_string_key) {
+ /* Work around json glib bug 747279 - it doesn't like empty constant strings - rather unsafe hack - and not freeing and only coping with one
+ */
+ char *at2 = g_malloc(strlen(at)+2);
+ char *atptr;
+ size_t len = empty_string_key - at;
+ //fprintf(stderr, "Bad string: %s\n", at);
+ //fprintf(stderr, "Bad key: %s\n", empty_string_key);
+ memcpy(at2, at, len);
+ atptr = at2+len;
+ *(atptr++) = '"';
+ *(atptr++) = '!';
+ *(atptr++) = '"';
+ *(atptr++) = ':';
+ strcpy(atptr, empty_string_key+3);
+ //fprintf(stderr, "fixed string : %s\n", at2);
+ at=at2;
+ length++;
+ }
+ //fprintf(stderr, "%s: about to parse %s\n", __func__, at);
if(!json_parser_load_from_data(response_data -> json_parser, at, length,
&err)) {
purple_debug_info("matrixprpl", "unable to parse JSON: %s\n",
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in matrix-api.c at _handle_body and the json_parser_load_from_data call; review how application/json responses with empty object keys reach json-glib. Reproduce the malformed response described in the issue and compare its behavior with the existing workaround. Done means the homeserver response is handled without the unsafe workaround and the invalid-response error is resolved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100