|
| 1 | +/* |
| 2 | + * Copyright 2025 Code Intelligence GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example; |
| 18 | + |
| 19 | +import com.code_intelligence.jazzer.api.BugDetectors; |
| 20 | +import com.code_intelligence.jazzer.api.SilentCloseable; |
| 21 | +import com.code_intelligence.jazzer.junit.FuzzTest; |
| 22 | +import java.io.FileInputStream; |
| 23 | +import java.io.FileNotFoundException; |
| 24 | +import java.io.IOException; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.nio.file.Paths; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | + |
| 29 | +public class FilePathTraversalPass { |
| 30 | + @BeforeEach |
| 31 | + public void setUp() { |
| 32 | + BugDetectors.setFilePathTraversalTarget(() -> Paths.get("../../hello")); |
| 33 | + } |
| 34 | + |
| 35 | + @FuzzTest |
| 36 | + void beforeEachWorks(boolean ignore) throws Exception { |
| 37 | + try (FileInputStream fis = new FileInputStream("test")) { |
| 38 | + fis.read(); |
| 39 | + } catch (FileNotFoundException ignored) { |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @FuzzTest |
| 44 | + void overwritingBeforeEachWorks(boolean ignore) { |
| 45 | + try (SilentCloseable unused = |
| 46 | + BugDetectors.setFilePathTraversalTarget(() -> Paths.get("../../jazzer-hey"))) { |
| 47 | + try (FileInputStream fis = new FileInputStream("../../hello")) { |
| 48 | + fis.read(); |
| 49 | + } catch (NullPointerException | IOException ignored) { |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @FuzzTest |
| 55 | + void allow(boolean ignore) { |
| 56 | + try (SilentCloseable unused = |
| 57 | + BugDetectors.setFilePathTraversalAllowPath((Path p) -> p.toString().contains("secret"))) { |
| 58 | + try (FileInputStream fis = new FileInputStream("my-secret-file")) { |
| 59 | + fis.read(); |
| 60 | + } catch (NullPointerException | IOException ignored) { |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @FuzzTest |
| 66 | + void targetMissed(boolean ignore) { |
| 67 | + try (SilentCloseable ignored = |
| 68 | + BugDetectors.setFilePathTraversalAllowPath((Path ignoredAgain) -> true)) { |
| 69 | + try (FileInputStream fis = new FileInputStream("../../hello-world")) { |
| 70 | + fis.read(); |
| 71 | + } catch (NullPointerException | IOException ignoredEvenMore) { |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments