jkevan commented on code in PR #405:
URL: https://github.com/apache/unomi/pull/405#discussion_r872127931
##########
itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java:
##########
@@ -385,6 +385,7 @@ public void testCreateEventWithProfileId_Success() throws
IOException, Interrupt
//Act
HttpPost request = new HttpPost(URL + CONTEXT_URL);
+ setBasicAuth(request, "karaf", "karaf");
Review Comment:
Auth is not required here, the /cxs/context.json request is public
##########
itests/src/test/java/org/apache/unomi/itests/BaseIT.java:
##########
@@ -442,10 +442,20 @@ public String getFullUrl(String url) throws Exception {
return BASE_URL + ":" + HTTP_PORT + url;
}
+ protected <T> T getAnonymous(final String url, Class<T> clazz) {
+ return getAs(url, clazz, null, null);
+ }
+
protected <T> T get(final String url, Class<T> clazz) {
+ return getAs(url, clazz, "karaf", "karaf");
+ }
+
+ protected <T> T getAs(final String url, Class<T> clazz, final String
username, final String password) {
CloseableHttpResponse response = null;
try {
final HttpGet httpGet = new HttpGet(getFullUrl(url));
+ setBasicAuth(httpGet, username, password);
+
Review Comment:
Actually there is already a Credential provider on the httpClient that is in
charge of setting the auth headers for karaf:karaf.
This is done in the httpClient init method in current class:
```
credsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(BASIC_AUTH_USER_NAME,
BASIC_AUTH_PASSWORD));
HttpClientBuilder httpClientBuilder =
HttpClients.custom().useSystemProperties().setDefaultCredentialsProvider(credsProvider);
```
I'm not sure witch one is used on this requests, since the header is added
manually, but probably for code clarity we should get rid of the
CredentialProvider. (to see if the httpClient was used directly somewhere else
than this functions.)
##########
itests/src/test/java/org/apache/unomi/itests/BaseIT.java:
##########
@@ -483,14 +502,35 @@ protected CloseableHttpResponse post(final String url,
final String resource, Co
return null;
}
+ protected CloseableHttpResponse postAnonymous(final String url, final
String resource, ContentType contentType) {
+ return postAs(url, resource, contentType, null, null);
+ }
+
+ protected CloseableHttpResponse postAnonymous(final String url, final
String resource) {
+ return postAnonymous(url, resource, JSON_CONTENT_TYPE);
+ }
+
+ protected CloseableHttpResponse post(final String url, final String
resource, ContentType contentType) {
+ return postAs(url, resource, contentType, "karaf", "karaf");
+ }
+
protected CloseableHttpResponse post(final String url, final String
resource) {
- return post(url, resource, JSON_CONTENT_TYPE);
+ return post(url,resource, JSON_CONTENT_TYPE);
+ }
+
+ protected CloseableHttpResponse deleteAnonymous(final String url) {
+ return deleteAs(url, null, null);
}
protected CloseableHttpResponse delete(final String url) {
+ return deleteAs(url, "karaf", "karaf");
+ }
+
+ protected CloseableHttpResponse deleteAs(final String url, final String
username, final String password) {
CloseableHttpResponse response = null;
try {
final HttpDelete httpDelete = new HttpDelete(getFullUrl(url));
+ setBasicAuth(httpDelete, username, password);
Review Comment:
Same here basic auth header is probably overriding the CredentialProvider
configure on the HttpClient
##########
itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java:
##########
@@ -525,6 +531,7 @@ public void testPersonalization() throws IOException,
InterruptedException {
Map<String, String> parameters = new HashMap<>();
HttpPost request = new HttpPost(URL + CONTEXT_URL);
+ setBasicAuth(request, "karaf", "karaf");
Review Comment:
Same here, request should be anonymous
##########
itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java:
##########
@@ -636,6 +646,7 @@ public void testRequireScoring() throws IOException,
InterruptedException {
// now let's test adding it.
parameters = new HashMap<>();
request = new HttpPost(URL + CONTEXT_URL);
+ setBasicAuth(request, "karaf", "karaf");
Review Comment:
Same here, request should be anonymous
##########
itests/src/test/java/org/apache/unomi/itests/BaseIT.java:
##########
@@ -467,9 +477,18 @@ protected <T> T get(final String url, Class<T> clazz) {
return null;
}
- protected CloseableHttpResponse post(final String url, final String
resource, ContentType contentType) {
+ protected void setBasicAuth(final HttpRequestBase request, final String
username, final String password) {
+ if (username != null && password != null) {
+ String basicAuth = username + ":" + password;
+ String wrap = "Basic " + new
String(Base64.getEncoder().encode(basicAuth.getBytes()));
+ request.setHeader("Authorization", wrap);
+ }
+ }
+
+ protected CloseableHttpResponse postAs(final String url, final String
resource, ContentType contentType, final String username, final String
password) {
try {
final HttpPost request = new HttpPost(getFullUrl(url));
+ setBasicAuth(request, username, password);
Review Comment:
Same here basic auth header is probably overriding the CredentialProvider
configure on the HttpClient
##########
itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java:
##########
@@ -414,6 +415,7 @@ public void
testCreateEventWithPropertiesValidation_Success() throws IOException
HttpPost request = new HttpPost(URL + CONTEXT_URL);
request.addHeader(THIRD_PARTY_HEADER_NAME, UNOMI_KEY);
request.setEntity(new
StringEntity(objectMapper.writeValueAsString(contextRequest),
ContentType.APPLICATION_JSON));
+ setBasicAuth(request, "karaf", "karaf");
Review Comment:
Same here this request should be anonymous
##########
itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java:
##########
@@ -562,6 +570,7 @@ public void testPersonalizationWithControlGroup() throws
IOException, Interrupte
// now let's test with session storage
parameters.put("storeInSession", "true");
request = new HttpPost(URL + CONTEXT_URL);
+ setBasicAuth(request, "karaf", "karaf");
Review Comment:
Same here, request should be anonymous
##########
itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java:
##########
@@ -469,6 +472,7 @@ public void
testCreateEventWithPropertyNameValidation_Failure() throws IOExcepti
//Act
HttpPost request = new HttpPost(URL + CONTEXT_URL);
+ setBasicAuth(request, "karaf", "karaf");
Review Comment:
Same here, request should be anonymous
##########
itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java:
##########
@@ -625,6 +634,7 @@ public void testRequireScoring() throws IOException,
InterruptedException {
// first let's make sure everything works without the requireScoring
parameter
parameters = new HashMap<>();
HttpPost request = new HttpPost(URL + CONTEXT_URL);
+ setBasicAuth(request, "karaf", "karaf");
Review Comment:
Same here, request should be anonymous
##########
itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java:
##########
@@ -442,6 +444,7 @@ public void
testCreateEventWithPropertyValueValidation_Failure() throws IOExcept
//Act
HttpPost request = new HttpPost(URL + CONTEXT_URL);
+ setBasicAuth(request, "karaf", "karaf");
Review Comment:
Same here, request should be anonymous
##########
graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/servlet/auth/GraphQLServletSecurityValidator.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.unomi.graphql.servlet.auth;
+
+import graphql.language.Definition;
+import graphql.language.Document;
+import graphql.language.Field;
+import graphql.language.Node;
+import graphql.language.OperationDefinition;
+import graphql.parser.Parser;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.callback.PasswordCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Base64;
+import java.util.List;
+
+import static graphql.language.OperationDefinition.Operation.MUTATION;
+import static graphql.language.OperationDefinition.Operation.QUERY;
+import static graphql.language.OperationDefinition.Operation.SUBSCRIPTION;
+import static org.osgi.service.http.HttpContext.AUTHENTICATION_TYPE;
+import static org.osgi.service.http.HttpContext.REMOTE_USER;
+
+public class GraphQLServletSecurityValidator {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(GraphQLServletSecurityValidator.class);
+
+ private final Parser parser;
+
+ public GraphQLServletSecurityValidator() {
+ parser = new Parser();
+ }
+
+ public boolean validate(String query, String operationName,
HttpServletRequest req, HttpServletResponse res) throws IOException {
+ if (isPublicOperation(query)) {
+ return true;
+ } else if (req.getHeader("Authorization") == null) {
+ res.addHeader("WWW-Authenticate", "Basic realm=\"karaf\"");
+ res.sendError(HttpServletResponse.SC_UNAUTHORIZED);
+ return false;
+ }
+
+ if (isAuthenticatedUser(req)) {
+ return true;
+ } else {
+ res.sendError(HttpServletResponse.SC_UNAUTHORIZED);
+ return false;
+ }
+ }
+
+ private boolean isPublicOperation(String query) {
+ final Document queryDoc = parser.parseDocument(query);
+ final Definition<?> def = queryDoc.getDefinitions().get(0);
+ if (def instanceof OperationDefinition) {
+ OperationDefinition opDef = (OperationDefinition) def;
+ if ("IntrospectionQuery".equals(opDef.getName())
+ || SUBSCRIPTION.equals(opDef.getOperation())) {
Review Comment:
Are we sure that all subscriptions can be considered as public ?
##########
itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java:
##########
@@ -491,6 +495,7 @@ public void testOGNLVulnerability() throws IOException,
InterruptedException {
Map<String, String> parameters = new HashMap<>();
parameters.put("VULN_FILE_PATH", vulnFileCanonicalPath);
HttpPost request = new HttpPost(URL + CONTEXT_URL);
+ setBasicAuth(request, "karaf", "karaf");
Review Comment:
Same here, request should be anonymous
##########
graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/servlet/auth/GraphQLServletSecurityValidator.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.unomi.graphql.servlet.auth;
+
+import graphql.language.Definition;
+import graphql.language.Document;
+import graphql.language.Field;
+import graphql.language.Node;
+import graphql.language.OperationDefinition;
+import graphql.parser.Parser;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.callback.PasswordCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Base64;
+import java.util.List;
+
+import static graphql.language.OperationDefinition.Operation.MUTATION;
+import static graphql.language.OperationDefinition.Operation.QUERY;
+import static graphql.language.OperationDefinition.Operation.SUBSCRIPTION;
+import static org.osgi.service.http.HttpContext.AUTHENTICATION_TYPE;
+import static org.osgi.service.http.HttpContext.REMOTE_USER;
+
+public class GraphQLServletSecurityValidator {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(GraphQLServletSecurityValidator.class);
+
+ private final Parser parser;
+
+ public GraphQLServletSecurityValidator() {
+ parser = new Parser();
+ }
+
+ public boolean validate(String query, String operationName,
HttpServletRequest req, HttpServletResponse res) throws IOException {
+ if (isPublicOperation(query)) {
+ return true;
+ } else if (req.getHeader("Authorization") == null) {
+ res.addHeader("WWW-Authenticate", "Basic realm=\"karaf\"");
+ res.sendError(HttpServletResponse.SC_UNAUTHORIZED);
+ return false;
+ }
+
+ if (isAuthenticatedUser(req)) {
+ return true;
+ } else {
+ res.sendError(HttpServletResponse.SC_UNAUTHORIZED);
+ return false;
+ }
+ }
+
+ private boolean isPublicOperation(String query) {
+ final Document queryDoc = parser.parseDocument(query);
+ final Definition<?> def = queryDoc.getDefinitions().get(0);
+ if (def instanceof OperationDefinition) {
+ OperationDefinition opDef = (OperationDefinition) def;
+ if ("IntrospectionQuery".equals(opDef.getName())
+ || SUBSCRIPTION.equals(opDef.getOperation())) {
+ // allow subscriptions and introspection query
+ return true;
+ }
+
+ List<Node> children = opDef.getSelectionSet().getChildren();
+ final Field cdp = (Field) children.stream().filter((node) -> {
+ return (node instanceof Field) && "cdp".equals(((Field)
node).getName());
+ }).findFirst().orElse(null);
+ if (cdp == null) {
+ // allow not a cdp namespace
+ return true;
+ }
+
+ String allowedNodeName = "";
+ if (QUERY.equals(opDef.getOperation())) {
+ allowedNodeName = "getProfile";
Review Comment:
This look dangerous, because the Unomi ticket description mentioned:
**Retrieve current profile**
But looking at the current method, anybody could read the profile of the
others. since the getProfile query can take a profileId as parameter.
##########
itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java:
##########
@@ -536,6 +543,7 @@ public void testPersonalizationWithControlGroup() throws
IOException, Interrupte
Map<String, String> parameters = new HashMap<>();
parameters.put("storeInSession", "false");
HttpPost request = new HttpPost(URL + CONTEXT_URL);
+ setBasicAuth(request, "karaf", "karaf");
Review Comment:
Same here, request should be anonymous
##########
itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java:
##########
@@ -512,6 +517,7 @@ public void testMVELVulnerability() throws IOException,
InterruptedException {
Map<String, String> parameters = new HashMap<>();
parameters.put("VULN_FILE_PATH", vulnFileCanonicalPath);
HttpPost request = new HttpPost(URL + CONTEXT_URL);
+ setBasicAuth(request, "karaf", "karaf");
Review Comment:
Same here, request should be anonymous
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]