Skip to content

Commit a1b9f76

Browse files
Merge branch 'master' into SNOW-1915375-Drop-copyright-header-in-comments-from-Snowflake-repositories
2 parents 5d63ebf + ef81582 commit a1b9f76

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/main/java/net/snowflake/client/jdbc/SnowflakeFileTransferAgent.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import net.snowflake.client.log.ArgSupplier;
6767
import net.snowflake.client.log.SFLogger;
6868
import net.snowflake.client.log.SFLoggerFactory;
69+
import net.snowflake.client.util.SecretDetector;
6970
import net.snowflake.common.core.FileCompressionType;
7071
import net.snowflake.common.core.RemoteStoreFileEncryptionMaterial;
7172
import net.snowflake.common.core.SqlState;
@@ -1329,7 +1330,8 @@ private static JsonNode parseCommandInGS(SFStatement statement, String command)
13291330
}
13301331

13311332
JsonNode jsonNode = (JsonNode) result;
1332-
logger.debug("Response: {}", jsonNode.toString());
1333+
1334+
logger.debug("Response: {}", SecretDetector.maskSecrets(jsonNode.toString()));
13331335

13341336
SnowflakeUtil.checkErrorAndThrowException(jsonNode);
13351337
return jsonNode;

src/main/java/net/snowflake/client/util/SecretDetector.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public class SecretDetector {
7575
"(token|assertion content)" + "(['\"\\s:=]+)" + "([a-z0-9=/_\\-+]{8,})",
7676
Pattern.CASE_INSENSITIVE);
7777

78+
private static final Pattern ENCRYPTION_MATERIAL_PATTERN =
79+
Pattern.compile("\"encryptionMaterial\"\\s*:\\s*\\{.*?\\}", Pattern.CASE_INSENSITIVE);
80+
7881
// only attempt to find secrets in its leading 100Kb SNOW-30961
7982
private static final int MAX_LENGTH = 100 * 1000;
8083

@@ -218,7 +221,9 @@ public static String maskSASToken(String text) {
218221
public static String maskSecrets(String text) {
219222
return filterAccessTokens(
220223
filterConnectionTokens(
221-
filterPassword(filterSASTokens(filterAWSKeys(filterOAuthTokens(text))))));
224+
filterPassword(
225+
filterSASTokens(
226+
filterAWSKeys(filterOAuthTokens(filterEncryptionMaterial(text)))))));
222227
}
223228

224229
/**
@@ -279,6 +284,23 @@ public static String filterAccessTokens(String message) {
279284
return message;
280285
}
281286

287+
/**
288+
* Filter encryption material that may be buried inside a JSON string.
289+
*
290+
* @param message the message text which may contain encryption material
291+
* @return Return filtered message
292+
*/
293+
public static String filterEncryptionMaterial(String message) {
294+
Matcher matcher =
295+
ENCRYPTION_MATERIAL_PATTERN.matcher(
296+
message.length() <= MAX_LENGTH ? message : message.substring(0, MAX_LENGTH));
297+
298+
if (matcher.find()) {
299+
return matcher.replaceAll("\"encryptionMaterial\" : ****");
300+
}
301+
return message;
302+
}
303+
282304
public static JSONObject maskJsonObject(JSONObject json) {
283305
for (Map.Entry<String, Object> entry : json.entrySet()) {
284306
if (entry.getValue() instanceof String) {

src/test/java/net/snowflake/client/util/SecretDetectorTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,27 @@ public void testMaskJacksonObject() {
421421
"Nested Jackson array node is not masked successfully",
422422
maskedNestedArrayStr.equals(SecretDetector.maskJacksonNode(objNode4).toString()));
423423
}
424+
425+
@Test
426+
public void testEncryptionMaterialFilter() throws Exception {
427+
String messageText =
428+
"{\"data\":"
429+
+ "{\"autoCompress\":true,"
430+
+ "\"overwrite\":false,"
431+
+ "\"clientShowEncryptionParameter\":true,"
432+
+ "\"encryptionMaterial\":{\"queryStageMasterKey\":\"asdfasdfasdfasdf==\",\"queryId\":\"01b6f5ba-0002-0181-0000-11111111da\",\"smkId\":1111},"
433+
+ "\"stageInfo\":{\"locationType\":\"AZURE\", \"region\":\"eastus2\"}";
434+
435+
String filteredMessageText =
436+
"{\"data\":"
437+
+ "{\"autoCompress\":true,"
438+
+ "\"overwrite\":false,"
439+
+ "\"clientShowEncryptionParameter\":true,"
440+
+ "\"encryptionMaterial\" : ****,"
441+
+ "\"stageInfo\":{\"locationType\":\"AZURE\", \"region\":\"eastus2\"}";
442+
443+
String result = SecretDetector.filterEncryptionMaterial(messageText);
444+
445+
assertEquals(filteredMessageText, result);
446+
}
424447
}

0 commit comments

Comments
 (0)