-
Notifications
You must be signed in to change notification settings - Fork 11.9k
fix(@schematics/angular): add trusted-proxy-headers migration #33065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+204
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
97 changes: 97 additions & 0 deletions
97
packages/schematics/angular/migrations/trust-proxy-headers/migration.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { Rule } from '@angular-devkit/schematics'; | ||
| import ts from 'typescript'; | ||
| import { allTargetOptions, allWorkspaceTargets, getWorkspace } from '../../utility/workspace'; | ||
|
|
||
| const TODO_COMMENT = | ||
| '// TODO: This is a security-sensitive option. Remove if not needed. ' + | ||
| 'For more information, see https://angular.dev/best-practices/security#configuring-trusted-proxy-headers'; | ||
|
|
||
| export default function (): Rule { | ||
| return async (tree) => { | ||
| const workspace = await getWorkspace(tree); | ||
| const serverFiles = new Set<string>(); | ||
|
|
||
| for (const [targetName, target] of allWorkspaceTargets(workspace)) { | ||
| if (targetName !== 'build') { | ||
| continue; | ||
| } | ||
|
|
||
| for (const [, options] of allTargetOptions(target)) { | ||
| if (typeof options?.['server'] === 'string') { | ||
| serverFiles.add(options['server']); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const path of serverFiles) { | ||
| if (!tree.exists(path)) { | ||
| continue; | ||
| } | ||
|
|
||
| const content = tree.readText(path); | ||
| if (content.includes(TODO_COMMENT)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!content.includes('AngularAppEngine') && !content.includes('AngularNodeAppEngine')) { | ||
| continue; | ||
| } | ||
|
|
||
| const sourceFile = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true); | ||
| const recorder = tree.beginUpdate(path); | ||
|
|
||
| function visit(node: ts.Node) { | ||
| if ( | ||
| ts.isNewExpression(node) && | ||
| ts.isIdentifier(node.expression) && | ||
| (node.expression.text === 'AngularNodeAppEngine' || | ||
| node.expression.text === 'AngularAppEngine') | ||
| ) { | ||
| // Check arguments | ||
| if (!node.arguments || node.arguments.length === 0) { | ||
| // Case 1: No arguments passed | ||
| const insertPos = node.end - 1; // right before ) | ||
| recorder.insertRight( | ||
| insertPos, | ||
| `{\n ${TODO_COMMENT}\n ` + | ||
| `trustProxyHeaders: ['x-forwarded-host', 'x-forwarded-proto'],\n}`, | ||
| ); | ||
| } else if (node.arguments.length > 0) { | ||
| const firstArg = node.arguments[0]; | ||
| if (ts.isObjectLiteralExpression(firstArg)) { | ||
| // Check if trustProxyHeaders is already present | ||
| const hasTrustProxyHeaders = firstArg.properties.some( | ||
| (prop: ts.ObjectLiteralElementLike) => | ||
| ts.isPropertyAssignment(prop) && | ||
| (ts.isIdentifier(prop.name) || ts.isStringLiteral(prop.name)) && | ||
| prop.name.text === 'trustProxyHeaders', | ||
| ); | ||
|
alan-agius4 marked this conversation as resolved.
|
||
|
|
||
| if (!hasTrustProxyHeaders) { | ||
| // Insert right after the opening brace | ||
| const insertPos = firstArg.getStart() + 1; | ||
| recorder.insertRight( | ||
| insertPos, | ||
| `\n ${TODO_COMMENT}\n ` + | ||
| `trustProxyHeaders: ['x-forwarded-host', 'x-forwarded-proto'],`, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ts.forEachChild(node, visit); | ||
| } | ||
|
|
||
| visit(sourceFile); | ||
| tree.commitUpdate(recorder); | ||
| } | ||
| }; | ||
| } | ||
101 changes: 101 additions & 0 deletions
101
packages/schematics/angular/migrations/trust-proxy-headers/migration_spec.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { EmptyTree } from '@angular-devkit/schematics'; | ||
| import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
|
|
||
| function createWorkSpaceConfig(tree: UnitTestTree) { | ||
| const angularConfig = { | ||
| version: 1, | ||
| projects: { | ||
| app: { | ||
| root: '', | ||
| sourceRoot: 'src', | ||
| projectType: 'application', | ||
| architect: { | ||
| build: { | ||
| options: { | ||
| server: '/server.ts', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); | ||
| } | ||
|
|
||
| describe(`Migration to add trustProxyHeaders to server.ts`, () => { | ||
| const schematicName = 'trust-proxy-headers'; | ||
| const schematicRunner = new SchematicTestRunner( | ||
| 'migrations', | ||
| require.resolve('../migration-collection.json'), | ||
| ); | ||
| const TODO_COMMENT = | ||
| '// TODO: This is a security-sensitive option. Remove if not needed. ' + | ||
| 'For more information, see https://angular.dev/best-practices/security#configuring-trusted-proxy-headers'; | ||
|
|
||
| let tree: UnitTestTree; | ||
| beforeEach(() => { | ||
| tree = new UnitTestTree(new EmptyTree()); | ||
| createWorkSpaceConfig(tree); | ||
| }); | ||
|
|
||
| it(`should add trustProxyHeaders to AngularNodeAppEngine with no args`, async () => { | ||
| tree.create( | ||
| '/server.ts', | ||
| `import { AngularNodeAppEngine } from '@angular/ssr/node';\nconst angularApp = new AngularNodeAppEngine();`, | ||
| ); | ||
|
|
||
| const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); | ||
| const content = newTree.readText('/server.ts'); | ||
| expect(content).toContain(`const angularApp = new AngularNodeAppEngine({`); | ||
| expect(content).toContain(TODO_COMMENT); | ||
| expect(content).toContain(`trustProxyHeaders: ['x-forwarded-host', 'x-forwarded-proto'],`); | ||
| }); | ||
|
|
||
| it(`should add trustProxyHeaders to AngularNodeAppEngine with existing args`, async () => { | ||
| tree.create( | ||
| '/server.ts', | ||
| `import { AngularNodeAppEngine } from '@angular/ssr/node';\n` + | ||
| `const angularApp = new AngularNodeAppEngine({\n allowedHosts: ['localhost']\n});`, | ||
| ); | ||
|
|
||
| const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); | ||
| const content = newTree.readText('/server.ts'); | ||
| expect(content).toContain(`const angularApp = new AngularNodeAppEngine({`); | ||
| expect(content).toContain(TODO_COMMENT); | ||
| expect(content).toContain(`trustProxyHeaders: ['x-forwarded-host', 'x-forwarded-proto'],`); | ||
| expect(content).toContain(`allowedHosts: ['localhost']`); | ||
| }); | ||
|
|
||
| it(`should add trustProxyHeaders to AngularAppEngine`, async () => { | ||
| tree.create( | ||
| '/server.ts', | ||
| `import { AngularAppEngine } from '@angular/ssr';\nconst angularApp = new AngularAppEngine();`, | ||
| ); | ||
|
|
||
| const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); | ||
| const content = newTree.readText('/server.ts'); | ||
| expect(content).toContain(`const angularApp = new AngularAppEngine({`); | ||
| expect(content).toContain(TODO_COMMENT); | ||
| expect(content).toContain(`trustProxyHeaders: ['x-forwarded-host', 'x-forwarded-proto'],`); | ||
| }); | ||
|
|
||
| it(`should not add trustProxyHeaders if it already exists`, async () => { | ||
| const originalContent = | ||
| `import { AngularAppEngine } from '@angular/ssr';\n` + | ||
| `const angularApp = new AngularAppEngine({\n trustProxyHeaders: true\n});`; | ||
| tree.create('/server.ts', originalContent); | ||
|
|
||
| const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); | ||
| const content = newTree.readText('/server.ts'); | ||
| expect(content).toBe(originalContent); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.