Skip to content

Commit 4d53f39

Browse files
gkelloggdavidlehn
authored andcommitted
Better frame validation.
1 parent 06cc9a1 commit 4d53f39

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Handling of `@` values for `@reverse`.
1919
- Changes in object embedding.
2020
- Better support for graph framing.
21+
- Better frame validation.
2122

2223
### Changed
2324
- Keep term definitions mapping to null so they may be protected.

lib/frame.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {isKeyword} = require('./context');
77
const graphTypes = require('./graphTypes');
88
const types = require('./types');
99
const util = require('./util');
10+
const url = require('./url');
1011
const JsonLdError = require('./JsonLdError');
1112
const {
1213
createNodeMap: _createNodeMap,
@@ -453,6 +454,32 @@ function _validateFrame(frame) {
453454
'Invalid JSON-LD syntax; a JSON-LD frame must be a single object.',
454455
'jsonld.SyntaxError', {frame});
455456
}
457+
458+
if(frame[0].hasOwnProperty('@id')) {
459+
for(const id of util.asArray(frame[0]['@id'])) {
460+
// @id must be wildcard or an IRI
461+
if(!(types.isObject(id) || url.isAbsolute(id)) ||
462+
(types.isString(id) && id.indexOf('_:') === 0))
463+
{
464+
throw new JsonLdError(
465+
'Invalid JSON-LD syntax; invalid @id in frame.',
466+
'jsonld.SyntaxError', {code: 'invalid frame', frame});
467+
}
468+
}
469+
}
470+
471+
if(frame[0].hasOwnProperty('@type')) {
472+
for(const type of util.asArray(frame[0]['@type'])) {
473+
// @id must be wildcard or an IRI
474+
if(!(types.isObject(type) || url.isAbsolute(type)) ||
475+
(types.isString(type) && type.indexOf('_:') === 0))
476+
{
477+
throw new JsonLdError(
478+
'Invalid JSON-LD syntax; invalid @type in frame.',
479+
'jsonld.SyntaxError', {code: 'invalid frame', frame});
480+
}
481+
}
482+
}
456483
}
457484

458485
/**
@@ -733,7 +760,7 @@ function _cleanupPreserve(input, options) {
733760
}
734761
}
735762
return input;
736-
};
763+
}
737764

738765
/**
739766
* Adds framing output to the given parent.

lib/jsonld.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ const {toRDF: _toRDF} = require('./toRdf');
4949

5050
const {
5151
frameMergedOrDefault: _frameMergedOrDefault,
52-
cleanupNull: _cleanupNull,
53-
cleanupPreserve: _cleanupPreserve
52+
cleanupNull: _cleanupNull
5453
} = require('./frame');
5554

5655
const {
@@ -488,7 +487,7 @@ jsonld.frame = async function(input, frame, options) {
488487
opts.is11 = _processingMode(activeCtx, 1.1);
489488

490489
// do framing
491-
let framed = _frameMergedOrDefault(expanded, expandedFrame, opts);
490+
const framed = _frameMergedOrDefault(expanded, expandedFrame, opts);
492491

493492
opts.graph = !options.omitGraph;
494493
opts.skipExpansion = true;

tests/test-common.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ const TEST_TYPES = {
126126
specVersion: ['json-ld-1.0'],
127127
// FIXME
128128
idRegex: [
129-
// blank nodes
130-
/frame-manifest.jsonld#t0052$/,
131-
/frame-manifest.jsonld#t0053$/,
132129
// lists
133130
/frame-manifest.jsonld#t0055$/,
134131
/frame-manifest.jsonld#t0058$/,

0 commit comments

Comments
 (0)