Skip to content

Commit f0aef80

Browse files
authored
feat: change default search pattern to only include environment files (#82)
BREAKING CHANGE: In order to improve security (and apply `Secure by default`), the default for `searchPattern` has been changed to `{sourceRoot}/**/environments/environment*.ts` (from `{sourceRoot}/**/!(*server*).ts`). If your project uses environment variables in other files, update the `searchPattern` entry in your `ngsscbuild` block in your angular.json.
1 parent c1f4d02 commit f0aef80

File tree

8 files changed

+32
-10
lines changed

8 files changed

+32
-10
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ Starting with version 13, there are experimental builders for the `build` and `s
2020
To install the experimental builders run `ng add angular-server-side-configuration` and answer
2121
`Would you like to use the experimental builders for build and serve?` with `y(es)`.
2222

23+
## Changes with version 16
24+
25+
In order to better follow `Secure by default`, the default for `searchPattern` has been changed
26+
to `{sourceRoot}/**/environments/environment*.ts` (from `{sourceRoot}/**/!(*server*).ts`).
27+
If your project uses environment variables in other files, update the `searchPattern` entry
28+
in your `ngsscbuild` block in your angular.json.
29+
2330
## Changes with version 15.1
2431

2532
With the change in version 15 to search all files for environment variables, a potential
@@ -35,17 +42,18 @@ This should have little to no impact, if you only have a simple angular.json wor
3542
a backend component.
3643

3744
With 15.1 this risk is mitigated, as the lookup path is restricted by adding an option
38-
`searchPattern` which defaults to `{sourceRoot}/**/!(*server*).ts` (only search `sourceRoot`
39-
of the related angular.json project and exclude files with `server` in the name).
45+
`searchPattern` which defaults to ~~`{sourceRoot}/**/!(*server*).ts` (only search `sourceRoot`
46+
of the related angular.json project and exclude files with `server` in the name)~~
47+
`{sourceRoot}/**/environments/environment*.ts` with version 16.
4048

4149
## Changes with version 15
4250

4351
With version 15 of the Angular CLI, environment files are no longer generated by default. In
4452
order to adapt, using `ng add angular-server-side-configuration` will try to add the import in
4553
either `environment.prod.ts`, `environment.ts`, `app.module.ts`, `app.components.ts` or `main.ts`
46-
depending on availability. The ngssc builder will also now search all files for environment
54+
depending on availability. ~~The ngssc builder will also now search all files for environment
4755
variable usages instead of just the defined environment file. Due to this, the
48-
`ngsscEnvironmentFile` option can be removed.
56+
`ngsscEnvironmentFile` option can be removed.~~
4957

5058
## Getting Started
5159

@@ -94,8 +102,8 @@ used environment variables and generate an [ngssc.json](#ngsscjson) in the defin
94102
// Optional
95103
// (Defaults to the basename of the index option of the browser target)
96104
"filePattern": "index.html",
97-
// (Defaults to {sourceRoot}/**/!(*server*).ts)
98-
"searchPattern": "src/environments/environment*.ts"
105+
// (Defaults to {sourceRoot}/**/environments/environment*.ts)
106+
"searchPattern": "src/environments/environment.ts"
99107
},
100108
"configurations": {
101109
"production": {

projects/angular-server-side-configuration/builders/ngsscbuild/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ export async function detectVariables(
5959

6060
const projectMetadata = await context.getProjectMetadata(projectName);
6161
const sourceRoot = projectMetadata.sourceRoot as string | undefined;
62-
const defaultSearchPattern = sourceRoot ? `${sourceRoot}/**/!(*server*).ts` : '**/!(*server*).ts';
62+
const defaultSearchPattern = sourceRoot
63+
? `${sourceRoot}/**/environments/environment*.ts`
64+
: '**/environments/environment*.ts';
6365

6466
const detector = new VariableDetector(context.logger);
6567
const typeScriptFiles = await glob(searchPattern || defaultSearchPattern, {

projects/angular-server-side-configuration/builders/ngsscbuild/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
"searchPattern": {
1919
"type": "string",
20-
"description": "The search pattern to use when searching for environment variable occurrences (Defaults to {sourceRoot}/**/!(*server*).ts)",
20+
"description": "The search pattern to use when searching for environment variable occurrences (Defaults to {sourceRoot}/**/environments/environment*.ts)",
2121
"default": ""
2222
}
2323
}

projects/angular-server-side-configuration/schematics/ng-add/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
} from '@angular-devkit/schematics';
99
import { InsertChange } from '@schematics/angular/utility/change';
1010
import { getWorkspace, updateWorkspace } from '@schematics/angular/utility/workspace';
11-
import type { FileReplacement } from '@angular-devkit/build-angular';
1211
import { Schema } from './schema';
1312

1413
export function ngAdd(options: Schema): Rule {

test/hello-world-app/src/app/app.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
*/
88

99
import { Component } from '@angular/core';
10+
import { environment } from '../environments/environment';
1011

1112
@Component({
1213
selector: 'app-root',
1314
templateUrl: './app.component.html',
1415
styleUrls: ['./app.component.css'],
1516
})
1617
export class AppComponent {
17-
title = process.env.TEST;
18+
title = environment.title;
1819
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const environment = {
2+
title: 'dev',
3+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'angular-server-side-configuration/process';
2+
3+
export const environment = {
4+
title: process.env.TEST,
5+
};

test/hello-world-app/tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"compileOnSave": false,
33
"compilerOptions": {
4+
"paths": {
5+
"angular-server-side-configuration": ["../../dist/angular-server-side-configuration"],
6+
"angular-server-side-configuration/*": ["../../dist/angular-server-side-configuration/*"]
7+
},
48
"baseUrl": "./",
59
"outDir": "./dist/out-tsc",
610
"sourceMap": true,

0 commit comments

Comments
 (0)