|
| 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 | + |
| 20 | +package org.apache.axis2.deployment; |
| 21 | + |
| 22 | +import org.apache.axis2.description.AxisServiceGroup; |
| 23 | + |
| 24 | +import junit.framework.TestCase; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.FileOutputStream; |
| 28 | +import java.lang.reflect.Method; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import java.util.zip.ZipEntry; |
| 31 | +import java.util.zip.ZipOutputStream; |
| 32 | + |
| 33 | +public class AddAsWebResourcesZipSlipTest extends TestCase { |
| 34 | + |
| 35 | + public void testWwwEntryCannotEscapeWebDirectory() throws Exception { |
| 36 | + File base = File.createTempFile("axis2-zipslip", "dir"); |
| 37 | + assertTrue(base.delete()); |
| 38 | + File webLocation = new File(base, "web"); |
| 39 | + // out = webLocation/svc — the per-service web dir, created from the |
| 40 | + // archive's WWW/ directory entry just as a real deployment would. |
| 41 | + assertTrue(new File(webLocation, "svc").mkdirs()); |
| 42 | + |
| 43 | + // A WWW/../../ entry resolves to base/, outside the web resource |
| 44 | + // directory entirely. |
| 45 | + File escapeTarget = new File(base, "pwned.txt"); |
| 46 | + assertFalse(escapeTarget.exists()); |
| 47 | + |
| 48 | + File archive = new File(base, "evil.aar"); |
| 49 | + try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(archive))) { |
| 50 | + zos.putNextEntry(new ZipEntry("WWW/")); |
| 51 | + zos.closeEntry(); |
| 52 | + zos.putNextEntry(new ZipEntry("WWW/../../pwned.txt")); |
| 53 | + zos.write("owned".getBytes(StandardCharsets.UTF_8)); |
| 54 | + zos.closeEntry(); |
| 55 | + } |
| 56 | + |
| 57 | + String previous = DeploymentEngine.getWebLocationString(); |
| 58 | + DeploymentEngine.setWebLocationString(webLocation.getAbsolutePath()); |
| 59 | + try { |
| 60 | + Method m = DeploymentEngine.class.getDeclaredMethod( |
| 61 | + "addAsWebResources", File.class, String.class, AxisServiceGroup.class); |
| 62 | + m.setAccessible(true); |
| 63 | + m.invoke(null, archive, "svc", new AxisServiceGroup()); |
| 64 | + } finally { |
| 65 | + DeploymentEngine.setWebLocationString(previous); |
| 66 | + } |
| 67 | + |
| 68 | + assertFalse("zip slip: WWW/ entry was written outside the web resource directory", |
| 69 | + escapeTarget.exists()); |
| 70 | + } |
| 71 | +} |
0 commit comments