Repository navigation
Conversation
…ersectMBO#412) generateTypeScript spliced blueprint-controlled strings into the output raw: a preamble or definition title containing the comment terminator closed its JSDoc early and emitted the rest as code, and validator title/hash/compiledCode and constructor tags broke out of their string literals with a quote. That is the injection GHSA-79p9-vmph-w58f covers for codegen run over an untrusted blueprint. Emit string values with JSON.stringify, quote property names that are not valid identifiers, and neutralise the comment terminator in comment text. Output for ordinary blueprints is unchanged. Five new tests cover the header comment, a definition JSDoc, the validator literals, a constructor tag, and a non-identifier field title; each fails on main. Full suite: 1683 passed / 75 skipped / 0 failed (main baseline 1678 + 5).
Collaborator
|
Closing: we're not accepting bulk |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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
Fixes #412.
generateTypeScript()inblueprint/Codegen.tsspliced blueprint-controlled strings into the generated TypeScript without escaping, so a crafted blueprint (e.g. a third party'splutus.json, the case GHSA-79p9-vmph-w58f is about) could inject statements into the output that run when a consumer builds or imports the generated file.Reproduced locally on main with the new tests: a preamble title of
x */+ newline +globalThis.__pwned = 1+ newline +/*closes the header comment and emits the payload as a bare statement; a validator title containing"breaks out of its string literal the same way.Fix
Three small helpers in
Codegen.ts, applied at every splice point:stringLiteral()— emits a value withJSON.stringify, used for validatortitle/hash/compiledCodeand for constructor tags inTSchema.Literal/TSchema.TaggedStruct.propertyName()— emits a field or variant name bare when it is a valid identifier (the normal case), quoted viaJSON.stringifyotherwise. Applied to struct field names ingenerateTSchema, variant and explicit-union keys, and the property namesgenerateTSTypeemits in type aliases.commentText()— replaces the comment terminator in title/description text placed inside JSDoc (file header, definition comments, validator comments), so untrusted text stays inside its comment.Output for ordinary blueprints is byte-identical:
JSON.stringifyof a plain string is the same double-quoted literal codegen already emitted, identifiers stay bare, and text without a comment terminator is untouched. The existing codegen tests (including theTSchema.Literal("Never" as const, { index: 1 })assertions) pass unchanged.Tests
Five new tests in
test/Blueprint.test.tsunder "Codegen - escaping untrusted blueprint strings (#412)", each verified to fail on main and pass with the fix:JSON.stringifyliterals;TSchema.Literal;not an identifier) is emitted quoted.Full package suite on this branch: 1683 passed / 75 skipped / 0 failed (test files 101 passed / 1 skipped of 102) — exactly main's 1678 baseline plus the 5 new tests.
tsc --noEmitand eslint are clean, and the touched files add no new prettier drift (the source file's prettier diff-line count equals main's, 89 = 89; the test file is prettier-clean).One note from writing the fix: the first draft's own doc comment for the comment-neutralising helper contained the literal terminator sequence and closed itself — the tests caught the resulting parse failure immediately, which is a fair demonstration of how easy this bug is to write.
Independent builder, not affiliated with the Cardano Foundation, IOG or Intersect.
@Kshot9000 (https://x.com/kshot9000) · Tips: addr1q8hnl6vl5a6k3rw3n5g3jtte696zcl76kfatzv7gpswa9r0dj7fma6klq55y4ffm7tf0em09udnyhuk4ah92pl5x9jpqjae44v