Skip to content

fix(@angular/build): escape providersFile specifier in generated Vitest test setup - #33899

Open
herdiyana256 wants to merge 1 commit into
angular:mainfrom
herdiyana256:fix/unit-test-providers-file-injection
Open

fix(@angular/build): escape providersFile specifier in generated Vitest test setup#33899
herdiyana256 wants to merge 1 commit into
angular:mainfrom
herdiyana256:fix/unit-test-providers-file-injection

Conversation

@herdiyana256

@herdiyana256 herdiyana256 commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

The Vitest unit-test runner builds the TestBed initialization virtual file by interpolating the providersFile path into an import statement. importPath comes from the providersFile builder option via path.relative/path.parse/path.join, none of which strips quotes or newlines, and the option has no schema pattern:

const importPath = toPosixPath(path.join(dir, name));
providersImport = `import providers from './${importPath}';`;

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 regular src/x.ts present generates:

import providers from './x';someCode();'';

and someCode() runs during ng test.

This builds the specifier with JSON.stringify so 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:

providersImport = `import providers from ${JSON.stringify('./' + importPath)};`;

Adds a unit test covering the quote and newline cases.

…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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant