@@ -32,6 +32,11 @@ const {
32
32
isAbsolute : _isAbsoluteIri
33
33
} = require ( './url' ) ;
34
34
35
+ const _HEX = '[0-9A-Fa-f]' ;
36
+ const _UCHAR = '\\u' + _HEX + '{4}|\\U' + _HEX + '{8}' ;
37
+ const IRIREF_RE = new RegExp ( '^([^\\x00-\\x20<>"{}|^`\\\\]|' + _UCHAR + ')*$' ) ;
38
+ const LANG_RE = / ^ [ a - z A - Z ] + ( - [ a - z A - Z 0 - 9 ] + ) * $ / ;
39
+
35
40
const api = { } ;
36
41
module . exports = api ;
37
42
@@ -56,6 +61,11 @@ api.toRDF = (input, options) => {
56
61
if ( graphName === '@default' ) {
57
62
graphTerm = { termType : 'DefaultGraph' , value : '' } ;
58
63
} else if ( _isAbsoluteIri ( graphName ) ) {
64
+ // invalid graph IRI
65
+ if ( ! IRIREF_RE . test ( graphName ) ) {
66
+ continue ;
67
+ }
68
+
59
69
if ( graphName . startsWith ( '_:' ) ) {
60
70
graphTerm = { termType : 'BlankNode' } ;
61
71
} else {
@@ -109,6 +119,11 @@ function _graphToRDF(dataset, graph, graphTerm, issuer, options) {
109
119
continue ;
110
120
}
111
121
122
+ // invalid subject IRI
123
+ if ( ! IRIREF_RE . test ( id ) ) {
124
+ continue ;
125
+ }
126
+
112
127
// RDF predicate
113
128
const predicate = {
114
129
termType : property . startsWith ( '_:' ) ? 'BlankNode' : 'NamedNode' ,
@@ -120,6 +135,11 @@ function _graphToRDF(dataset, graph, graphTerm, issuer, options) {
120
135
continue ;
121
136
}
122
137
138
+ // invalid predicate IRI
139
+ if ( ! IRIREF_RE . test ( property ) ) {
140
+ continue ;
141
+ }
142
+
123
143
// skip blank node predicates unless producing generalized RDF
124
144
if ( predicate . termType === 'BlankNode' &&
125
145
! options . produceGeneralizedRdf ) {
@@ -219,6 +239,11 @@ function _objectToRDF(item) {
219
239
let value = item [ '@value' ] ;
220
240
const datatype = item [ '@type' ] || null ;
221
241
242
+ // invalid datatype IRI
243
+ if ( datatype && ! IRIREF_RE . test ( datatype ) ) {
244
+ return null ;
245
+ }
246
+
222
247
// convert to XSD datatypes as appropriate
223
248
if ( types . isBoolean ( value ) ) {
224
249
object . value = value . toString ( ) ;
@@ -234,6 +259,9 @@ function _objectToRDF(item) {
234
259
object . value = value . toFixed ( 0 ) ;
235
260
object . datatype . value = datatype || XSD_INTEGER ;
236
261
} else if ( '@language' in item ) {
262
+ if ( ! LANG_RE . test ( item [ '@language' ] ) ) {
263
+ return null ;
264
+ }
237
265
object . value = value ;
238
266
object . datatype . value = datatype || RDF_LANGSTRING ;
239
267
object . language = item [ '@language' ] ;
@@ -244,6 +272,12 @@ function _objectToRDF(item) {
244
272
} else {
245
273
// convert string/node object to RDF
246
274
const id = types . isObject ( item ) ? item [ '@id' ] : item ;
275
+
276
+ // invalid object IRI
277
+ if ( ! IRIREF_RE . test ( id ) ) {
278
+ return null ;
279
+ }
280
+
247
281
object . termType = id . startsWith ( '_:' ) ? 'BlankNode' : 'NamedNode' ;
248
282
object . value = id ;
249
283
}
0 commit comments