-
Notifications
You must be signed in to change notification settings - Fork 67
Refactoring reference resolving #585
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
Open
coheigea
wants to merge
2
commits into
main
Choose a base branch
from
coheigea/reference-resolving-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
46 changes: 46 additions & 0 deletions
46
src/main/java/org/apache/xml/security/utils/resolver/ResolverUtils.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.xml.security.utils.resolver; | ||
|
|
||
| /** | ||
| * Shared utility methods for resource resolver implementations. | ||
| */ | ||
| public final class ResolverUtils { | ||
|
|
||
| private ResolverUtils() { | ||
| } | ||
|
|
||
| /** | ||
| * Returns {@code true} if {@code uri} does not start with {@code file:}. | ||
| * Returns {@code false} for {@code null}, empty strings, relative URIs (no {@code ':'}), | ||
| * and {@code file:} URIs. | ||
| * | ||
| * @param uri the URI string to test; may be {@code null} | ||
| * @return {@code true} if {@code uri} has a non-{@code file:} scheme | ||
| */ | ||
| public static boolean hasExplicitNonFileScheme(String uri) { | ||
| if (uri == null || uri.isEmpty()) { | ||
| return false; | ||
| } | ||
| if (uri.indexOf(':') <= 0) { | ||
| return false; // no scheme present — relative URI or fragment | ||
| } | ||
| return !uri.startsWith("file:"); | ||
| } | ||
| } |
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
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
140 changes: 140 additions & 0 deletions
140
...est/java/org/apache/xml/security/test/dom/utils/resolver/ResolverLocalFilesystemTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.xml.security.test.dom.utils.resolver; | ||
|
|
||
| import org.apache.xml.security.test.dom.TestUtils; | ||
| import org.apache.xml.security.utils.resolver.ResourceResolverContext; | ||
| import org.apache.xml.security.utils.resolver.implementations.ResolverLocalFilesystem; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.w3c.dom.Attr; | ||
| import org.w3c.dom.Document; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * Tests for the scheme-checking logic in ResolverLocalFilesystem. | ||
| * | ||
| * The fix requires that at least one of uriToResolve / baseUri starts with "file:" | ||
| * AND neither carries a different explicit scheme. This prevents a resolver-hijacking | ||
| * attack where an https: (or ftp:, etc.) uriToResolve was incorrectly accepted solely | ||
| * because the baseUri happened to start with "file:". | ||
| */ | ||
| class ResolverLocalFilesystemTest { | ||
|
|
||
| static { | ||
| org.apache.xml.security.Init.init(); | ||
| } | ||
|
|
||
| private static ResourceResolverContext makeContext(String uri, String baseUri) throws Exception { | ||
| Document doc = TestUtils.newDocument(); | ||
| Attr attr = doc.createAttribute("URI"); | ||
| attr.setValue(uri); | ||
| return new ResourceResolverContext(attr, baseUri, false); | ||
| } | ||
|
|
||
| /** | ||
| * Baseline: a plain file: URI with a file: baseUri is accepted (expected behaviour). | ||
| */ | ||
| @Test | ||
| void testFileUriWithFileBaseUriIsAccepted() throws Exception { | ||
| ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); | ||
| ResourceResolverContext ctx = makeContext("file:///etc/hosts", "file:///var/app/"); | ||
| assertTrue(resolver.engineCanResolveURI(ctx), | ||
| "A file: URI with a file: baseUri should be accepted"); | ||
| } | ||
|
|
||
| /** | ||
| * FIX: an https: uriToResolve must be rejected even when baseUri is "file:". | ||
| * Previously the resolver incorrectly claimed ownership of https: URIs solely | ||
| * because baseUri started with "file:", allowing resolver hijacking. | ||
| */ | ||
| @Test | ||
| void testHttpsUriIsRejectedWhenBaseUriIsFile() throws Exception { | ||
| ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); | ||
| ResourceResolverContext ctx = makeContext("https://attacker.com/payload", "file:///var/app/"); | ||
|
|
||
| assertFalse(resolver.engineCanResolveURI(ctx), | ||
| "FIX VERIFIED: https: uriToResolve must be rejected even with a file: baseUri"); | ||
| } | ||
|
|
||
| /** | ||
| * FIX: any non-file explicit scheme in uriToResolve must be rejected, | ||
| * regardless of baseUri. | ||
| */ | ||
| @Test | ||
| void testFtpUriIsRejectedWhenBaseUriIsFile() throws Exception { | ||
| ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); | ||
| ResourceResolverContext ctx = makeContext("ftp://attacker.com/file.xml", "file:///var/app/"); | ||
|
|
||
| assertFalse(resolver.engineCanResolveURI(ctx), | ||
| "FIX VERIFIED: ftp: uriToResolve must be rejected even with a file: baseUri"); | ||
| } | ||
|
|
||
| /** | ||
| * FIX: http: uriToResolve must also be rejected when baseUri is "file:". | ||
| */ | ||
| @Test | ||
| void testHttpUriIsRejectedWhenBaseUriIsFile() throws Exception { | ||
| ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); | ||
| ResourceResolverContext ctx = makeContext("http://attacker.com/payload", "file:///var/app/"); | ||
|
|
||
| assertFalse(resolver.engineCanResolveURI(ctx), | ||
| "FIX VERIFIED: http: uriToResolve must be rejected even with a file: baseUri"); | ||
| } | ||
|
|
||
| /** | ||
| * A relative uriToResolve (no scheme) combined with a file: baseUri is accepted — | ||
| * this is the primary legitimate use case for providing a file: baseUri. | ||
| */ | ||
| @Test | ||
| void testRelativeUriWithFileBaseUriIsAccepted() throws Exception { | ||
| ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); | ||
| ResourceResolverContext ctx = makeContext("subdoc.xml", "file:///var/app/"); | ||
|
|
||
| assertTrue(resolver.engineCanResolveURI(ctx), | ||
| "A relative uriToResolve with a file: baseUri must be accepted"); | ||
| } | ||
|
|
||
| /** | ||
| * Sanity check: an https: URI with no baseUri (or a non-file: baseUri) is | ||
| * correctly rejected by engineCanResolveURI. | ||
| */ | ||
| @Test | ||
| void testHttpsUriWithNullBaseUriIsRejected() throws Exception { | ||
| ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); | ||
| ResourceResolverContext ctx = makeContext("https://attacker.com/payload", null); | ||
|
|
||
| assertFalse(resolver.engineCanResolveURI(ctx), | ||
| "https: URI with no baseUri should be rejected by the filesystem resolver"); | ||
| } | ||
|
|
||
| /** | ||
| * Sanity check: an https: URI with an https: baseUri is also correctly rejected. | ||
| */ | ||
| @Test | ||
| void testHttpsUriWithHttpsBaseUriIsRejected() throws Exception { | ||
| ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); | ||
| ResourceResolverContext ctx = makeContext("https://attacker.com/payload", "https://victim.com/"); | ||
|
|
||
| assertFalse(resolver.engineCanResolveURI(ctx), | ||
| "https: URI with https: baseUri should be rejected by the filesystem resolver"); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.