|
| 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.stax.resourceResolvers; |
| 20 | + |
| 21 | +import org.apache.xml.security.stax.impl.resourceResolvers.ResolverFilesystem; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 27 | + |
| 28 | +/** |
| 29 | + * Tests for the scheme-checking logic in ResolverFilesystem. |
| 30 | + * |
| 31 | + * The fix requires that at least one of uri / baseURI starts with "file:" |
| 32 | + * AND neither carries a different explicit scheme. This prevents a live-SSRF |
| 33 | + * attack where an https: (or http:, ftp:, etc.) uri was incorrectly accepted |
| 34 | + * solely because baseURI happened to start with "file:". |
| 35 | + */ |
| 36 | +class ResolverFilesystemTest { |
| 37 | + |
| 38 | + static { |
| 39 | + org.apache.xml.security.Init.init(); |
| 40 | + } |
| 41 | + |
| 42 | + // ------------------------------------------------------------------------- |
| 43 | + // canResolve() — scheme bypass tests |
| 44 | + // ------------------------------------------------------------------------- |
| 45 | + |
| 46 | + /** |
| 47 | + * Baseline: a plain file: URI with a file: baseURI is accepted (expected). |
| 48 | + */ |
| 49 | + @Test |
| 50 | + void testFileUriWithFileBaseUriIsAccepted() { |
| 51 | + ResolverFilesystem resolver = new ResolverFilesystem(); |
| 52 | + assertNotNull(resolver.canResolve("file:///etc/hosts", "file:///var/app/"), |
| 53 | + "A file: URI with a file: baseURI should be accepted"); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * FIX: https: uri must be rejected even when baseURI is file:. |
| 58 | + */ |
| 59 | + @Test |
| 60 | + void testHttpsUriIsRejectedWhenBaseUriIsFile() { |
| 61 | + ResolverFilesystem resolver = new ResolverFilesystem(); |
| 62 | + assertNull(resolver.canResolve("https://attacker.com/payload", "file:///var/app/"), |
| 63 | + "FIX VERIFIED: https: uri must be rejected even with a file: baseURI"); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * FIX: http: uri must also be rejected when baseURI is file:. |
| 68 | + */ |
| 69 | + @Test |
| 70 | + void testHttpUriIsRejectedWhenBaseUriIsFile() { |
| 71 | + ResolverFilesystem resolver = new ResolverFilesystem(); |
| 72 | + assertNull(resolver.canResolve("http://attacker.com/payload", "file:///var/app/"), |
| 73 | + "FIX VERIFIED: http: uri must be rejected even with a file: baseURI"); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * FIX: ftp: and other non-file schemes must also be rejected. |
| 78 | + */ |
| 79 | + @Test |
| 80 | + void testFtpUriIsRejectedWhenBaseUriIsFile() { |
| 81 | + ResolverFilesystem resolver = new ResolverFilesystem(); |
| 82 | + assertNull(resolver.canResolve("ftp://attacker.com/file.xml", "file:///var/app/"), |
| 83 | + "FIX VERIFIED: ftp: uri must be rejected even with a file: baseURI"); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * A relative uri (no scheme) combined with a file: baseURI is accepted — |
| 88 | + * this is the primary legitimate use case. |
| 89 | + */ |
| 90 | + @Test |
| 91 | + void testRelativeUriWithFileBaseUriIsAccepted() { |
| 92 | + ResolverFilesystem resolver = new ResolverFilesystem(); |
| 93 | + assertNotNull(resolver.canResolve("subdoc.xml", "file:///var/app/"), |
| 94 | + "A relative uri with a file: baseURI must be accepted"); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * VULN: URI.resolve() returns an absolute uri unchanged, so the final URL |
| 99 | + * opened by getInputStreamFromExternalReference() will be the attacker's |
| 100 | + * https: URL — a live SSRF. Demonstrate that resolve() does not anchor |
| 101 | + * the result under the file: base. |
| 102 | + */ |
| 103 | + @Test |
| 104 | + void testUriResolveLeavesAbsoluteUriUnchanged() throws Exception { |
| 105 | + java.net.URI base = new java.net.URI("file:///var/app/"); |
| 106 | + java.net.URI absolute = new java.net.URI("https://attacker.com/payload"); |
| 107 | + |
| 108 | + java.net.URI resolved = base.resolve(absolute); |
| 109 | + |
| 110 | + assertFalse("file".equals(resolved.getScheme()), |
| 111 | + "VULN CONFIRMED: resolved URI scheme is '" + resolved.getScheme() |
| 112 | + + "', not 'file' — toURL().openStream() will make an outbound HTTPS request"); |
| 113 | + } |
| 114 | + |
| 115 | + // ------------------------------------------------------------------------- |
| 116 | + // Sanity checks — cases that should correctly be rejected |
| 117 | + // ------------------------------------------------------------------------- |
| 118 | + |
| 119 | + /** |
| 120 | + * An https: URI with no baseURI is correctly rejected. |
| 121 | + */ |
| 122 | + @Test |
| 123 | + void testHttpsUriWithNullBaseUriIsRejected() { |
| 124 | + ResolverFilesystem resolver = new ResolverFilesystem(); |
| 125 | + assertNull(resolver.canResolve("https://attacker.com/payload", null), |
| 126 | + "https: URI with null baseURI should not be accepted"); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * An https: URI with an https: baseURI is correctly rejected. |
| 131 | + */ |
| 132 | + @Test |
| 133 | + void testHttpsUriWithHttpsBaseUriIsRejected() { |
| 134 | + ResolverFilesystem resolver = new ResolverFilesystem(); |
| 135 | + assertNull(resolver.canResolve("https://attacker.com/payload", "https://victim.com/"), |
| 136 | + "https: URI with https: baseURI should not be accepted"); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * A null URI is correctly rejected. |
| 141 | + */ |
| 142 | + @Test |
| 143 | + void testNullUriIsRejected() { |
| 144 | + ResolverFilesystem resolver = new ResolverFilesystem(); |
| 145 | + assertNull(resolver.canResolve(null, "file:///var/app/"), |
| 146 | + "null URI should always be rejected"); |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments