|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.xml.security.test.dom.utils.resolver; |
| 20 | + |
| 21 | +import org.apache.xml.security.test.dom.TestUtils; |
| 22 | +import org.apache.xml.security.utils.resolver.ResourceResolverContext; |
| 23 | +import org.apache.xml.security.utils.resolver.implementations.ResolverLocalFilesystem; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.w3c.dom.Attr; |
| 26 | +import org.w3c.dom.Document; |
| 27 | + |
| 28 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for the scheme-checking logic in ResolverLocalFilesystem. |
| 33 | + * |
| 34 | + * The fix requires that at least one of uriToResolve / baseUri starts with "file:" |
| 35 | + * AND neither carries a different explicit scheme. This prevents a resolver-hijacking |
| 36 | + * attack where an https: (or ftp:, etc.) uriToResolve was incorrectly accepted solely |
| 37 | + * because the baseUri happened to start with "file:". |
| 38 | + */ |
| 39 | +class ResolverLocalFilesystemTest { |
| 40 | + |
| 41 | + static { |
| 42 | + org.apache.xml.security.Init.init(); |
| 43 | + } |
| 44 | + |
| 45 | + private static ResourceResolverContext makeContext(String uri, String baseUri) throws Exception { |
| 46 | + Document doc = TestUtils.newDocument(); |
| 47 | + Attr attr = doc.createAttribute("URI"); |
| 48 | + attr.setValue(uri); |
| 49 | + return new ResourceResolverContext(attr, baseUri, false); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Baseline: a plain file: URI with a file: baseUri is accepted (expected behaviour). |
| 54 | + */ |
| 55 | + @Test |
| 56 | + void testFileUriWithFileBaseUriIsAccepted() throws Exception { |
| 57 | + ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); |
| 58 | + ResourceResolverContext ctx = makeContext("file:///etc/hosts", "file:///var/app/"); |
| 59 | + assertTrue(resolver.engineCanResolveURI(ctx), |
| 60 | + "A file: URI with a file: baseUri should be accepted"); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * FIX: an https: uriToResolve must be rejected even when baseUri is "file:". |
| 65 | + * Previously the resolver incorrectly claimed ownership of https: URIs solely |
| 66 | + * because baseUri started with "file:", allowing resolver hijacking. |
| 67 | + */ |
| 68 | + @Test |
| 69 | + void testHttpsUriIsRejectedWhenBaseUriIsFile() throws Exception { |
| 70 | + ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); |
| 71 | + ResourceResolverContext ctx = makeContext("https://attacker.com/payload", "file:///var/app/"); |
| 72 | + |
| 73 | + assertFalse(resolver.engineCanResolveURI(ctx), |
| 74 | + "FIX VERIFIED: https: uriToResolve must be rejected even with a file: baseUri"); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * FIX: any non-file explicit scheme in uriToResolve must be rejected, |
| 79 | + * regardless of baseUri. |
| 80 | + */ |
| 81 | + @Test |
| 82 | + void testFtpUriIsRejectedWhenBaseUriIsFile() throws Exception { |
| 83 | + ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); |
| 84 | + ResourceResolverContext ctx = makeContext("ftp://attacker.com/file.xml", "file:///var/app/"); |
| 85 | + |
| 86 | + assertFalse(resolver.engineCanResolveURI(ctx), |
| 87 | + "FIX VERIFIED: ftp: uriToResolve must be rejected even with a file: baseUri"); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * FIX: http: uriToResolve must also be rejected when baseUri is "file:". |
| 92 | + */ |
| 93 | + @Test |
| 94 | + void testHttpUriIsRejectedWhenBaseUriIsFile() throws Exception { |
| 95 | + ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); |
| 96 | + ResourceResolverContext ctx = makeContext("http://attacker.com/payload", "file:///var/app/"); |
| 97 | + |
| 98 | + assertFalse(resolver.engineCanResolveURI(ctx), |
| 99 | + "FIX VERIFIED: http: uriToResolve must be rejected even with a file: baseUri"); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * A relative uriToResolve (no scheme) combined with a file: baseUri is accepted — |
| 104 | + * this is the primary legitimate use case for providing a file: baseUri. |
| 105 | + */ |
| 106 | + @Test |
| 107 | + void testRelativeUriWithFileBaseUriIsAccepted() throws Exception { |
| 108 | + ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); |
| 109 | + ResourceResolverContext ctx = makeContext("subdoc.xml", "file:///var/app/"); |
| 110 | + |
| 111 | + assertTrue(resolver.engineCanResolveURI(ctx), |
| 112 | + "A relative uriToResolve with a file: baseUri must be accepted"); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Sanity check: an https: URI with no baseUri (or a non-file: baseUri) is |
| 117 | + * correctly rejected by engineCanResolveURI. |
| 118 | + */ |
| 119 | + @Test |
| 120 | + void testHttpsUriWithNullBaseUriIsRejected() throws Exception { |
| 121 | + ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); |
| 122 | + ResourceResolverContext ctx = makeContext("https://attacker.com/payload", null); |
| 123 | + |
| 124 | + assertFalse(resolver.engineCanResolveURI(ctx), |
| 125 | + "https: URI with no baseUri should be rejected by the filesystem resolver"); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Sanity check: an https: URI with an https: baseUri is also correctly rejected. |
| 130 | + */ |
| 131 | + @Test |
| 132 | + void testHttpsUriWithHttpsBaseUriIsRejected() throws Exception { |
| 133 | + ResolverLocalFilesystem resolver = new ResolverLocalFilesystem(); |
| 134 | + ResourceResolverContext ctx = makeContext("https://attacker.com/payload", "https://victim.com/"); |
| 135 | + |
| 136 | + assertFalse(resolver.engineCanResolveURI(ctx), |
| 137 | + "https: URI with https: baseUri should be rejected by the filesystem resolver"); |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments