Skip to content

Commit 0b132b5

Browse files
RomainMullerrix0rrr
authored andcommitted
fix: Switch from js-yaml to yaml (#1092)
Implementations that produce `YAML-1.2` in compatible mode still can end up producing YAML that will be icnorrectly parsed by CloudFormation. For example, literal strings containing only numbers and starting with a `0` do not get quoted, which results in a `YAML-1.1` parser interpreting those as a number. This can lead to invalid principals being generated where the account ID form is used (for example, in Lambda permissions objects). Switching to `yaml@1.0.0` addresses this as it can be configured to emit `YAML-1.1` explicitly.
1 parent ae03ddb commit 0b132b5

File tree

7 files changed

+53
-20
lines changed

7 files changed

+53
-20
lines changed

packages/@aws-cdk/applet-js/bin/cdk-applet-js.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import child_process = require('child_process');
66
import fs = require('fs-extra');
77
import os = require('os');
88
import path = require('path');
9+
import YAML = require('yaml');
910
import { isStackConstructor, parseApplet } from '../lib/applet-helpers';
1011

11-
// tslint:disable-next-line:no-var-requires
12-
const YAML = require('js-yaml');
13-
1412
main().catch(e => {
1513
// tslint:disable-next-line:no-console
1614
console.error(e);
@@ -26,7 +24,7 @@ async function main() {
2624
}
2725

2826
// read applet(s) properties from the provided file
29-
const fileContents = YAML.safeLoad(await fs.readFile(appletFile, { encoding: 'utf-8' }));
27+
const fileContents = YAML.parse(await fs.readFile(appletFile, { encoding: 'utf-8' }), { schema: 'yaml-1.1' });
3028
if (typeof fileContents !== 'object') {
3129
throw new Error(`${appletFile}: should contain a YAML object`);
3230
}

packages/@aws-cdk/applet-js/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@
2222
},
2323
"license": "Apache-2.0",
2424
"devDependencies": {
25-
"@types/yamljs": "^0.2.0",
25+
"@types/fs-extra": "^5.0.4",
26+
"@types/yaml": "^1.0.0",
2627
"cdk-build-tools": "^0.14.1",
2728
"pkglint": "^0.14.1"
2829
},
2930
"dependencies": {
3031
"@aws-cdk/cdk": "^0.14.1",
31-
"@types/fs-extra": "^5.0.4",
32-
"@types/js-yaml": "^3.11.2",
3332
"fs-extra": "^7.0.0",
34-
"js-yaml": "^3.12.0",
35-
"source-map-support": "^0.5.6"
33+
"source-map-support": "^0.5.6",
34+
"yaml": "^1.0.0"
3635
},
3736
"repository": {
3837
"url": "https://github.com/awslabs/aws-cdk.git",

packages/aws-cdk/bin/cdk.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import 'source-map-support/register';
44
import cxapi = require('@aws-cdk/cx-api');
55
import colors = require('colors/safe');
66
import fs = require('fs-extra');
7-
import YAML = require('js-yaml');
87
import minimatch = require('minimatch');
98
import util = require('util');
9+
import YAML = require('yaml');
1010
import yargs = require('yargs');
1111
import cdkUtil = require('../lib/util');
1212

@@ -610,7 +610,7 @@ async function initCommandLine() {
610610
/* Attempt to parse YAML, fall back to JSON. */
611611
function parseTemplate(text: string): any {
612612
try {
613-
return YAML.safeLoad(text);
613+
return YAML.parse(text, { schema: 'yaml-1.1' });
614614
} catch (e) {
615615
return JSON.parse(text);
616616
}
@@ -684,9 +684,7 @@ async function initCommandLine() {
684684
const indentWidth = 2;
685685
return JSON.stringify(object, noFiltering, indentWidth);
686686
} else {
687-
const inlineJsonAfterDepth = 16;
688-
const indentWidth = 4;
689-
return YAML.safeDump(object, { indent: indentWidth, flowLevel: inlineJsonAfterDepth });
687+
return YAML.stringify(object, { schema: 'yaml-1.1' });
690688
}
691689
}
692690
}

packages/aws-cdk/lib/api/deploy-stack.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import cxapi = require('@aws-cdk/cx-api');
22
import aws = require('aws-sdk');
33
import colors = require('colors/safe');
4-
import YAML = require('js-yaml');
54
import uuid = require('uuid');
5+
import YAML = require('yaml');
66
import { prepareAssets } from '../assets';
77
import { debug, error, print } from '../logging';
88
import { Mode } from './aws-auth/credentials';
@@ -113,7 +113,7 @@ async function getStackOutputs(cfn: aws.CloudFormation, stackName: string): Prom
113113
* @param toolkitInfo information about the toolkit stack
114114
*/
115115
async function makeBodyParameter(stack: cxapi.SynthesizedStack, toolkitInfo?: ToolkitInfo): Promise<TemplateBodyParameter> {
116-
const templateJson = YAML.safeDump(stack.template, { indent: 4, flowLevel: 16 });
116+
const templateJson = YAML.stringify(stack.template, { schema: 'yaml-1.1' });
117117
if (toolkitInfo) {
118118
const s3KeyPrefix = `cdk/${stack.name}/`;
119119
const s3KeySuffix = '.yml';

packages/aws-cdk/package-lock.json

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/aws-cdk/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
"devDependencies": {
3434
"@types/archiver": "^2.1.2",
3535
"@types/fs-extra": "^4.0.8",
36-
"@types/js-yaml": "^3.11.2",
3736
"@types/minimatch": "^3.0.3",
3837
"@types/mockery": "^1.4.29",
3938
"@types/request": "^2.47.1",
4039
"@types/semver": "^5.5.0",
4140
"@types/uuid": "^3.4.3",
41+
"@types/yaml": "^1.0.0",
4242
"@types/yargs": "^8.0.3",
4343
"cdk-build-tools": "^0.14.1",
4444
"mockery": "^2.1.0",
@@ -54,14 +54,14 @@
5454
"colors": "^1.2.1",
5555
"decamelize": "^2.0.0",
5656
"fs-extra": "^4.0.2",
57-
"js-yaml": "^3.12.0",
5857
"json-diff": "^0.3.1",
5958
"minimatch": ">=3.0",
6059
"promptly": "^0.2.0",
6160
"proxy-agent": "^3.0.1",
6261
"request": "^2.83.0",
6362
"semver": "^5.5.0",
6463
"source-map-support": "^0.5.6",
64+
"yaml": "^1.0.0",
6565
"yargs": "^9.0.1"
6666
},
6767
"repository": {

packages/aws-cdk/test/test.yaml.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Test } from 'nodeunit';
2+
import YAML = require('yaml');
3+
4+
export = {
5+
'validate that our YAML quotes the word "ON"'(test: Test) {
6+
// tslint:disable-next-line:no-console
7+
const output = YAML.stringify({
8+
notABoolean: "ON"
9+
}, { schema: 'yaml-1.1' });
10+
11+
test.equals(output.trim(), 'notABoolean: "ON"');
12+
13+
test.done();
14+
},
15+
16+
'validate that our YAML correctly quotes strings with a leading zero'(test: Test) {
17+
const output = YAML.stringify({
18+
leadingZero: "0123456789"
19+
} , { schema: 'yaml-1.1' });
20+
21+
test.equals(output.trim(), 'leadingZero: "0123456789"');
22+
23+
test.done();
24+
},
25+
26+
'validate that our YAML correctly parses strings with a leading zero'(test: Test) {
27+
const output = YAML.parse('leadingZero: "0123456789"', { schema: 'yaml-1.1' });
28+
29+
test.deepEqual(output, { leadingZero: '0123456789' });
30+
31+
test.done();
32+
},
33+
};

0 commit comments

Comments
 (0)