Sanitize other values when generating TypeScript bindings.#2478
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the safety of generated TypeScript bindings by escaping additional user-provided values before embedding them into generated .ts source, reducing the risk of malformed output and code injection via string literal breakouts.
Changes:
- Exposes
sanitize_stringfor crate-internal reuse. - Escapes
network_passphraseandcontract_idbefore generating thenetworksconstant in the TypeScript project boilerplate. - Adds a regression test to ensure the generated networks snippet doesn’t contain unescaped injected code.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
cmd/crates/soroban-spec-typescript/src/lib.rs |
Makes sanitize_string available to other modules within the crate to centralize TS string escaping. |
cmd/crates/soroban-spec-typescript/src/boilerplate.rs |
Applies string escaping to generated networks values and adds a test to validate sanitization. |
Comments suppressed due to low confidence (1)
cmd/crates/soroban-spec-typescript/src/lib.rs:416
sanitize_stringstill allows the Unicode line separator characters U+2028 and U+2029 through unescaped. In JS/TS source, these are treated as line terminators and can break a string literal (potentially re-enabling code injection even after escaping\n/\r). Consider escaping them as\\u2028/\\u2029(and adding a regression test), or switching to a string-escaping routine that guarantees JS/TS source compatibility for all line terminators.
/// Escape a string for use in a TypeScript string literal
pub(crate) fn sanitize_string(s: &str) -> String {
s.replace('\\', "\\\\")
.replace('"', "\\\"")
.replace('\n', "\\n")
.replace('\r', "\\r")
}
mootz12
approved these changes
Apr 14, 2026
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.
What
Sanitize other values when generating TypeScript bindings.
Why
So values are properly escaped.
Known limitations
Doesn't handle JS/TS keywords.