Skip to content

Commit

Permalink
fix(@angular/cli): add a flag to let assets outside of outDir
Browse files Browse the repository at this point in the history
On top of #7778
Fixes #8122
  • Loading branch information
hansl committed Nov 20, 2017
1 parent a5dde90 commit 535c85f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/@angular/cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
"type": "string",
"default": "",
"description": "The output path (relative to the outDir)."
},
"allowOutsideOutDir": {
"type": "boolean",
"description": "Allow assets to be copied outside the outDir.",
"default": false
}
},
"additionalProperties": false
Expand Down
16 changes: 13 additions & 3 deletions packages/@angular/cli/models/webpack-configs/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,22 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
asset.output = asset.output || '';
asset.glob = asset.glob || '';

// Prevent asset configurations from writing outside of the output path
// Prevent asset configurations from writing outside of the output path, except if the user
// specify a configuration flag.
// Also prevent writing outside the project path. That is not overridable.
const fullOutputPath = path.resolve(buildOptions.outputPath, asset.output);
if (!fullOutputPath.startsWith(path.resolve(buildOptions.outputPath))) {
const message = 'An asset cannot be written to a location outside of the output path.';
if (!fullOutputPath.startsWith(projectRoot)) {
const message = 'An asset cannot be written to a location outside the project.';
throw new SilentError(message);
}
if (!fullOutputPath.startsWith(path.resolve(buildOptions.outputPath))) {
if (!asset.allowOutsideOutDir) {
const message = 'An asset cannot be written to a location outside of the output path. '
+ 'You can override this message by setting the `allowOutsideOutDir` '
+ 'property on the asset to true in the CLI configuration.';
throw new SilentError(message);
}
}

// Ensure trailing slash.
if (isDirectory(path.resolve(asset.input))) {
Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/models/webpack-configs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@ export interface AssetPattern {
glob: string;
input?: string;
output?: string;
allowOutsideOutDir?: boolean;
}
23 changes: 22 additions & 1 deletion tests/e2e/tests/build/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,31 @@ export default function () {
.then(() => updateJsonFile('.angular-cli.json', configJson => {
const app = configJson['apps'][0];
app['assets'] = [
{ 'glob': '**/*', 'input': '../node_modules/some-package/', 'output': '../package-folder' }
{ 'glob': '**/*', 'input': '../node_modules/some-package/', 'output': '../temp' }
];
}))
.then(() => expectToFail(() => ng('build')))

// Set an exception for the invalid asset config in .angular-cli.json.
.then(() => updateJsonFile('.angular-cli.json', configJson => {
const app = configJson['apps'][0];
app['assets'] = [
{ 'glob': '**/*', 'input': '../node_modules/some-package/', 'output': '../temp',
'allowOutsideOutDir': true }
];
}))
.then(() => ng('build'))

// This asset should fail even with the exception above.
.then(() => updateJsonFile('.angular-cli.json', configJson => {
const app = configJson['apps'][0];
app['assets'] = [
{ 'glob': '**/*', 'input': '../node_modules/some-package/', 'output': '../../temp',
'allowOutsideOutDir': true }
];
}))
.then(() => expectToFail(() => ng('build')))

// Add asset config in .angular-cli.json.
.then(() => updateJsonFile('.angular-cli.json', configJson => {
const app = configJson['apps'][0];
Expand Down

0 comments on commit 535c85f

Please sign in to comment.