fix(@angular/build): escape providersFile specifier in generated Vitest test setup - #33899
Open
herdiyana256 wants to merge 1 commit into
Open
fix(@angular/build): escape providersFile specifier in generated Vitest test setup#33899herdiyana256 wants to merge 1 commit into
herdiyana256 wants to merge 1 commit into
Conversation
…st test setup The Vitest unit-test runner builds the TestBed initialization virtual file by interpolating the `providersFile` path from the project configuration directly into an import statement. A value containing a quote or newline terminated the import specifier early, so the trailing text was emitted as executable code in the generated file and ran when the tests were executed. The specifier is now constructed with `JSON.stringify`, so any quotes, backslashes, or newlines in the path are escaped and the value can only ever be a single string literal.
There was a problem hiding this comment.
Code Review
This pull request exports the createTestBedInitVirtualFile function and updates it to use JSON.stringify when generating the import path for providersImport. This change prevents potential code injection vulnerabilities by escaping quotes, backslashes, and newlines in the providersFile path. Additionally, a new test suite build-options_spec.ts has been added to verify correct behavior and ensure malicious paths are properly escaped. I have no feedback to provide.
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.
The Vitest unit-test runner builds the TestBed initialization virtual file by interpolating the
providersFilepath into an import statement.importPathcomes from theprovidersFilebuilder option viapath.relative/path.parse/path.join, none of which strips quotes or newlines, and the option has no schema pattern:A value containing a single quote closes the specifier early, so the trailing characters are emitted as top-level code in the generated file and run when the tests execute. For example,
providersFile: "src/x';someCode();'"with a regularsrc/x.tspresent generates:and
someCode()runs duringng test.This builds the specifier with
JSON.stringifyso any quotes, backslashes, or newlines are escaped and the value can only ever be a single string literal, matching the escaping already used for generated imports elsewhere:Adds a unit test covering the quote and newline cases.