|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.services.cloudfront.internal.utils; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | + |
| 21 | +import java.time.Instant; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.ValueSource; |
| 25 | + |
| 26 | +class SigningUtilsTest { |
| 27 | + |
| 28 | + private static final Instant EXPIRATION = Instant.ofEpochSecond(1704067200); |
| 29 | + private static final Instant ACTIVE_DATE = Instant.ofEpochSecond(1640995200); |
| 30 | + private static final String VALID_URL = "https://d111111abcdef8.cloudfront.net/s3ObjectKey"; |
| 31 | + private static final String VALID_IP = "192.168.1.0/24"; |
| 32 | + |
| 33 | + @Test |
| 34 | + void buildCannedPolicy_withValidUrl_producesValidJson() { |
| 35 | + String policy = SigningUtils.buildCannedPolicy(VALID_URL, EXPIRATION); |
| 36 | + |
| 37 | + assertThat(policy).contains("\"Resource\":\"" + VALID_URL + "\""); |
| 38 | + assertThat(policy).contains("\"AWS:EpochTime\":" + EXPIRATION.getEpochSecond()); |
| 39 | + // Verify it's valid JSON structure |
| 40 | + assertThat(policy).startsWith("{"); |
| 41 | + assertThat(policy).endsWith("}"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void buildCustomPolicy_withAllParameters_producesValidJson() { |
| 46 | + String policy = SigningUtils.buildCustomPolicy(VALID_URL, ACTIVE_DATE, EXPIRATION, VALID_IP); |
| 47 | + |
| 48 | + assertThat(policy).contains("\"Resource\":\"" + VALID_URL + "\""); |
| 49 | + assertThat(policy).contains("\"DateLessThan\""); |
| 50 | + assertThat(policy).contains("\"DateGreaterThan\""); |
| 51 | + assertThat(policy).contains("\"IpAddress\""); |
| 52 | + assertThat(policy).contains("\"AWS:SourceIp\":\"" + VALID_IP + "\""); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + @Test |
| 57 | + void buildCannedPolicy_withDoubleQuoteInUrl_shouldRejectInput() { |
| 58 | + String maliciousUrl = "https://example.com/file\",\"Resource\":\"*\",\"x\":\""; |
| 59 | + |
| 60 | + assertThatThrownBy(() -> SigningUtils.buildCannedPolicy(maliciousUrl, EXPIRATION)) |
| 61 | + .isInstanceOf(IllegalArgumentException.class) |
| 62 | + .hasMessageContaining("contains invalid characters") |
| 63 | + .hasMessageContaining("resourceUrl"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void buildCannedPolicy_withBackslashInUrl_shouldRejectInput() { |
| 68 | + String maliciousUrl = "https://example.com/file\\"; |
| 69 | + |
| 70 | + assertThatThrownBy(() -> SigningUtils.buildCannedPolicy(maliciousUrl, EXPIRATION)) |
| 71 | + .isInstanceOf(IllegalArgumentException.class) |
| 72 | + .hasMessageContaining("contains invalid characters"); |
| 73 | + } |
| 74 | + |
| 75 | + @ParameterizedTest |
| 76 | + @ValueSource(strings = { |
| 77 | + "https://example.com/file\u0000", // null character |
| 78 | + "https://example.com/file\n", // newline |
| 79 | + "https://example.com/file\r", // carriage return |
| 80 | + "https://example.com/file\t" // tab |
| 81 | + }) |
| 82 | + void buildCannedPolicy_withControlCharactersInUrl_shouldRejectInput(String maliciousUrl) { |
| 83 | + assertThatThrownBy(() -> SigningUtils.buildCannedPolicy(maliciousUrl, EXPIRATION)) |
| 84 | + .isInstanceOf(IllegalArgumentException.class) |
| 85 | + .hasMessageContaining("contains invalid characters"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void buildCustomPolicy_withDoubleQuoteInUrl_shouldRejectInput() { |
| 90 | + String maliciousUrl = "https://example.com/file\""; |
| 91 | + |
| 92 | + assertThatThrownBy(() -> SigningUtils.buildCustomPolicy(maliciousUrl, ACTIVE_DATE, EXPIRATION, VALID_IP)) |
| 93 | + .isInstanceOf(IllegalArgumentException.class) |
| 94 | + .hasMessageContaining("resourceUrl"); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void buildCustomPolicy_withDoubleQuoteInIpAddress_shouldRejectInput() { |
| 99 | + String maliciousIp = "192.168.1.0\",\"Resource\":\"*"; |
| 100 | + |
| 101 | + assertThatThrownBy(() -> SigningUtils.buildCustomPolicy(VALID_URL, ACTIVE_DATE, EXPIRATION, maliciousIp)) |
| 102 | + .isInstanceOf(IllegalArgumentException.class) |
| 103 | + .hasMessageContaining("ipAddress"); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void buildCustomPolicyForSignedUrl_withDoubleQuoteInUrl_shouldRejectInput() { |
| 108 | + String maliciousUrl = "https://example.com/file\""; |
| 109 | + |
| 110 | + assertThatThrownBy(() -> SigningUtils.buildCustomPolicyForSignedUrl(maliciousUrl, ACTIVE_DATE, EXPIRATION, VALID_IP)) |
| 111 | + .isInstanceOf(IllegalArgumentException.class) |
| 112 | + .hasMessageContaining("contains invalid characters"); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void buildCustomPolicyForSignedUrl_withDoubleQuoteInIpRange_shouldRejectInput() { |
| 117 | + String maliciousIp = "192.168.1.0\""; |
| 118 | + |
| 119 | + assertThatThrownBy(() -> SigningUtils.buildCustomPolicyForSignedUrl(VALID_URL, ACTIVE_DATE, EXPIRATION, maliciousIp)) |
| 120 | + .isInstanceOf(IllegalArgumentException.class) |
| 121 | + .hasMessageContaining("contains invalid characters"); |
| 122 | + } |
| 123 | + |
| 124 | + // Null parameter validation tests |
| 125 | + |
| 126 | + @Test |
| 127 | + void buildCannedPolicy_withNullUrl_shouldThrowNullPointerException() { |
| 128 | + assertThatThrownBy(() -> SigningUtils.buildCannedPolicy(null, EXPIRATION)) |
| 129 | + .isInstanceOf(NullPointerException.class) |
| 130 | + .hasMessageContaining("resourceUrl"); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void buildCannedPolicy_withNullExpiration_shouldThrowNullPointerException() { |
| 135 | + assertThatThrownBy(() -> SigningUtils.buildCannedPolicy(VALID_URL, null)) |
| 136 | + .isInstanceOf(NullPointerException.class) |
| 137 | + .hasMessageContaining("expirationDate"); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void buildCustomPolicy_withNullUrl_shouldThrowNullPointerException() { |
| 142 | + assertThatThrownBy(() -> SigningUtils.buildCustomPolicy(null, ACTIVE_DATE, EXPIRATION, VALID_IP)) |
| 143 | + .isInstanceOf(NullPointerException.class) |
| 144 | + .hasMessageContaining("resourceUrl"); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void buildCustomPolicy_withNullExpiration_shouldThrowNullPointerException() { |
| 149 | + assertThatThrownBy(() -> SigningUtils.buildCustomPolicy(VALID_URL, ACTIVE_DATE, null, VALID_IP)) |
| 150 | + .isInstanceOf(NullPointerException.class) |
| 151 | + .hasMessageContaining("expirationDate"); |
| 152 | + } |
| 153 | + |
| 154 | + // Valid edge cases that should still work |
| 155 | + |
| 156 | + @Test |
| 157 | + void buildCannedPolicy_withWildcard_shouldSucceed() { |
| 158 | + String policy = SigningUtils.buildCannedPolicy("*", EXPIRATION); |
| 159 | + assertThat(policy).contains("\"Resource\":\"*\""); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + void buildCannedPolicy_withWildcardInPath_shouldSucceed() { |
| 164 | + String url = "https://d111111abcdef8.cloudfront.net/*"; |
| 165 | + String policy = SigningUtils.buildCannedPolicy(url, EXPIRATION); |
| 166 | + assertThat(policy).contains("\"Resource\":\"" + url + "\""); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + void buildCannedPolicy_withQueryParameters_shouldSucceed() { |
| 171 | + String url = "https://d111111abcdef8.cloudfront.net/file?param=value&other=123"; |
| 172 | + String policy = SigningUtils.buildCannedPolicy(url, EXPIRATION); |
| 173 | + assertThat(policy).contains("\"Resource\":\"" + url + "\""); |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments