|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
| 3 | + */ |
| 4 | +package com.marklogic.client.datamovement.functionaltests; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.AfterEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import java.lang.reflect.InvocationTargetException; |
| 10 | +import java.lang.reflect.Method; |
| 11 | +import java.util.concurrent.atomic.AtomicInteger; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 15 | + |
| 16 | +public class ServerStartStopSecurityTest { |
| 17 | + |
| 18 | + @AfterEach |
| 19 | + void resetProcessStarters() { |
| 20 | + WBFailover.resetProcessStarterForTest(); |
| 21 | + QBFailover.resetProcessStarterForTest(); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void wbServerStartStopRejectsSuspiciousHostnameBeforeProcessStart() { |
| 26 | + AtomicInteger processStartCount = new AtomicInteger(0); |
| 27 | + WBFailover.setProcessStarterForTest((server, commandToRun) -> { |
| 28 | + processStartCount.incrementAndGet(); |
| 29 | + throw new AssertionError("Process creation should not be attempted for invalid hostname"); |
| 30 | + }); |
| 31 | + |
| 32 | + assertThrows(IllegalArgumentException.class, |
| 33 | + () -> invokeServerStartStop(new WBFailover(), "host; echo pwned", "start")); |
| 34 | + assertEquals(0, processStartCount.get()); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void wbServerStartStopRejectsInvalidCommandBeforeProcessStart() { |
| 39 | + AtomicInteger processStartCount = new AtomicInteger(0); |
| 40 | + WBFailover.setProcessStarterForTest((server, commandToRun) -> { |
| 41 | + processStartCount.incrementAndGet(); |
| 42 | + throw new AssertionError("Process creation should not be attempted for invalid command"); |
| 43 | + }); |
| 44 | + |
| 45 | + assertThrows(IllegalArgumentException.class, |
| 46 | + () -> invokeServerStartStop(new WBFailover(), "host", "reboot")); |
| 47 | + assertEquals(0, processStartCount.get()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void qbServerStartStopRejectsSuspiciousHostnameBeforeProcessStart() { |
| 52 | + AtomicInteger processStartCount = new AtomicInteger(0); |
| 53 | + QBFailover.setProcessStarterForTest((server, commandToRun) -> { |
| 54 | + processStartCount.incrementAndGet(); |
| 55 | + throw new AssertionError("Process creation should not be attempted for invalid hostname"); |
| 56 | + }); |
| 57 | + |
| 58 | + assertThrows(IllegalArgumentException.class, |
| 59 | + () -> invokeServerStartStop(new QBFailover(), "host; echo pwned", "start")); |
| 60 | + assertEquals(0, processStartCount.get()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void qbServerStartStopRejectsInvalidCommandBeforeProcessStart() { |
| 65 | + AtomicInteger processStartCount = new AtomicInteger(0); |
| 66 | + QBFailover.setProcessStarterForTest((server, commandToRun) -> { |
| 67 | + processStartCount.incrementAndGet(); |
| 68 | + throw new AssertionError("Process creation should not be attempted for invalid command"); |
| 69 | + }); |
| 70 | + |
| 71 | + assertThrows(IllegalArgumentException.class, |
| 72 | + () -> invokeServerStartStop(new QBFailover(), "host", "reboot")); |
| 73 | + assertEquals(0, processStartCount.get()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void wbServerStartStopRejectsLeadingDashHostnameBeforeProcessStart() { |
| 78 | + AtomicInteger processStartCount = new AtomicInteger(0); |
| 79 | + WBFailover.setProcessStarterForTest((server, commandToRun) -> { |
| 80 | + processStartCount.incrementAndGet(); |
| 81 | + throw new AssertionError("Process creation should not be attempted for leading-dash hostname"); |
| 82 | + }); |
| 83 | + |
| 84 | + assertThrows(IllegalArgumentException.class, |
| 85 | + () -> invokeServerStartStop(new WBFailover(), "-oProxyCommand=id", "start")); |
| 86 | + assertEquals(0, processStartCount.get()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void qbServerStartStopRejectsLeadingDashHostnameBeforeProcessStart() { |
| 91 | + AtomicInteger processStartCount = new AtomicInteger(0); |
| 92 | + QBFailover.setProcessStarterForTest((server, commandToRun) -> { |
| 93 | + processStartCount.incrementAndGet(); |
| 94 | + throw new AssertionError("Process creation should not be attempted for leading-dash hostname"); |
| 95 | + }); |
| 96 | + |
| 97 | + assertThrows(IllegalArgumentException.class, |
| 98 | + () -> invokeServerStartStop(new QBFailover(), "-oProxyCommand=id", "start")); |
| 99 | + assertEquals(0, processStartCount.get()); |
| 100 | + } |
| 101 | + |
| 102 | + private void invokeServerStartStop(Object target, String server, String command) throws Throwable { |
| 103 | + Method method = target.getClass().getDeclaredMethod("serverStartStop", String.class, String.class); |
| 104 | + method.setAccessible(true); |
| 105 | + try { |
| 106 | + method.invoke(target, server, command); |
| 107 | + } catch (InvocationTargetException ex) { |
| 108 | + throw ex.getCause(); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments