|
| 1 | +/* |
| 2 | + * Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com) |
| 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 | + * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd (info@arcadedata.com) |
| 17 | + * SPDX-License-Identifier: Apache-2.0 |
| 18 | + */ |
| 19 | +package com.arcadedb.query.opencypher; |
| 20 | + |
| 21 | +import com.arcadedb.database.Database; |
| 22 | +import com.arcadedb.database.DatabaseFactory; |
| 23 | +import com.arcadedb.query.sql.executor.ResultSet; |
| 24 | +import org.junit.jupiter.api.AfterEach; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import static org.junit.jupiter.api.Assertions.*; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for security boundary conditions in Cypher functions. |
| 32 | + */ |
| 33 | +public class CypherFunctionSecurityTest { |
| 34 | + private Database database; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + public void setup() { |
| 38 | + database = new DatabaseFactory("./databases/test-function-security").create(); |
| 39 | + } |
| 40 | + |
| 41 | + @AfterEach |
| 42 | + public void teardown() { |
| 43 | + if (database != null) { |
| 44 | + database.drop(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testUtilSleepMaxDuration() { |
| 50 | + // Test that sleep duration is limited to prevent DoS |
| 51 | + final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { |
| 52 | + database.query("opencypher", "RETURN util.sleep(999999999999) AS result"); |
| 53 | + }); |
| 54 | + assertTrue(exception.getMessage().contains("Sleep duration exceeds maximum allowed")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testUtilSleepValidDuration() { |
| 59 | + // Test that valid sleep durations work (e.g., 100ms) |
| 60 | + final ResultSet resultSet = database.query("opencypher", "RETURN util.sleep(100) AS result"); |
| 61 | + assertTrue(resultSet.hasNext()); |
| 62 | + assertNull(resultSet.next().getProperty("result")); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testUtilSleepNegativeDuration() { |
| 67 | + // Test that negative durations are handled gracefully |
| 68 | + final ResultSet resultSet = database.query("opencypher", "RETURN util.sleep(-100) AS result"); |
| 69 | + assertTrue(resultSet.hasNext()); |
| 70 | + assertNull(resultSet.next().getProperty("result")); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testUtilSleepZeroDuration() { |
| 75 | + // Test that zero duration is handled |
| 76 | + final ResultSet resultSet = database.query("opencypher", "RETURN util.sleep(0) AS result"); |
| 77 | + assertTrue(resultSet.hasNext()); |
| 78 | + assertNull(resultSet.next().getProperty("result")); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testUtilCompressInputSizeLimit() { |
| 83 | + // Test that compression has input size limits to prevent DoS |
| 84 | + // Create a string larger than max allowed size (11MB exceeds 10MB limit) |
| 85 | + final int SIZE = 11 * 1024 * 1024; |
| 86 | + final char[] largeChars = new char[SIZE]; |
| 87 | + java.util.Arrays.fill(largeChars, 'x'); |
| 88 | + final String largeString = new String(largeChars); |
| 89 | + |
| 90 | + final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { |
| 91 | + database.query("opencypher", "RETURN util.compress($data) AS result", "data", largeString); |
| 92 | + }); |
| 93 | + assertTrue(exception.getMessage().contains("Input size exceeds maximum allowed")); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testUtilCompressValidSize() { |
| 98 | + // Test that valid compression works |
| 99 | + final ResultSet resultSet = database.query("opencypher", "RETURN util.compress('Hello World') AS result"); |
| 100 | + assertTrue(resultSet.hasNext()); |
| 101 | + assertNotNull(resultSet.next().getProperty("result")); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testUtilDecompressOutputSizeLimit() { |
| 106 | + // Test that decompression has output size limits to prevent zip bomb attacks |
| 107 | + // First, compress a small string |
| 108 | + final ResultSet compressResult = database.query("opencypher", "RETURN util.compress('test') AS compressed"); |
| 109 | + final String compressed = compressResult.next().getProperty("compressed").toString(); |
| 110 | + |
| 111 | + // For now, just verify decompression works with valid data |
| 112 | + final ResultSet decompressResult = database.query("opencypher", |
| 113 | + "RETURN util.decompress('" + compressed + "') AS result"); |
| 114 | + assertTrue(decompressResult.hasNext()); |
| 115 | + assertEquals("test", decompressResult.next().getProperty("result")); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testTextLpadMaxLength() { |
| 120 | + // Test that lpad has length limits to prevent excessive memory allocation |
| 121 | + final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { |
| 122 | + database.query("opencypher", "RETURN text.lpad('x', 999999999, ' ') AS result"); |
| 123 | + }); |
| 124 | + assertTrue(exception.getMessage().contains("length exceeds maximum allowed") || |
| 125 | + exception.getMessage().contains("Invalid length")); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testTextLpadNegativeLength() { |
| 130 | + // Test that negative lengths are rejected |
| 131 | + final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { |
| 132 | + database.query("opencypher", "RETURN text.lpad('x', -100, ' ') AS result"); |
| 133 | + }); |
| 134 | + assertTrue(exception.getMessage().contains("Invalid length") || |
| 135 | + exception.getMessage().contains("negative")); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testTextLpadValidLength() { |
| 140 | + // Test that valid padding works |
| 141 | + final ResultSet resultSet = database.query("opencypher", "RETURN text.lpad('x', 5, ' ') AS result"); |
| 142 | + assertTrue(resultSet.hasNext()); |
| 143 | + assertEquals(" x", resultSet.next().getProperty("result")); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testTextRpadMaxLength() { |
| 148 | + // Test that rpad has length limits to prevent excessive memory allocation |
| 149 | + final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { |
| 150 | + database.query("opencypher", "RETURN text.rpad('x', 999999999, ' ') AS result"); |
| 151 | + }); |
| 152 | + assertTrue(exception.getMessage().contains("length exceeds maximum allowed") || |
| 153 | + exception.getMessage().contains("Invalid length")); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testTextRpadNegativeLength() { |
| 158 | + // Test that negative lengths are rejected |
| 159 | + final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { |
| 160 | + database.query("opencypher", "RETURN text.rpad('x', -100, ' ') AS result"); |
| 161 | + }); |
| 162 | + assertTrue(exception.getMessage().contains("Invalid length") || |
| 163 | + exception.getMessage().contains("negative")); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void testTextRpadValidLength() { |
| 168 | + // Test that valid padding works |
| 169 | + final ResultSet resultSet = database.query("opencypher", "RETURN text.rpad('x', 5, ' ') AS result"); |
| 170 | + assertTrue(resultSet.hasNext()); |
| 171 | + assertEquals("x ", resultSet.next().getProperty("result")); |
| 172 | + } |
| 173 | +} |
0 commit comments