Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1660591 Remove sensitive encryption info when logging #2077

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import net.snowflake.client.log.ArgSupplier;
import net.snowflake.client.log.SFLogger;
import net.snowflake.client.log.SFLoggerFactory;
import net.snowflake.client.util.SecretDetector;
import net.snowflake.common.core.FileCompressionType;
import net.snowflake.common.core.RemoteStoreFileEncryptionMaterial;
import net.snowflake.common.core.SqlState;
Expand Down Expand Up @@ -1337,7 +1338,8 @@ private static JsonNode parseCommandInGS(SFStatement statement, String command)
}

JsonNode jsonNode = (JsonNode) result;
logger.debug("Response: {}", jsonNode.toString());

logger.debug("Response: {}", SecretDetector.filterEncryptionMaterial(jsonNode.toString()));

SnowflakeUtil.checkErrorAndThrowException(jsonNode);
return jsonNode;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/snowflake/client/util/SecretDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public class SecretDetector {
"(token|assertion content)" + "(['\"\\s:=]+)" + "([a-z0-9=/_\\-+]{8,})",
Pattern.CASE_INSENSITIVE);

private static final Pattern ENCRYPTION_MATERIAL_PATTERN =
Pattern.compile("\"encryptionMaterial\"\\s*:\\s*\\{.*?\\}", Pattern.CASE_INSENSITIVE);

// only attempt to find secrets in its leading 100Kb SNOW-30961
private static final int MAX_LENGTH = 100 * 1000;

Expand Down Expand Up @@ -283,6 +286,23 @@ public static String filterAccessTokens(String message) {
return message;
}

/**
* Filter encryption material that may be buried inside a JSON string.
*
* @param message the message text which may contain encryption material
* @return Return filtered message
*/
public static String filterEncryptionMaterial(String message) {
Matcher matcher =
ENCRYPTION_MATERIAL_PATTERN.matcher(
message.length() <= MAX_LENGTH ? message : message.substring(0, MAX_LENGTH));

if (matcher.find()) {
return matcher.replaceAll("\"encryptionMaterial\" : ****");
}
return message;
}

public static JSONObject maskJsonObject(JSONObject json) {
for (Map.Entry<String, Object> entry : json.entrySet()) {
if (entry.getValue() instanceof String) {
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/net/snowflake/client/util/SecretDetectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,30 @@ public void testMaskJacksonObject() {
"Nested Jackson array node is not masked successfully",
maskedNestedArrayStr.equals(SecretDetector.maskJacksonNode(objNode4).toString()));
}

/*

*/
@Test
public void testEncryptionMaterialFilter() throws Exception {
String messageText =
"{\"data\":"
+ "{\"autoCompress\":true,"
+ "\"overwrite\":false,"
+ "\"clientShowEncryptionParameter\":true,"
+ "\"encryptionMaterial\":{\"queryStageMasterKey\":\"asdfasdfasdfasdf==\",\"queryId\":\"01b6f5ba-0002-0181-0000-11111111da\",\"smkId\":1111},"
+ "\"stageInfo\":{\"locationType\":\"AZURE\", \"region\":\"eastus2\"}";

String filteredMessageText =
"{\"data\":"
+ "{\"autoCompress\":true,"
+ "\"overwrite\":false,"
+ "\"clientShowEncryptionParameter\":true,"
+ "\"encryptionMaterial\" : ****,"
+ "\"stageInfo\":{\"locationType\":\"AZURE\", \"region\":\"eastus2\"}";

String result = SecretDetector.filterEncryptionMaterial(messageText);

assertEquals(filteredMessageText, result);
}
}
Loading