BytesLogParam implements LogParam { StringBuilder filter(String value, Set<String> maskedFields) { var builder = new StringBuilder(value); for (String maskedField : maskedFields) { int current = -1; while (true) { current = builder.indexOf('\"' + maskedField + '\"', current); if (current < 0) break; int[] range = maskRange(builder, current + maskedField.length() + 2); if (range == null) break; builder.replace(range[0], range[1], "******"); current = range[1]; } } return builder; } BytesLogParam(byte[] bytes);  BytesLogParam(byte[] bytes, Charset charset); @Override void append(StringBuilder builder, Set<String> maskedFields, int maxParamLength);  }
@Test void filterJSONWithoutMask() { var param = new BytesLogParam(null, null); var value = "{\"field1\": \"value1\"}"; assertThat(param.filter(value, Set.of()).toString()).isEqualTo(value); }
@Test void filterJSONWithOneMaskField() { var param = new BytesLogParam(null, null); var value = "{\"field1\": \"value1\",\n \"password\" : \"pass123\",\n \"field2\": \"value2\"\n}"; assertThat(param.filter(value, Set.of("password", "passwordConfirm")).toString()) .isEqualTo("{\"field1\": \"value1\",\n \"password\" : \"******\",\n \"field2\": \"value2\"\n}"); value = "{\"field1\": \"value1\", \"password\": null, \"field2\": null, \"field3\": null}"; assertThat(param.filter(value, Set.of("password", "passwordConfirm")).toString()) .isEqualTo(value); value = "{\"field1\": \"value1\", \"password\": {\"field2\": null}}"; assertThat(param.filter(value, Set.of("password", "passwordConfirm")).toString()) .isEqualTo(value); value = "{\"field1\": [\"secret1\", \"secret2\", \"secret3\"], \"field2\": \"value\"}"; assertThat(param.filter(value, Set.of("secret1", "secret2", "secret3")).toString()) .isEqualTo(value); }
@Test void filterJSONWithMultipleMaskFields() { var param = new BytesLogParam(null, null); var value = "{\"field1\": \"value1\", \"password\": \"pass123\", \"passwordConfirm\": \"pass123\", \"field2\": \"value2\", \"nested\": {\"password\": \"pass\\\"123\", \"passwordConfirm\": \"pass123\"}}"; assertThat(param.filter(value, Set.of("password", "passwordConfirm")).toString()) .isEqualTo("{\"field1\": \"value1\", \"password\": \"******\", \"passwordConfirm\": \"******\", \"field2\": \"value2\", \"nested\": {\"password\": \"******\", \"passwordConfirm\": \"******\"}}"); }
@Test void filterWithBrokenJSON() { var param = new BytesLogParam(null, null); var value = "{\"field1\": \"value1\",\n \"password\": \"pass123\",\n \"passwordConfirm\": \"pass12"; assertThat(param.filter(value, Set.of("password", "passwordConfirm"))).doesNotContain("pass123"); value = "{\"field1\": \"value1\",\n \"password\": \"pass123\",\n \"passwordConfirm\""; assertThat(param.filter(value, Set.of("password", "passwordConfirm"))).doesNotContain("pass123"); value = "{\"field1\": \"value1\", \"password\": \"pass123"; assertThat(param.filter(value, Set.of("password", "passwordConfirm")).toString()) .isEqualTo("{\"field1\": \"value1\", \"password\": \"******"); }