|
| 1 | +package wrappers; |
| 2 | + |
| 3 | +import dev.aikido.agent_api.context.Context; |
| 4 | +import dev.aikido.agent_api.storage.ServiceConfigStore; |
| 5 | +import org.junit.jupiter.api.AfterEach; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import utils.EmptySampleContextObject; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | + |
| 14 | +public class ProcessBuilderTest { |
| 15 | + @AfterEach |
| 16 | + void cleanup() { |
| 17 | + Context.set(null); |
| 18 | + } |
| 19 | + @BeforeEach |
| 20 | + void beforeEach() { |
| 21 | + cleanup(); |
| 22 | + ServiceConfigStore.updateBlocking(true); |
| 23 | + } |
| 24 | + private void setContextAndLifecycle(String url) { |
| 25 | + Context.set(new EmptySampleContextObject(url)); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testShellInjection() { |
| 30 | + setContextAndLifecycle(" -la"); |
| 31 | + Exception exception1 = assertThrows(RuntimeException.class, () -> { |
| 32 | + new ProcessBuilder("yjytjyjty", "-c", "ls -la").start(); |
| 33 | + }); |
| 34 | + assertEquals("Aikido Zen has blocked Shell Injection", exception1.getMessage()); |
| 35 | + |
| 36 | + cleanup(); |
| 37 | + setContextAndLifecycle("whoami"); |
| 38 | + Exception exception2 = assertThrows(RuntimeException.class, () -> { |
| 39 | + new ProcessBuilder("bash", "-c", "whoami").start(); |
| 40 | + }); |
| 41 | + assertEquals("Aikido Zen has blocked Shell Injection", exception2.getMessage()); |
| 42 | + |
| 43 | + cleanup(); |
| 44 | + assertDoesNotThrow(() -> { |
| 45 | + Runtime.getRuntime().exec("whoami && ls -la"); |
| 46 | + }); |
| 47 | + assertThrows(IllegalArgumentException.class, () -> { |
| 48 | + Runtime.getRuntime().exec(""); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testOnlyScansStrings() { |
| 54 | + setContextAndLifecycle("whoami"); |
| 55 | + assertDoesNotThrow(() -> { |
| 56 | + Runtime.getRuntime().exec(new String[]{"whoami"}); |
| 57 | + }); |
| 58 | + assertDoesNotThrow(() -> { |
| 59 | + Runtime.getRuntime().exec(new String[]{"whoami"}, new String[]{"MyEnvironmentVar=1"}); |
| 60 | + }); |
| 61 | + |
| 62 | + Exception exception1 = assertThrows(RuntimeException.class, () -> { |
| 63 | + Runtime.getRuntime().exec("whoami", new String[]{"MyEnvironmentVar=1"}); |
| 64 | + }); |
| 65 | + assertEquals("Aikido Zen has blocked Shell Injection", exception1.getMessage()); |
| 66 | + } |
| 67 | + |
| 68 | + // --- NEW TEST CASES --- |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testProcessBuilderCommandModification() { |
| 72 | + setContextAndLifecycle("whoami"); |
| 73 | + ProcessBuilder builder = new ProcessBuilder(); |
| 74 | + assertDoesNotThrow(() -> { |
| 75 | + builder.command("whoami"); |
| 76 | + builder.start(); |
| 77 | + }); |
| 78 | + |
| 79 | + Exception exception = assertThrows(RuntimeException.class, () -> { |
| 80 | + builder.command("sh", "-c", "whoami"); |
| 81 | + builder.start(); |
| 82 | + }); |
| 83 | + assertEquals("Aikido Zen has blocked Shell Injection", exception.getMessage()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testProcessBuilderWithDifferentShells() { |
| 88 | + setContextAndLifecycle("whoami"); |
| 89 | + Exception shException = assertThrows(RuntimeException.class, () -> { |
| 90 | + new ProcessBuilder("sh", "-c", "whoami").start(); |
| 91 | + }); |
| 92 | + assertEquals("Aikido Zen has blocked Shell Injection", shException.getMessage()); |
| 93 | + |
| 94 | + Exception bashException = assertThrows(RuntimeException.class, () -> { |
| 95 | + new ProcessBuilder("bash", "-c", "whoami").start(); |
| 96 | + }); |
| 97 | + assertEquals("Aikido Zen has blocked Shell Injection", bashException.getMessage()); |
| 98 | + |
| 99 | + Exception zshException = assertThrows(RuntimeException.class, () -> { |
| 100 | + new ProcessBuilder("zsh", "-c", "whoami").start(); |
| 101 | + }); |
| 102 | + assertEquals("Aikido Zen has blocked Shell Injection", zshException.getMessage()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testProcessBuilderWithDirectCommand() { |
| 107 | + setContextAndLifecycle("whoami"); |
| 108 | + assertDoesNotThrow(() -> { |
| 109 | + new ProcessBuilder("whoami").start(); |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testProcessBuilderWithArguments() { |
| 115 | + setContextAndLifecycle("whoami"); |
| 116 | + assertDoesNotThrow(() -> { |
| 117 | + new ProcessBuilder("ls", "-l", "/tmp").start(); |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testProcessBuilderWithEnvironment() { |
| 123 | + setContextAndLifecycle("whoami"); |
| 124 | + ProcessBuilder builder = new ProcessBuilder("whoami"); |
| 125 | + builder.environment().put("MY_VAR", "1"); |
| 126 | + assertDoesNotThrow(() -> { |
| 127 | + builder.start(); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testProcessBuilderWithShellInjectionInCommand() { |
| 133 | + setContextAndLifecycle("whoami; ls"); |
| 134 | + Exception exception = assertThrows(RuntimeException.class, () -> { |
| 135 | + new ProcessBuilder("sh", "-c", "whoami; ls").start(); |
| 136 | + }); |
| 137 | + assertEquals("Aikido Zen has blocked Shell Injection", exception.getMessage()); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testProcessBuilderWithComplexShellCommand() { |
| 142 | + setContextAndLifecycle("whoami && ls -la"); |
| 143 | + Exception exception = assertThrows(RuntimeException.class, () -> { |
| 144 | + new ProcessBuilder("bash", "-c", "whoami && ls -la").start(); |
| 145 | + }); |
| 146 | + assertEquals("Aikido Zen has blocked Shell Injection", exception.getMessage()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testProcessBuilderWithSafeCommand() { |
| 151 | + setContextAndLifecycle("whoami"); |
| 152 | + assertDoesNotThrow(() -> { |
| 153 | + new ProcessBuilder("whoami").start(); |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testProcessBuilderWithEmptyCommand() { |
| 159 | + assertThrows(IndexOutOfBoundsException.class, () -> { |
| 160 | + new ProcessBuilder().start(); |
| 161 | + }); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void testProcessBuilderWithNullCommand() { |
| 166 | + assertThrows(NullPointerException.class, () -> { |
| 167 | + new ProcessBuilder((String[]) null).start(); |
| 168 | + }); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void testProcessBuilderWithCommandModificationAfterStart() { |
| 173 | + setContextAndLifecycle("whoami"); |
| 174 | + ProcessBuilder builder = new ProcessBuilder("whoami"); |
| 175 | + assertDoesNotThrow(() -> { |
| 176 | + builder.start(); |
| 177 | + }); |
| 178 | + // Modifying command after start should not affect previous process |
| 179 | + builder.command("sh", "-c", "whoami"); |
| 180 | + Exception exception = assertThrows(RuntimeException.class, () -> { |
| 181 | + builder.start(); |
| 182 | + }); |
| 183 | + assertEquals("Aikido Zen has blocked Shell Injection", exception.getMessage()); |
| 184 | + } |
| 185 | +} |
0 commit comments