digitalbazaar / jsonld.js

A JSON-LD Processor and API implementation in JavaScript
https://json-ld.org/
Other
1.64k stars 195 forks source link

Improve `@language` and `@direction` safe mode checks. #541

Open davidlehn opened 8 months ago

davidlehn commented 8 months ago

If @language and/or @direction are incorrectly used as a regular properties, they appear in expanded output, but are silently dropped when converting to RDF quads. Determine if this should be a general error, general warning, safe mode error, or something else. May need main spec and/or test suite updates. May need to check if this happens with other keywords as well.

{
  "@language": "foo",
  "@direction": "rtl",
  "ex:a": "bar"
}
BigBlueHat commented 8 months ago

@gkellogg would love your take on why this happens. Distiller throws an error, so it's likely a bug in jsonld.js (and therefore the JSON-LD Playground). I doubt it's something we could make use of in a JSON-LD 1.2--i.e. allowing the use of these for the document even when a context file is provided out of band. Would be kind of nice, though, potentially (assuming it's not a breaking change for all other JSON-LD libraries).

At any rate, knowing the right error to through here would help--as I couldn't sort that out from the processing spec.

gkellogg commented 7 months ago

The spec is silent on this. My implementation does generate an error when generating RDF, but it seems to be due to some other failed expectation, and not specifically looking for node objects which contain keywords only expected in value objects. The error is actually coming from the Node Map Generation algorithm, where it expects element to either be an array or a map, and keywords such as @language and direction are neither.

    elsif !element.is_a?(Hash)
      raise "Expected hash or array to create_node_map, got #{element.inspect}"

The algorithm doesn't say what to do if element is neither an array nor a map; my implementation raises an error. Looking at jsonld.js createNodeMap seems to generally just return at the same point.

https://github.com/digitalbazaar/jsonld.js/blob/e2f6523b5c01f8d44a198e21e2b206586db4472e/lib/nodeMap.js#L56-L61