Skip to content

fix: don't resolve local files from remote schemas - #439

Closed
bardrop wants to merge 2 commits into
APIDevTools:mainfrom
bardrop-oss:fix/block-local-files-from-remote-schemas
Closed

bardrop wants to merge 2 commits into
APIDevTools:mainfrom
bardrop-oss:fix/block-local-files-from-remote-schemas

Conversation

@bardrop

@bardrop bardrop commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #433.

Problem. A schema loaded from an http(s) URL could read any local file through $ref. Two ways, both with the default safeUrlResolver: true:

  • an absolute $ref: "file:///etc/..."
  • a file:// $id plus a relative $ref (the $ref resolves against the $id)

The same happens when the root schema object is passed in with an https:// path (the first repro in the issue).

Change. parse() now also gets the URL of the document that holds the $ref (referrer). If that document is http(s) and the target is a filesystem path, it throws a ResolverError ("Cannot resolve local file ... from remote schema ...") before any resolver runs. The check uses the referring document, not the resolution base, so the $id trick is covered too. Both resolve paths pass the referrer: resolve-external.ts and resolve-external/with-matcher.ts (resolveExcludedPathMatcher). With continueOnError the error is collected like any other ResolverError.

Local schemas are not affected: local → local and local → remote work as before. A remote document reached from a local one still can't pull local files.

This is a behavior change for anyone who loads a remote schema on purpose and wants it to reference local files. I did not add an option for that. If you want one (for example resolve.file.allowFromRemote), I can add it.

One case is left as is: if a local schema already loaded a file, a remote schema that refers to the same file gets the cached value. That data is already in the user's output, so it does not leak anything new. I can move the check before the cache lookup if you prefer.

Tests. New test/specs/remote-local-file/remote-local-file.spec.ts (Node only; remote files are served from memory by a custom resolver, no network):

  • file:// $ref with an https:// root path
  • file:// $ref in a schema downloaded over https
  • relative $ref under a file:// $id (default and resolveExcludedPathMatcher)
  • $ref to a remote schema's own file:// $id, whose subschema then refers to a local file
  • remote → remote → local file
  • the same with resolveExcludedPathMatcher
  • continueOnError reports a ResolverError at the right path
  • local → local still works

The first 8 fail on main (the secret file is read) and pass now. Full vitest run (752 passed), pnpm lint, pnpm typecheck and oxfmt --check pass. docs/options.md has one new paragraph.

A schema loaded from an http(s) URL could read any local file with
$ref: "file:///...", or with a relative $ref under a file:// $id.
This worked even with safeUrlResolver on.

parse() now gets the URL of the document that holds the $ref and
throws a ResolverError when an http(s) document points to a
filesystem path. Both the default and the resolveExcludedPathMatcher
resolve paths pass it.

Fixes #433
@bardrop

bardrop commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Friendly ping when you have time. Is there anything you'd like changed here?

@jonluca

jonluca commented Nov 24, 2026

Copy link
Copy Markdown
Member

Thanks for the fix and the tests. We fixed this on our local main branch in a26cfb9. It checks the referring document’s retrieval URL before cache lookup and preserves references to embedded schema IDs. Closing this PR in favor of that commit.

@jonluca jonluca closed this Nov 24, 2026
@bardrop

bardrop commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for fixing it, checking before the cache lookup makes sense.

jonluca added a commit that referenced this pull request Nov 24, 2026
Use the physical document origin for each reference and check access before reusing a cached load. Keep canonical identifiers usable for embedded resources and attach grouped errors to their referring properties.

Refs #433, #439
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remote HTTP(S) schemas can dereference arbitrary local file:// URLs under default safeUrlResolver

2 participants