Skip to content

Commit aeb6828

Browse files
committed
Check the node type for !<?> tag in case user manually specifies it
1 parent 3e93973 commit aeb6828

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/js-yaml/loader.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,13 +1393,19 @@ function composeNode(state, parentIndent, nodeContext, allowToSeek, allowCompact
13931393

13941394
if (state.tag !== null && state.tag !== '!') {
13951395
if (state.tag === '?') {
1396+
// Implicit resolving is not allowed for non-scalar types, and '?'
1397+
// non-specific tag is only automatically assigned to plain scalars.
1398+
//
1399+
// We only need to check kind conformity in case user explicitly assigns '?'
1400+
// tag, for example like this: "!<?> [0]"
1401+
//
1402+
if (state.result !== null && state.kind !== 'scalar') {
1403+
throwError(state, 'unacceptable node kind for !<?> tag; it should be "scalar", not "' + state.kind + '"');
1404+
}
1405+
13961406
for (typeIndex = 0, typeQuantity = state.implicitTypes.length; typeIndex < typeQuantity; typeIndex += 1) {
13971407
type = state.implicitTypes[typeIndex];
13981408

1399-
// Implicit resolving is not allowed for non-scalar types, and '?'
1400-
// non-specific tag is only assigned to plain scalars. So, it isn't
1401-
// needed to check for 'kind' conformity.
1402-
14031409
if (type.resolve(state.result)) { // `state.result` updated in resolver if matched
14041410
state.result = type.construct(state.result);
14051411
state.tag = type.tag;

test/issues/0525-2.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
4+
var assert = require('assert');
5+
var yaml = require('../../');
6+
7+
8+
test('Should check kind type when resolving !<?> tag', function () {
9+
try {
10+
yaml.safeLoad('!<?> [0]');
11+
} catch (err) {
12+
assert(err.stack.startsWith('YAMLException: unacceptable node kind for !<?> tag'));
13+
return;
14+
}
15+
assert.fail(null, null, 'Expected an error to be thrown');
16+
});

0 commit comments

Comments
 (0)