voxpupuli / json-schema

Ruby JSON Schema Validator
MIT License
1.52k stars 241 forks source link

Windows file based schema paths do not trigger `accept_file` for the SchemaReader #439

Open glennsarti opened 4 years ago

glennsarti commented 4 years ago

I am writing a Schema validator that can run on Linux (and Mac) and Windows platforms and I noticed something strange with the Schema Reader.

When I attempted to read a schema from disk on Windows it worked fine, but on Linux, I was getting ReadRefused Errors

Sample code

...
        # The schema should not query external references, except for the meta-schema
        schema_reader = ::JSON::Schema::Reader.new(
          accept_file: false,
          accept_uri:  proc { |uri| uri.host.nil? || ['json-schema.org'].include?(uri.host) },
        )
        @document_schema = schema_reader.read(@schema_file)
...

Where @schema_file is the absolute file path. On Linux for example /tmp/something/ and Windows C:/something

However it seems the logic used to detect URI vs file path is not setup for Windows paths correctly. https://github.com/ruby-json-schema/json-schema/blob/3f07ef0599c9a0e080840bfd1be04df433b7bbff/lib/json-schema/schema/reader.rb#L86

In a simple pry session:

[7] pry(#<JSON::Schema::Reader>)> JSON::Util::URI.parse('C:/something').scheme
=> "C"
[8] pry(#<JSON::Schema::Reader>)> JSON::Util::URI.parse('/tmp/something').scheme
=> nil
[9] pry(#<JSON::Schema::Reader>)>

The Windows paths appear as "URIs" instead of actual files on disk, therefore they never trigger the accept_file gating logic

The documentation for SchemaReader makes no mention of this behavior either.

How are we supposed to pass in Windows, and Linux, paths so that they get parsed correctly?