Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return no quads for malformed QPF selectors #1171

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions packages/actor-rdf-resolve-hypermedia-qpf/lib/RdfSourceQpf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ export class RdfSourceQpf implements IQuadSource {
}

public match(subject: RDF.Term, predicate: RDF.Term, object: RDF.Term, graph: RDF.Term): AsyncIterator<RDF.Quad> {
if (_isMalformedSelector(subject, predicate, object, graph)) {
return _noQuads();
}

// If we are querying the default graph,
// and the source has an overridden value for the default graph (such as QPF can provide),
// we override the graph parameter with that value.
Expand All @@ -131,16 +135,7 @@ export class RdfSourceQpf implements IQuadSource {
} else if (Object.keys(this.searchForm.mappings).length === 4 && !this.defaultGraph) {
// If the sd:defaultGraph is not declared on a QPF endpoint,
// then the default graph must be empty.
const quads = new ArrayIterator([], { autoStart: false });
quads.setProperty('metadata', {
requestTime: 0,
cardinality: { type: 'exact', value: 0 },
first: null,
next: null,
last: null,
canContainUndefs: false,
});
return quads;
return _noQuads();
}
}

Expand Down Expand Up @@ -248,3 +243,26 @@ function _termToString(term: RDF.Term): string {
'|' :
termToString(term);
}

function _isMalformedSelector(subject: RDF.Term, predicate: RDF.Term, object: RDF.Term, graph: RDF.Term): boolean {
const wellFormed =
(subject.termType === 'Variable' || subject.termType === 'NamedNode') &&
(predicate.termType === 'Variable' || predicate.termType === 'NamedNode') &&
(object.termType === 'Variable' || object.termType === 'NamedNode' || object.termType === 'Literal') &&
(graph.termType === 'Variable' || graph.termType === 'NamedNode' || graph.termType === 'DefaultGraph');

return !wellFormed;
}

function _noQuads(): AsyncIterator<RDF.Quad> {
const quads = new ArrayIterator([], { autoStart: false });
quads.setProperty('metadata', {
requestTime: 0,
cardinality: { type: 'exact', value: 0 },
first: null,
next: null,
last: null,
canContainUndefs: false,
});
return quads;
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,70 @@ describe('RdfSourceQpf', () => {
]);
});

describe('[QPF] should not return any quads if if the selector is malformed', () => {
const L = DF.literal('l');
const B = DF.blankNode();
const D = DF.defaultGraph();

beforeEach(() => {
metadata = {
searchForms: {
values: [
{
getUri: (entries: any) => `${entries.s || '_'},${entries.p || '_'},${entries.o || '_'
},${entries.g || '_'}`,
mappings: {
g: 'G',
o: 'O',
p: 'P',
s: 'S',
},
},
],
},
};

source = new RdfSourceQpf(
mediatorMetadata,
mediatorMetadataExtract,
mediatorDereferenceRdf,
's',
'p',
'o',
'g',
metadata,
new ActionContext(),
streamifyArray([
quad('"L"', 'p', 'o', 'g'),
quad('_:B', 'p', 'o', 'g'),
quad('', 'p', 'o', 'g'),
quad('s', '"L"', 'o', 'g'),
quad('s', '_:B', 'o', 'g'),
quad('s', '', 'o', 'g'),
quad('s', 'p', '_:B', 'g'),
quad('s', 'p', '', 'g'),
quad('s', 'p', 'o', '"L"'),
quad('s', 'p', 'o', '_:B'),
]),
);
});

it.each([
[ 'subject is a literal', L, v, v, v ],
[ 'subject is a blank', B, v, v, v ],
[ 'subject is a graph', D, v, v, v ],
[ 'predicate is a literal', v, L, v, v ],
[ 'predicate is a blank', v, B, v, v ],
[ 'predicate is a graph', v, D, v, v ],
[ 'object is a blank', v, v, B, v ],
[ 'object is a graph', v, v, D, v ],
[ 'graph is a literal', v, v, v, L ],
[ 'graph is a blank', v, v, v, B ],
])('when the %s node', async(title: string, s: RDF.Term, p: RDF.Term, o: RDF.Term, g: RDF.Term) => {
expect(await arrayifyStream(source.match(s, p, o, g))).toBeRdfIsomorphic([]);
});
});

it('should return multiple copies of the initial quads for the empty pattern', async() => {
expect(await arrayifyStream(source.match(v, v, v, v))).toBeRdfIsomorphic([
quad('s1', 'p1', 'o1'),
Expand Down