Skip to content

Commit cb4a373

Browse files
committed
fix(@schematics/angular): escape module specifier in insertImport
insertImport builds an `import ... from '<fileName>'` statement by interpolating fileName directly between single quotes with no escaping. A fileName containing a single quote breaks out of the string literal, so any caller that passes untrusted data as the module specifier (e.g. a schematic using addRootProvider's external() with a value sourced from user/project config) ends up inserting attacker-controlled source into the target file rather than a plain import path. fileName is now escaped for backslashes and single quotes before being interpolated, matching the existing single-quote output format so the common case (a real module specifier) is byte-for-byte unchanged.
1 parent 0ae5690 commit cb4a373

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

packages/schematics/angular/utility/ast-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ export function insertImport(
7777
// if there are no imports or 'use strict' statement, insert import at beginning of file
7878
const insertAtBeginning = allImports.length === 0 && useStrict.length === 0;
7979
const separator = insertAtBeginning ? '' : `;${eol}`;
80+
const escapedFileName = fileName.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
8081
const toInsert =
8182
`${separator}import ${open}${importExpression}${close}` +
82-
` from '${fileName}'${insertAtBeginning ? `;${eol}` : ''}`;
83+
` from '${escapedFileName}'${insertAtBeginning ? `;${eol}` : ''}`;
8384

8485
return insertAfterLastOccurrence(
8586
allImports,

0 commit comments

Comments
 (0)