Skip to content

Commit 8cf36b2

Browse files
authored
Escape pipe character for injected users (#5175) (#5192)
Signed-off-by: shikharj05 <[email protected]>
1 parent 62b390c commit 8cf36b2

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

src/main/java/org/opensearch/security/privileges/PrivilegesEvaluatorImpl.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111

112112
import static org.opensearch.security.OpenSearchSecurityPlugin.traceAction;
113113
import static org.opensearch.security.support.ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT;
114+
import static org.opensearch.security.support.SecurityUtils.escapePipe;
114115

115116
public class PrivilegesEvaluatorImpl implements PrivilegesEvaluator {
116117

@@ -275,12 +276,14 @@ public boolean isInitialized() {
275276
private void setUserInfoInThreadContext(User user) {
276277
if (threadContext.getTransient(OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT) == null) {
277278
StringJoiner joiner = new StringJoiner("|");
278-
joiner.add(user.getName());
279-
joiner.add(String.join(",", user.getRoles()));
280-
joiner.add(String.join(",", user.getSecurityRoles()));
279+
// Escape any pipe characters in the values before joining
280+
joiner.add(escapePipe(user.getName()));
281+
joiner.add(escapePipe(String.join(",", user.getRoles())));
282+
joiner.add(escapePipe(String.join(",", user.getSecurityRoles())));
283+
281284
String requestedTenant = user.getRequestedTenant();
282285
if (!Strings.isNullOrEmpty(requestedTenant)) {
283-
joiner.add(requestedTenant);
286+
joiner.add(escapePipe(requestedTenant));
284287
}
285288
threadContext.putTransient(OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT, joiner.toString());
286289
}

src/main/java/org/opensearch/security/privileges/legacy/PrivilegesEvaluatorImpl.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109

110110
import static org.opensearch.security.OpenSearchSecurityPlugin.traceAction;
111111
import static org.opensearch.security.support.ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT;
112+
import static org.opensearch.security.support.SecurityUtils.escapePipe;
112113

113114
public class PrivilegesEvaluatorImpl implements PrivilegesEvaluator {
114115

@@ -245,12 +246,14 @@ public boolean isInitialized() {
245246
private void setUserInfoInThreadContext(User user) {
246247
if (threadContext.getTransient(OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT) == null) {
247248
StringJoiner joiner = new StringJoiner("|");
248-
joiner.add(user.getName());
249-
joiner.add(String.join(",", user.getRoles()));
250-
joiner.add(String.join(",", user.getSecurityRoles()));
249+
// Escape any pipe characters in the values before joining
250+
joiner.add(escapePipe(user.getName()));
251+
joiner.add(escapePipe(String.join(",", user.getRoles())));
252+
joiner.add(escapePipe(String.join(",", user.getSecurityRoles())));
253+
251254
String requestedTenant = user.getRequestedTenant();
252255
if (!Strings.isNullOrEmpty(requestedTenant)) {
253-
joiner.add(requestedTenant);
256+
joiner.add(escapePipe(requestedTenant));
254257
}
255258
threadContext.putTransient(OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT, joiner.toString());
256259
}

src/main/java/org/opensearch/security/support/SecurityUtils.java

+8
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,12 @@ private static String resolveEnvVar(String envVarName, String mode, boolean bc,
161161
return bc ? Hasher.hash(envVarValue.toCharArray(), settings) : envVarValue;
162162
}
163163
}
164+
165+
// Helper method to escape pipe characters
166+
public static String escapePipe(String input) {
167+
if (input == null) {
168+
return "";
169+
}
170+
return input.replace("|", "\\|");
171+
}
164172
}

src/test/java/org/opensearch/security/support/SecurityUtilsTest.java

+31
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,35 @@ private void checkKeysWithPredicate(Collection<String> keys, String predicateNam
7070
);
7171
});
7272
}
73+
74+
@Test
75+
public void testEscapePipe_NullInput() {
76+
assertThat("Null input should return empty string", SecurityUtils.escapePipe(null), equalTo(""));
77+
}
78+
79+
@Test
80+
public void testEscapePipe_EmptyString() {
81+
assertThat("Empty string should return empty string", SecurityUtils.escapePipe(""), equalTo(""));
82+
}
83+
84+
@Test
85+
public void testEscapePipe_StringWithoutPipe() {
86+
assertThat("String without pipe should remain unchanged", SecurityUtils.escapePipe("normal string"), equalTo("normal string"));
87+
}
88+
89+
@Test
90+
public void testEscapePipe_SinglePipe() {
91+
assertThat("Single pipe should be escaped", SecurityUtils.escapePipe("before|after"), equalTo("before\\|after"));
92+
}
93+
94+
@Test
95+
public void testEscapePipe_MultiplePipes() {
96+
assertThat("Multiple pipes should all be escaped", SecurityUtils.escapePipe("a|b|c"), equalTo("a\\|b\\|c"));
97+
}
98+
99+
@Test
100+
public void testEscapePipe_MultiplePipesConsecutive() {
101+
assertThat("Consecutive pipes should all be escaped", SecurityUtils.escapePipe("|||"), equalTo("\\|\\|\\|"));
102+
}
103+
73104
}

0 commit comments

Comments
 (0)