Escape DID when building DID document to prevent JSON injection#33
Open
achamayou wants to merge 2 commits into
Open
Escape DID when building DID document to prevent JSON injection#33achamayou wants to merge 2 commits into
achamayou wants to merge 2 commits into
Conversation
create_did_document spliced the attacker-influenced DID into the output JSON using std::regex_replace. That had two problems: - The DID was inserted into JSON string values without escaping, so a DID containing '"', '\\' or control characters could break out of the string and corrupt or inject structure into the document. - std::regex_replace interprets '$' in the replacement ($&, $1, etc.), and the sequential _DID_/_LEAF_JWK_ substitutions could interfere with one another. Build the document with plain concatenation and JSON-escape the DID. The leaf JWK is generated internally (base64url values + fixed keys) and is inserted verbatim. Drop the now-unused <regex> include.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens DID document generation against JSON injection by ensuring attacker-influenced did values are JSON-escaped before being embedded into the resolved DID document, and by removing the prior std::regex_replace-based templating approach.
Changes:
- Added a
json_escape_stringhelper to escape quotes, backslashes, standard short escapes, and control characters for safe JSON string embedding. - Reworked
create_did_documentto build JSON via string concatenation, inserting the escaped DID everywhere it is used. - Removed the unused
<regex>include after eliminatingstd::regex_replace.
Comments suppressed due to low confidence (1)
didx509cpp.h:1583
- The new JSON escaping behavior (json_escape_string + embedding did_json) doesn’t appear to be covered by tests. Existing tests parse the DID document JSON, but they only use normal DID inputs and won’t catch regressions where quotes/backslashes/control characters aren’t escaped correctly (the security scenario this PR addresses). Add a unit test that uses a DID containing characters like
",\\, and a control character (e.g.\nvia a literal newline) and assertnlohmann::json::parse(resolve(...))succeeds and thatdoc["id"]/doc["verificationMethod"][0]["id"]match the original DID value (plus#key-1where applicable).
inline std::pair<bool, bool> is_agreed_signature_key(const UqX509& cert)
{
const bool include_assertion_method =
!cert.has_key_usage() || cert.has_key_usage_digital_signature();
const bool include_key_agreement =
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
create_did_documentspliced the attacker-influenceddidinto the output JSON usingstd::regex_replace. This is unsafe for two reasons:didis placed inside JSON string values (id,verificationMethod[].id,controller,assertionMethod,keyAgreement) without escaping. Adidcontaining",\, or control characters could break out of the JSON string and corrupt or inject structure into the resulting document.std::regex_replaceinterprets$in the replacement text ($&,$1,$`,$$), and the sequential_DID_/_LEAF_JWK_substitutions could interfere with one another.Changes
json_escape_stringhelper that escapes",\, the standard short escapes, and control characters (\u00XX).dideverywhere it is embedded.<regex>include.Testing
cmake --build build && ctest— all tests pass. The existing tests parse the resolved document withnlohmann::json, so malformed output would be caught.Found during a security review of the codebase.