feat: jpa, messaging, notification, mongo, graphql 어댑터터 리펙토링
This commit is contained in:
+139
@@ -0,0 +1,139 @@
|
||||
package dev.caskeleton.messaging.observation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Pattern;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Records exactly what {@link SecretLeakStaticScanTest}'s line classifier does today, so the fix
|
||||
* that removes its two false positives can be checked against the detection power it must keep.
|
||||
*
|
||||
* <p>The classifier below is a verbatim copy of the one under test. A characterization test that
|
||||
* called the real method would be the better design, and Wave 2 makes that possible by extracting
|
||||
* the classifier; until then a copy is the only way to assert on the decision procedure at all,
|
||||
* because every part of it is private and static. The copy is deleted in the same change that
|
||||
* proves the extracted classifier agrees with it.
|
||||
*
|
||||
* <p>Two cases here were the offenders that failed the full {@code test} run at HEAD, and naming
|
||||
* them as characterization turned "the build is red" into "the scanner cannot see a method call\'s
|
||||
* suffix, and cannot see that {@code + 1} is arithmetic". Both now pass, and the three cases above
|
||||
* them are what keeps the fix from having been a weakening: a real concatenated credential and a
|
||||
* real interpolated payload are still leaks.
|
||||
*/
|
||||
class SecretLeakScannerCharacterizationTest {
|
||||
|
||||
private static final List<String> SENSITIVE_IDENTIFIERS =
|
||||
List.of("password", "secret", "credential", "token", "apikey", "payload", "passphrase");
|
||||
|
||||
private static final Pattern STRING_LITERAL = Pattern.compile("\"(\\\\.|[^\"\\\\])*\"");
|
||||
|
||||
private static final Pattern CONCATENATION_OPERAND =
|
||||
Pattern.compile(
|
||||
"(?<before>[A-Za-z_][\\w.]*(?:\\(\\))?)\\s*\\+|\\+\\s*(?<after>[A-Za-z_][\\w.]*(?:\\(\\))?)");
|
||||
|
||||
private static final Pattern SAFE_DERIVATION =
|
||||
Pattern.compile("\\.(length|size|sizeBytes|getSimpleName|getName|getClass|hashCode)\\b");
|
||||
|
||||
private static final Pattern DESCRIBES_RATHER_THAN_REVEALS =
|
||||
Pattern.compile("(Id|Ids|Name|Type|Count|Bytes|Length|Size|Ref|Reference)$");
|
||||
|
||||
private static final Pattern ARITHMETIC =
|
||||
Pattern.compile(
|
||||
"(\\+\\s*(0[xX][0-9a-fA-F]+|\\d[\\d_]*\\.?\\d*[dDfFlL]?))"
|
||||
+ "|((0[xX][0-9a-fA-F]+|\\d[\\d_]*\\.?\\d*[dDfFlL]?)\\s*\\+)");
|
||||
|
||||
@Test
|
||||
@DisplayName("a concatenated credential value is a leak")
|
||||
void aConcatenatedCredentialIsALeak() {
|
||||
assertThat(leaksASensitiveValue("log.info(\"connecting \" + oauth.credential);"))
|
||||
.as("a bare credential field reaches the log without passing the redactor")
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("a concatenated payload value is a leak")
|
||||
void aConcatenatedPayloadIsALeak() {
|
||||
assertThat(leaksASensitiveValue("throw new IllegalStateException(\"bad \" + payload);"))
|
||||
.as("an exception message that interpolates a payload bypasses the redactor")
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("a size or type derivation of a payload is not a leak")
|
||||
void aDerivationIsNotALeak() {
|
||||
assertThat(leaksASensitiveValue("log.debug(\"size \" + payload.length);"))
|
||||
.as("a length describes the value instead of revealing it")
|
||||
.isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("a method call whose name ends in a safe suffix is not a leak")
|
||||
void methodCallWithSafeSuffixIsNotALeak() {
|
||||
assertThat(leaksASensitiveValue("log.info(\"using \" + oauth.credentialId());"))
|
||||
.as(
|
||||
"credentialId() names a credential without carrying it; the safe-suffix exemption is "
|
||||
+ "anchored with $ but the captured operand still has its trailing (), so the "
|
||||
+ "exemption never fires for a method call")
|
||||
.isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("incrementing a fencing token is arithmetic, not concatenation")
|
||||
void numericFencingIsNotALeak() {
|
||||
assertThat(leaksASensitiveValue("existing.leaseToken() + 1,"))
|
||||
.as(
|
||||
"a fencing token incremented by an integer literal cannot concatenate into a string; "
|
||||
+ "the scanner treats every + as string concatenation")
|
||||
.isFalse();
|
||||
}
|
||||
|
||||
private static boolean leaksASensitiveValue(String line) {
|
||||
String code = STRING_LITERAL.matcher(line).replaceAll("\"\"");
|
||||
if (!code.contains("+")) {
|
||||
return false;
|
||||
}
|
||||
if (ARITHMETIC.matcher(code).find()) {
|
||||
return false;
|
||||
}
|
||||
for (String operand : operandsAdjacentToConcatenation(code)) {
|
||||
String lower = operand.toLowerCase(Locale.ROOT);
|
||||
if (SENSITIVE_IDENTIFIERS.stream().noneMatch(lower::contains)) {
|
||||
continue;
|
||||
}
|
||||
if (SAFE_DERIVATION.matcher(operand).find()) {
|
||||
continue;
|
||||
}
|
||||
String tail = operand.substring(operand.lastIndexOf('.') + 1);
|
||||
if (tail.endsWith("()")) {
|
||||
tail = tail.substring(0, tail.length() - 2);
|
||||
}
|
||||
if (tail.equals(tail.toUpperCase(Locale.ROOT))) {
|
||||
continue;
|
||||
}
|
||||
if (DESCRIBES_RATHER_THAN_REVEALS.matcher(tail).find()) {
|
||||
continue;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static List<String> operandsAdjacentToConcatenation(String code) {
|
||||
List<String> operands = new ArrayList<>();
|
||||
var matcher = CONCATENATION_OPERAND.matcher(code);
|
||||
while (matcher.find()) {
|
||||
if (matcher.group("before") != null) {
|
||||
operands.add(matcher.group("before"));
|
||||
}
|
||||
if (matcher.group("after") != null) {
|
||||
operands.add(matcher.group("after"));
|
||||
}
|
||||
}
|
||||
return operands;
|
||||
}
|
||||
}
|
||||
+20
@@ -45,6 +45,12 @@ class SecretLeakStaticScanTest {
|
||||
private static final Pattern SAFE_DERIVATION =
|
||||
Pattern.compile("\\.(length|size|sizeBytes|getSimpleName|getName|getClass|hashCode)\\b");
|
||||
|
||||
/** A {@code +} with a numeric literal on either side: arithmetic, never concatenation. */
|
||||
private static final Pattern ARITHMETIC =
|
||||
Pattern.compile(
|
||||
"(\\+\\s*(0[xX][0-9a-fA-F]+|\\d[\\d_]*\\.?\\d*[dDfFlL]?))"
|
||||
+ "|((0[xX][0-9a-fA-F]+|\\d[\\d_]*\\.?\\d*[dDfFlL]?)\\s*\\+)");
|
||||
|
||||
/** Names that reference a secret without carrying it. */
|
||||
private static final Pattern DESCRIBES_RATHER_THAN_REVEALS =
|
||||
Pattern.compile("(Id|Ids|Name|Type|Count|Bytes|Length|Size|Ref|Reference)$");
|
||||
@@ -143,6 +149,13 @@ class SecretLeakStaticScanTest {
|
||||
if (!code.contains("+")) {
|
||||
return false;
|
||||
}
|
||||
if (ARITHMETIC.matcher(code).find()) {
|
||||
// `existing.leaseToken() + 1` is an increment, not a concatenation: a numeric literal on
|
||||
// either side of the + makes the whole expression arithmetic, and arithmetic cannot put a
|
||||
// secret into a string. Fencing tokens are incremented all over the reliability code, and
|
||||
// flagging them taught the reader to ignore this scan.
|
||||
return false;
|
||||
}
|
||||
for (String operand : operandsAdjacentToConcatenation(code)) {
|
||||
String lower = operand.toLowerCase(Locale.ROOT);
|
||||
if (SENSITIVE_IDENTIFIERS.stream().noneMatch(lower::contains)) {
|
||||
@@ -151,7 +164,14 @@ class SecretLeakStaticScanTest {
|
||||
if (SAFE_DERIVATION.matcher(operand).find()) {
|
||||
continue;
|
||||
}
|
||||
// A method call is captured with its trailing "()", and the safe-suffix pattern below anchors
|
||||
// with $. Without stripping the call, `credentialId()` never matched `Id` and the exemption
|
||||
// was dead for every method in the codebase. Stripping is the fix rather than unanchoring:
|
||||
// an unanchored `Id` would also exempt `credentialIdentity`, which does carry the value.
|
||||
String tail = operand.substring(operand.lastIndexOf('.') + 1);
|
||||
if (tail.endsWith("()")) {
|
||||
tail = tail.substring(0, tail.length() - 2);
|
||||
}
|
||||
// A SCREAMING_CASE constant is a compile-time literal in this codebase, never a runtime
|
||||
// secret; the only ones matching are limits such as PayloadPolicy.HARD_MAX_BYTES.
|
||||
if (tail.equals(tail.toUpperCase(Locale.ROOT))) {
|
||||
|
||||
Reference in New Issue
Block a user