Repository navigation
Conversation
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 APIDevTools#433
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #433.
Problem. A schema loaded from an
http(s)URL could read any local file through$ref. Two ways, both with the defaultsafeUrlResolver: true:$ref: "file:///etc/..."file://$idplus a relative$ref(the$refresolves 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 ishttp(s)and the target is a filesystem path, it throws aResolverError("Cannot resolve local file ... from remote schema ...") before any resolver runs. The check uses the referring document, not the resolution base, so the$idtrick is covered too. Both resolve paths pass the referrer:resolve-external.tsandresolve-external/with-matcher.ts(resolveExcludedPathMatcher). WithcontinueOnErrorthe error is collected like any otherResolverError.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://$refwith anhttps://root pathfile://$refin a schema downloaded over https$refunder afile://$id(default andresolveExcludedPathMatcher)$refto a remote schema's ownfile://$id, whose subschema then refers to a local fileresolveExcludedPathMatchercontinueOnErrorreports aResolverErrorat the right pathThe first 8 fail on
main(the secret file is read) and pass now. Fullvitest run(752 passed),pnpm lint,pnpm typecheckandoxfmt --checkpass.docs/options.mdhas one new paragraph.