Hi,
we have the following problem:
We are receiving the following data from the server:
{"match_data":{"match_id":"dab9224e-e04c-48e9-9a7d-322249e697b7.nakama-0", "data":"CgZqBAgBEAIKGiIYEAIYCCACKgYKBAgCEAEqCAoECAMQARA/CgQaAggCCkcSRQoVCgYKBAoCAAgKCCIGCgQIAxABEgE4ChUKBgoECgIAAQoIIgYKBAgDEAESATEKFQoGCgQKAgAJCggiBgoECAMQARIBOQo2WjQIAhIcaWkzblUzZU1WMWFWcVRMTmdyRDhuQUVGVmFBMhoMQm9yeXMtb25saW5lIAEqAlBMCjZaNAgDEhxBSmdXNDlIVWxNTUxpVEtySW1yNXJhckJKUkMzGgxCb3J5cy1vbmxpbmUgASoCUEwKCmIICAIQ4NQDGAEKCGIGCAMgASgCCgRSAhAB"}}
By reading the code, we can see that match_data.data
is passed to b64DecodeUnicode
:
function b64DecodeUnicode(str) {
return decodeURIComponent(decode(str).split("").map(function(c) {
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
}).join(""));
}
and the function throws exception while trying to decode %fd
:
Uncaught URIError: URI malformed at decodeURIComponent.
The following snippet shows the problem. The long string to be decoded is the one returned by decode(str) call in above snippet.
'%0a%06%6a%04%08%01%10%03%0a%1a%22%18%10%02%18%08%20%02%2a%06%0a%04%08%02%10%01%2a%08%0a%04%08%03%10%01%10%3f%0a%04%1a%02%08%02%0a%36%5a%34%08%02%12%1c%41%4a%67%57%34%39%48%55%6c%4d%4d%4c%69%54%4b%72%49%6d%72%35%72%61%72%42%4a%52%43%33%1a%0c%42%6f%72%79%73%2d%6f%6e%6c%69%6e%65%20%01%2a%02%50%4c%0a%36%5a%34%08%03%12%1c%69%69%33%6e%55%33%65%4d%56%31%61%56%71%54%4c%4e%67%72%44%38%6e%41%45%46%56%61%41%32%1a%0c%42%6f%72%79%73%2d%6f%6e%6c%69%6e%65%20%01%2a%02%50%4c%0a%0a%62%08%08%02%10%fd%fd%03%18%01%0a%08%62%06%08%03%20%01%28%02%0a%04%52%02%10%01'.split('%').forEach(v => { if (v != '') { console.log(v); decodeURIComponent('%' + v); }});
We have already tried using WebSocketAdapterPb adapter and the problem still exists. The error occures in the internals of the library.
So I attempted to fix this by replacing decodeURIComponent
with unescape
function and it started to decode properly:
function b64DecodeUnicode(str) {
return unescape(decode(str).split("").map(function(c) {
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
}).join(""));
}
however unescape
is deprecated function and decodeURIComponent
should be used instead (but, as I already mentioned - it fails).
So, having this problem solved, another one occured. The result of this function is passed to JSON.parse
:
message.match_data.data = message.match_data.data != null ? JSON.parse(b64DecodeUnicode(message.match_data.data)) : null;
which fails on parsing JSON, as the data passed to it is not JSON string, but binary.
We have verified and other client libraries (Unity and Android) works fine.
Please advice.