Skip to content

fix(tokeer): escape JSON string values in the redeem request body - #6

Open
3304711297 wants to merge 1 commit into
madoiscool:mainfrom
3304711297:fix/tokeer-json-escape
Open

3304711297 wants to merge 1 commit into
madoiscool:mainfrom
3304711297:fix/tokeer-json-escape

Conversation

@3304711297

Copy link
Copy Markdown

Problem

TokeerBridge::Redeem() builds the request body for POST /drm/redeem by concatenating
the redeem code straight into a JSON string, with no escaping:

const std::string url = TokeerBaseUrl() + "/drm/redeem";
const std::string reqBody = "{\"code\":\"" + code + "\"}";

code comes verbatim from the bst:// URL — HandleUri() splits the URL on / and
passes the remainder through unchanged:

const std::string arg = (slash == std::string::npos) ? "" : rest.substr(slash + 1);

if (action == "redeem") {
    if (!arg.empty()) Redeem(arg);

JSON requires that " inside a string value be escaped. A value containing a double
quote therefore terminates the string early and changes the structure of the request,
so such a value cannot be represented correctly. A backslash or a control character has
the same effect.

The scheme is registered in HKCU\Software\Classes\bst, so the value is reachable
from a URL handler, i.e. it is not restricted to a locally typed value.

Change

Adds a JsonEscape() helper in the same anonymous namespace as the existing
hand-rolled JSON helpers, and applies it to the value that is embedded into the
request body:

const std::string reqBody = "{\"code\":\"" + JsonEscape(code) + "\"}";

JsonEscape() escapes " and \, maps the named control escapes (\b, \f,
\n, \r, \t), and emits \u00XX for the remaining control characters, per
RFC 8259.

Scope

One file, one call site. The other JSON helper in this file (JsonString(), which
parses the response) is left as is — this change only covers the request side.

Verification

Built with MSVC 19.44 (VS 2022) in Release; TokeerBridge.cpp compiles clean.

Note for the reviewer: an unrelated pre-existing build issue in this tree surfaces
on a machine with a GBK (code page 936) system locale — Hooks_Manifest.cpp and
Dialog.h contain non-ASCII characters in comments and produce
warning C4819 plus a syntax error at Hooks_Manifest.cpp:236. It reproduces on an
unmodified checkout and is not related to this change. Building with /utf-8 clears
it.

The redeem code is taken verbatim from the bst:// URL and concatenated into a
JSON request body without escaping. A value containing a double quote ends the
JSON string early and changes the structure of the request, so the value can
not be represented correctly.

Escape the value per RFC 8259 before embedding it: double quote, backslash and
the control characters (using \u00XX for the remaining ones).

This is the same shape as the existing hand-rolled JSON helpers in this file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant