Skip to content

Commit 3710ed5

Browse files
mo-automoabu
andauthored
fix(jans-scim): address security scan findings (#15622)
* fix(jans-scim): address security scan findings Part of #15571. Co-authored-by: Mohammad Abudayyeh <47318409+moabu@users.noreply.github.com> Signed-off-by: moauto <54212639+mo-auto@users.noreply.github.com> * revert(jans-scim): restore ListResponseProvider Change was readability-only, out of scope for security fixes. Co-authored-by: Mohammad Abudayyeh <47318409+moabu@users.noreply.github.com> Signed-off-by: moauto <54212639+mo-auto@users.noreply.github.com> * fix(jans-scim): address review on scim fixes - UpdatedUsers: drop search script hooks (non-spec endpoint must not call scripts); also omit mobile and trusted device attributes - deleteUser: snapshot memberOf before the delete hook so group cleanup uses the original membership - reject non-object cnf claims without parsing them - client: send Accept-Encoding identity since decompression is off Co-authored-by: Mohammad Abudayyeh <47318409+moabu@users.noreply.github.com> Signed-off-by: moauto <54212639+mo-auto@users.noreply.github.com> * fix(jans-scim): omit jansAuthenticator from UpdatedUsers OTP scripts keep HOTP/TOTP keys there as well as in jansExtUid. Co-authored-by: Mohammad Abudayyeh <47318409+moabu@users.noreply.github.com> Signed-off-by: moauto <54212639+mo-auto@users.noreply.github.com> * revert(jans-scim): restore AbstractScimClient Maintainer considers the compression hardening unnecessary. Co-authored-by: Mohammad Abudayyeh <47318409+moabu@users.noreply.github.com> Signed-off-by: moauto <54212639+mo-auto@users.noreply.github.com> * revert(jans-scim): restore Fido2DeviceService logging Co-authored-by: Mohammad Abudayyeh <47318409+moabu@users.noreply.github.com> Signed-off-by: moauto <54212639+mo-auto@users.noreply.github.com> * fix(jans-scim): lowercase secret attrs with ROOT Co-authored-by: Mohammad Abudayyeh <47318409+moabu@users.noreply.github.com> Signed-off-by: moauto <54212639+mo-auto@users.noreply.github.com> * fix(jans-scim): omit CIBA device token from UpdatedUsers Co-authored-by: Mohammad Abudayyeh <47318409+moabu@users.noreply.github.com> Signed-off-by: moauto <54212639+mo-auto@users.noreply.github.com> * fix(jans-scim): reject lexer errors, hide CIBA user code Co-authored-by: Mohammad Abudayyeh <47318409+moabu@users.noreply.github.com> Signed-off-by: moauto <54212639+mo-auto@users.noreply.github.com> --------- Signed-off-by: moauto <54212639+mo-auto@users.noreply.github.com> Co-authored-by: Mohammad Abudayyeh <47318409+moabu@users.noreply.github.com>
1 parent 7aae783 commit 3710ed5

14 files changed

Lines changed: 120 additions & 19 deletions

File tree

‎jans-scim/client/src/main/java/io/jans/scim2/client/ScimClient.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private void updateTokens() throws Exception {
147147
} finally {
148148
response.close();
149149
}
150-
logger.debug("Got token: " + access_token);
150+
logger.debug("Got access token");
151151

152152
}
153153

‎jans-scim/model/src/main/java/io/jans/scim/model/scim2/util/ScimResourceUtil.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,13 @@ private static void deleteCustomAttribute(Map<String, Object> source, String pat
233233
Map<String, Object> submap=IntrospectUtil.strObjMap(source.get(path));
234234
//get method above could have returned null (no extended attributes for source object)
235235
if (submap != null) {
236-
submap.put(key, null);
236+
//Only registered fields of the extension can be removed
237+
String field = ext.getFields().keySet().stream().filter(key::equalsIgnoreCase).findFirst().orElse(null);
238+
if (field == null) {
239+
log.warn("Attribute {} is not part of extension {}", key, path);
240+
} else {
241+
submap.put(field, null);
242+
}
237243
}
238244
}
239245

‎jans-scim/server/src/main/java/io/jans/scim/service/antlr/scimFilter/FilterListener.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ public String getError() {
148148
public Filter getFilter() {
149149
if (StringUtils.isEmpty(error)) {
150150
Filter f = filter.poll();
151-
log.info("LDAP filter expression computed was {}", Optional.ofNullable(f).map(Filter::toString).orElse(null));
151+
if (log.isInfoEnabled()) {
152+
log.info("LDAP filter expression computed was {}",
153+
Optional.ofNullable(f).map(Filter::toString).map(str -> str.replaceAll("[\\r\\n\\t]", "_")).orElse(null));
154+
}
152155
return f;
153156
}
154157
return null;

‎jans-scim/server/src/main/java/io/jans/scim/service/antlr/scimFilter/ScimFilterParserService.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.antlr.v4.runtime.ANTLRInputStream;
1919
import org.antlr.v4.runtime.CommonTokenStream;
20+
import org.antlr.v4.runtime.Token;
2021
import org.antlr.v4.runtime.tree.ParseTree;
2122
import org.antlr.v4.runtime.tree.ParseTreeWalker;
2223
import org.apache.commons.lang3.StringUtils;
@@ -60,18 +61,25 @@ public boolean isLdapBackend() {
6061
return ldapBackend;
6162
}
6263

63-
private ParseTree getParseTree(String filter, ScimFilterErrorListener errorListener){
64+
private ParseTree getParseTree(String filter, ScimFilterErrorListener errorListener) throws SCIMException {
6465

6566
ANTLRInputStream input = new ANTLRInputStream(filter);
6667
ScimFilterLexer lexer = new ScimFilterLexer(input);
68+
lexer.removeErrorListeners();
69+
lexer.addErrorListener(errorListener);
6770
CommonTokenStream tokens = new CommonTokenStream(lexer);
6871

6972
ScimFilterParser parser = new ScimFilterParser(tokens);
7073
parser.setTrimParseTree(true);
7174
parser.removeErrorListeners();
7275
parser.addErrorListener(errorListener);
7376

74-
return parser.filter();
77+
ParseTree tree = parser.filter();
78+
if (tokens.LA(1) != Token.EOF) {
79+
throw new SCIMException(String.format("Error parsing filter (symbol='%s'; message='unexpected trailing input')",
80+
tokens.LT(1).getText()));
81+
}
82+
return tree;
7583
}
7684

7785
public ParseTree getParseTree(String filter) throws Exception {

‎jans-scim/server/src/main/java/io/jans/scim/service/scim2/ExternalConstraintsService.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.jans.orm.PersistenceEntryManager;
1414
import io.jans.scim.model.conf.AppConfiguration;
1515
import io.jans.scim.model.conf.ScimMode;
16+
import io.jans.scim.model.scim2.ErrorScimType;
1617
import io.jans.scim.model.scim2.SearchRequest;
1718
import io.jans.scim.service.external.ExternalScimService;
1819
import io.jans.scim.service.external.OperationContext;
@@ -66,6 +67,9 @@ public Response applySearchCheck(SearchRequest searchReq, HttpHeaders httpHeader
6667
if (!StringUtils.isEmpty(filterPrepend)) {
6768
if (StringUtils.isEmpty(searchReq.getFilter())) {
6869
searchReq.setFilter(filterPrepend);
70+
} else if (!hasBalancedGrouping(searchReq.getFilter())) {
71+
return BaseScimWebService.getErrorResponse(Status.BAD_REQUEST, ErrorScimType.INVALID_FILTER,
72+
"Unbalanced grouping in filter");
6973
} else {
7074
searchReq.setFilter(String.format("%s and (%s)", filterPrepend, searchReq.getFilter()));
7175
}
@@ -105,6 +109,44 @@ private OperationContext makeContext(HttpHeaders httpHeaders, UriInfo uriInfo,
105109

106110
}
107111

112+
/**
113+
* Checks parentheses and brackets outside string literals are balanced, so the caller's filter
114+
* cannot close the group it is wrapped in and escape the mandatory prepended restriction
115+
*/
116+
static boolean hasBalancedGrouping(String filter) {
117+
118+
int parens = 0;
119+
int brackets = 0;
120+
boolean inString = false;
121+
boolean escaped = false;
122+
for (int i = 0; i < filter.length(); i++) {
123+
char c = filter.charAt(i);
124+
if (inString) {
125+
if (escaped) {
126+
escaped = false;
127+
} else if (c == '\\') {
128+
escaped = true;
129+
} else if (c == '"') {
130+
inString = false;
131+
}
132+
continue;
133+
}
134+
switch (c) {
135+
case '"': inString = true; break;
136+
case '(': parens++; break;
137+
case ')': parens--; break;
138+
case '[': brackets++; break;
139+
case ']': brackets--; break;
140+
default: break;
141+
}
142+
if (parens < 0 || brackets < 0) {
143+
return false;
144+
}
145+
}
146+
return !inString && parens == 0 && brackets == 0;
147+
148+
}
149+
108150
private TokenDetails getDatabaseToken(String token) {
109151

110152
String hashedToken = token.startsWith("{sha256Hex}") ? token : DigestUtils.sha256Hex(token);

‎jans-scim/server/src/main/java/io/jans/scim/service/scim2/Scim2GroupService.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public PagedResult<BaseScimResource> searchGroups(String filter, String sortBy,
387387

388388
Filter ldapFilter = scimFilterParserService.createFilter(filter, Filter.createPresenceFilter("inum"), GroupResource.class);
389389
log.info("Executing search for groups using: ldapfilter '{}', sortBy '{}', sortOrder '{}', startIndex '{}', count '{}'",
390-
ldapFilter.toString(), sortBy, sortOrder.getValue(), startIndex, count);
390+
ldapFilter.toString().replaceAll("[\\r\\n\\t]", "_"), sortBy == null ? null : sortBy.replaceAll("[\\r\\n\\t]", "_"), sortOrder.getValue(), startIndex, count);
391391

392392
PagedResult<JansGroup> list = ldapEntryManager.findPagedEntries(groupService.getDnForGroup(null),
393393
JansGroup.class, ldapFilter, null, sortBy, sortOrder, startIndex - 1, count, maxCount);

‎jans-scim/server/src/main/java/io/jans/scim/service/scim2/Scim2PatchService.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public BaseScimResource applyPatchOperation(BaseScimResource resource, PatchOper
142142
}
143143
}
144144

145-
log.debug("applyPatchOperation. Generating a ScimResource from generic map: {}", genericMap.toString());
145+
log.debug("applyPatchOperation. Generating a ScimResource from generic map with keys: {}", genericMap.keySet());
146146
}
147147

148148
//Try parse genericMap as an instance of the resource

‎jans-scim/server/src/main/java/io/jans/scim/service/scim2/Scim2UserService.java‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,7 @@ public void replacePersonInfo(ScimCustomPerson gluuPerson, UserResource user, St
562562

563563
public void deleteUser(ScimCustomPerson gluuPerson) throws Exception {
564564

565-
if (gluuPerson.getMemberOf() != null && gluuPerson.getMemberOf().size() > 0) {
566-
log.info("Removing user {} from groups", gluuPerson.getUid());
567-
userPersistenceHelper.removeUserFromGroups(gluuPerson);
568-
}
569-
log.info("Removing user entry {}", gluuPerson.getDn());
570-
565+
List<String> groups = gluuPerson.getMemberOf() == null ? null : new ArrayList<>(gluuPerson.getMemberOf());
571566
if (externalScimService.isEnabled()) {
572567
boolean result = externalScimService.executeScimDeleteUserMethods(gluuPerson);
573568
if (!result) {
@@ -576,6 +571,12 @@ public void deleteUser(ScimCustomPerson gluuPerson) throws Exception {
576571
}
577572
}
578573

574+
if (groups != null && !groups.isEmpty()) {
575+
log.info("Removing user {} from groups", gluuPerson.getUid());
576+
userPersistenceHelper.removeUserFromGroups(gluuPerson, groups);
577+
}
578+
log.info("Removing user entry {}", gluuPerson.getDn());
579+
579580
userPersistenceHelper.removePerson(gluuPerson);
580581

581582
if (externalScimService.isEnabled())
@@ -588,7 +589,7 @@ public PagedResult<BaseScimResource> searchUsers(String filter, String sortBy, S
588589

589590
Filter ldapFilter = scimFilterParserService.createFilter(filter, Filter.createPresenceFilter("inum"), UserResource.class);
590591
log.info("Executing search for users using: ldapfilter '{}', sortBy '{}', sortOrder '{}', startIndex '{}', count '{}'",
591-
ldapFilter.toString(), sortBy, sortOrder.getValue(), startIndex, count);
592+
ldapFilter.toString().replaceAll("[\\r\\n\\t]", "_"), sortBy == null ? null : sortBy.replaceAll("[\\r\\n\\t]", "_"), sortOrder.getValue(), startIndex, count);
592593

593594
PagedResult<ScimCustomPerson> list = ldapEntryManager.findPagedEntries(personService.getDnForPerson(null),
594595
ScimCustomPerson.class, ldapFilter, null, sortBy, sortOrder, startIndex - 1, count, maxCount);

‎jans-scim/server/src/main/java/io/jans/scim/service/scim2/UserPersistenceHelper.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ public void updatePerson(ScimCustomPerson person) {
107107
* @param person The person in question
108108
* @throws Exception
109109
*/
110-
public void removeUserFromGroups(ScimCustomPerson person) {
110+
public void removeUserFromGroups(ScimCustomPerson person, List<String> groups) {
111111

112112
String dn = person.getDn();
113-
List<String> groups = person.getMemberOf();
114113

115114
for (String oneGroup : groups) {
116115
try {

‎jans-scim/server/src/main/java/io/jans/scim/service/scim2/serialization/ScimResourceSerializer.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.LinkedHashMap;
77
import java.util.List;
88
import java.util.Map;
9+
import java.util.Objects;
910
import java.util.Set;
1011
import java.util.SortedSet;
1112
import java.util.TreeSet;
@@ -98,6 +99,8 @@ private void buildIncludeSet(SortedSet<String> include, Class<? extends BaseScim
9899
extendedSet.addAll(IntrospectUtil.allAttrs.get(resourceClass));
99100
tempSet = expandAttributesPaths(attributes, defaultSchema, schemas, extendedSet);
100101
tempSet.removeAll(neverSet);
102+
//A requested sub-path would otherwise make containsProperty select its never-returned parent
103+
tempSet.removeIf(path -> neverSet.stream().anyMatch(never -> path.startsWith(never + ".")));
101104
include.addAll(tempSet);
102105

103106
} else if (exclussions != null) {
@@ -189,6 +192,7 @@ private void traverse(String prefix, Map<String, Object> map, LinkedHashMap<Stri
189192

190193
public String serialize(BaseScimResource resource, String attributes, String exclusions) throws Exception {
191194

195+
Objects.requireNonNull(resource, "resource");
192196
SortedSet<String> include = new TreeSet<>();
193197
Class<? extends BaseScimResource> resourceClass = resource.getClass();
194198
buildIncludeSet(include, resourceClass, new ArrayList<>(resource.getSchemas()), attributes, exclusions);

0 commit comments

Comments
 (0)