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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we replace that call with maskSecrets method? we want to replace any possible secrets


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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add this function as well to the chain in maskSecrets method?

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