Skip to content

Commit

Permalink
Check the node type for !<?> tag in case user manually specifies it
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Nov 4, 2019
1 parent 3e93973 commit aeb6828
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/js-yaml/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1393,13 +1393,19 @@ function composeNode(state, parentIndent, nodeContext, allowToSeek, allowCompact

if (state.tag !== null && state.tag !== '!') {
if (state.tag === '?') {
// Implicit resolving is not allowed for non-scalar types, and '?'
// non-specific tag is only automatically assigned to plain scalars.
//
// We only need to check kind conformity in case user explicitly assigns '?'
// tag, for example like this: "!<?> [0]"
//
if (state.result !== null && state.kind !== 'scalar') {
throwError(state, 'unacceptable node kind for !<?> tag; it should be "scalar", not "' + state.kind + '"');
}

for (typeIndex = 0, typeQuantity = state.implicitTypes.length; typeIndex < typeQuantity; typeIndex += 1) {
type = state.implicitTypes[typeIndex];

// Implicit resolving is not allowed for non-scalar types, and '?'
// non-specific tag is only assigned to plain scalars. So, it isn't
// needed to check for 'kind' conformity.

if (type.resolve(state.result)) { // `state.result` updated in resolver if matched
state.result = type.construct(state.result);
state.tag = type.tag;
Expand Down
16 changes: 16 additions & 0 deletions test/issues/0525-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';


var assert = require('assert');
var yaml = require('../../');


test('Should check kind type when resolving !<?> tag', function () {
try {
yaml.safeLoad('!<?> [0]');
} catch (err) {
assert(err.stack.startsWith('YAMLException: unacceptable node kind for !<?> tag'));
return;
}
assert.fail(null, null, 'Expected an error to be thrown');
});

0 comments on commit aeb6828

Please sign in to comment.