Skip to content

Commit 8392a7e

Browse files
committed
Merge pull request #40493 from onobc
* pr/40493: Encode JSON string in Pulsar auth params Closes gh-40493
2 parents d340f87 + 7a7bcd0 commit 8392a7e

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapper.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,25 @@ private String getAuthenticationParamsJson(Map<String, String> params) {
8989
try {
9090
return sortedParams.entrySet()
9191
.stream()
92-
.map((entry) -> "\"%s\":\"%s\"".formatted(entry.getKey(), entry.getValue()))
92+
.map((entry) -> "\"%s\":\"%s\"".formatted(entry.getKey(), escapeJson(entry.getValue())))
9393
.collect(Collectors.joining(",", "{", "}"));
9494
}
9595
catch (Exception ex) {
9696
throw new IllegalStateException("Could not convert auth parameters to encoded string", ex);
9797
}
9898
}
9999

100+
private String escapeJson(String raw) {
101+
return raw.replace("\\", "\\\\")
102+
.replace("\"", "\\\"")
103+
.replace("/", "\\/")
104+
.replace("\b", "\\b")
105+
.replace("\t", "\\t")
106+
.replace("\n", "\\n")
107+
.replace("\f", "\\f")
108+
.replace("\r", "\\r");
109+
}
110+
100111
<T> void customizeProducerBuilder(ProducerBuilder<T> producerBuilder) {
101112
PulsarProperties.Producer properties = this.properties.getProducer();
102113
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapperTests.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ void customizeClientBuilderWhenHasNoAuthentication() {
7272
@Test
7373
void customizeClientBuilderWhenHasAuthentication() throws UnsupportedAuthenticationException {
7474
PulsarProperties properties = new PulsarProperties();
75-
Map<String, String> params = Map.of("param", "name");
76-
String authParamString = "{\"param\":\"name\"}";
75+
Map<String, String> params = Map.of("simpleParam", "foo", "complexParam",
76+
"{\n\t\"k1\" : \"v1\",\n\t\"k2\":\"v2\"\n}");
77+
String authParamString = "{\"complexParam\":\"{\\n\\t\\\"k1\\\" : \\\"v1\\\",\\n\\t\\\"k2\\\":\\\"v2\\\"\\n}\""
78+
+ ",\"simpleParam\":\"foo\"}";
7779
properties.getClient().getAuthentication().setPluginClassName("myclass");
7880
properties.getClient().getAuthentication().setParam(params);
7981
ClientBuilder builder = mock(ClientBuilder.class);
@@ -112,8 +114,10 @@ void customizeAdminBuilderWhenHasNoAuthentication() {
112114
@Test
113115
void customizeAdminBuilderWhenHasAuthentication() throws UnsupportedAuthenticationException {
114116
PulsarProperties properties = new PulsarProperties();
115-
Map<String, String> params = Map.of("param", "name");
116-
String authParamString = "{\"param\":\"name\"}";
117+
Map<String, String> params = Map.of("simpleParam", "foo", "complexParam",
118+
"{\n\t\"k1\" : \"v1\",\n\t\"k2\":\"v2\"\n}");
119+
String authParamString = "{\"complexParam\":\"{\\n\\t\\\"k1\\\" : \\\"v1\\\",\\n\\t\\\"k2\\\":\\\"v2\\\"\\n}\""
120+
+ ",\"simpleParam\":\"foo\"}";
117121
properties.getAdmin().getAuthentication().setPluginClassName("myclass");
118122
properties.getAdmin().getAuthentication().setParam(params);
119123
PulsarAdminBuilder builder = mock(PulsarAdminBuilder.class);

0 commit comments

Comments
 (0)