-
Notifications
You must be signed in to change notification settings - Fork 265
KNOX-3039 - Add error message sanitization to GatewayServlet #1062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
smolnar82
wants to merge
1
commit into
apache:master
Choose a base branch
from
smolnar82:KNOX-3039
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
gateway-server/src/main/java/org/apache/knox/gateway/SanitizedException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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.knox.gateway; | ||
|
|
||
| import javax.servlet.ServletException; | ||
|
|
||
| public class SanitizedException extends ServletException { | ||
|
|
||
| public SanitizedException() { | ||
| super(); | ||
| } | ||
|
|
||
| public SanitizedException(String message) { | ||
| super(message); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
gateway-server/src/main/java/org/apache/knox/gateway/util/ExceptionSanitizer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * 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.knox.gateway.util; | ||
|
|
||
| import org.apache.knox.gateway.SanitizedException; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class ExceptionSanitizer { | ||
|
|
||
| private ExceptionSanitizer() { | ||
| } | ||
|
|
||
| public static SanitizedException from(final Exception e, final boolean isSanitizationEnabled, final String pattern) { | ||
| if (Objects.isNull(e) || Objects.isNull(e.getMessage())) { | ||
| return new SanitizedException(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case there is no offending message (i.e. the message does not contain IP address) shouldn't we just throw the same exception instead of SanitizedException? looks like this might a) hide the original exception b) add new stack trace that won't be useful. |
||
| } | ||
| final String message = isSanitizationEnabled ? e.getMessage().replaceAll(pattern, "[hidden]") : e.getMessage(); | ||
| return new SanitizedException(message); | ||
| } | ||
| } | ||
131 changes: 131 additions & 0 deletions
131
gateway-server/src/test/java/org/apache/knox/gateway/GatewayServletTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * 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.knox.gateway; | ||
|
|
||
| import org.apache.knox.gateway.config.GatewayConfig; | ||
| import org.easymock.EasyMock; | ||
| import org.easymock.IMocksControl; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Parameterized; | ||
|
|
||
| import javax.servlet.FilterConfig; | ||
| import javax.servlet.ServletConfig; | ||
| import javax.servlet.ServletContext; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNull; | ||
|
|
||
| @RunWith(Parameterized.class) | ||
| public class GatewayServletTest { | ||
|
|
||
| private static final String IP_ADDRESS_PATTERN = "\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b"; | ||
| private static final String EMAIL_ADDRESS_PATTERN = "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b"; | ||
| private static final String CREDIT_CARD_PATTERN = "\\b\\d{4}-\\d{4}-\\d{4}-\\d{4}\\b"; | ||
|
|
||
| @Parameterized.Parameters(name = "{index}: SanitizationEnabled={2}, Pattern={3}, Exception={0}, ExpectedMessage={1}") | ||
| public static Collection<Object[]> data() { | ||
| return Arrays.asList(new Object[][] { | ||
| { new IOException("Connection to 192.168.1.1 failed"), "Connection to [hidden] failed", true, IP_ADDRESS_PATTERN }, | ||
| { new RuntimeException("Connection to 192.168.1.1 failed"), "Connection to [hidden] failed", true, IP_ADDRESS_PATTERN }, | ||
| { new NullPointerException(), null, true, IP_ADDRESS_PATTERN }, | ||
| { new IOException("General failure"), "General failure", true, IP_ADDRESS_PATTERN }, | ||
| { new IOException("Connection to 192.168.1.1 failed"), "Connection to 192.168.1.1 failed", false, IP_ADDRESS_PATTERN }, | ||
| { new RuntimeException("Connection to 192.168.1.1 failed"), "Connection to 192.168.1.1 failed", false, IP_ADDRESS_PATTERN }, | ||
| { new NullPointerException(), null, false, IP_ADDRESS_PATTERN }, | ||
| { new IOException("General failure"), "General failure", false, IP_ADDRESS_PATTERN }, | ||
| { new IOException("User email: user@example.com"), "User email: [hidden]", true, EMAIL_ADDRESS_PATTERN }, | ||
| { new RuntimeException("Credit card number: 1234-5678-9101-1121"), "Credit card number: [hidden]", true, CREDIT_CARD_PATTERN }, | ||
| }); | ||
| } | ||
|
|
||
| private final Exception exception; | ||
| private final String expectedMessage; | ||
| private final boolean isSanitizationEnabled; | ||
| private final String sanitizationPattern; | ||
|
|
||
| public GatewayServletTest(Exception exception, String expectedMessage, boolean isSanitizationEnabled, String sanitizationPattern) { | ||
| this.exception = exception; | ||
| this.expectedMessage = expectedMessage; | ||
| this.isSanitizationEnabled = isSanitizationEnabled; | ||
| this.sanitizationPattern = sanitizationPattern; | ||
| } | ||
|
|
||
| private IMocksControl mockControl; | ||
| private ServletConfig servletConfig; | ||
| private ServletContext servletContext; | ||
| private GatewayConfig gatewayConfig; | ||
| private HttpServletRequest request; | ||
| private HttpServletResponse response; | ||
| private GatewayFilter filter; | ||
|
|
||
| @Before | ||
| public void setUp() throws ServletException { | ||
| mockControl = EasyMock.createControl(); | ||
| servletConfig = mockControl.createMock(ServletConfig.class); | ||
| servletContext = mockControl.createMock(ServletContext.class); | ||
| gatewayConfig = mockControl.createMock(GatewayConfig.class); | ||
| request = mockControl.createMock(HttpServletRequest.class); | ||
| response = mockControl.createMock(HttpServletResponse.class); | ||
| filter = mockControl.createMock(GatewayFilter.class); | ||
|
|
||
| EasyMock.expect(servletConfig.getServletName()).andStubReturn("default"); | ||
| EasyMock.expect(servletConfig.getServletContext()).andStubReturn(servletContext); | ||
| EasyMock.expect(servletContext.getAttribute("org.apache.knox.gateway.config")).andStubReturn(gatewayConfig); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExceptionSanitization() throws ServletException, IOException { | ||
| GatewayServlet servlet = initializeServletWithSanitization(isSanitizationEnabled, sanitizationPattern); | ||
|
|
||
| try { | ||
| servlet.service(request, response); | ||
| } catch (Exception e) { | ||
| if (expectedMessage != null) { | ||
| assertEquals(expectedMessage, e.getMessage()); | ||
| } else { | ||
| assertNull(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| mockControl.verify(); | ||
| } | ||
|
|
||
| private GatewayServlet initializeServletWithSanitization(boolean isErrorMessageSanitizationEnabled, String sanitizationPattern) throws ServletException, IOException { | ||
| EasyMock.expect(gatewayConfig.isErrorMessageSanitizationEnabled()).andStubReturn(isErrorMessageSanitizationEnabled); | ||
| EasyMock.expect(gatewayConfig.getErrorMessageSanitizationPattern()).andStubReturn(sanitizationPattern); | ||
|
|
||
| filter.init(EasyMock.anyObject(FilterConfig.class)); | ||
| EasyMock.expectLastCall().once(); | ||
| filter.doFilter(EasyMock.eq(request), EasyMock.eq(response), EasyMock.isNull()); | ||
| EasyMock.expectLastCall().andThrow(exception).once(); | ||
|
|
||
| mockControl.replay(); | ||
|
|
||
| GatewayServlet servlet = new GatewayServlet(filter); | ||
| servlet.init(servletConfig); | ||
| return servlet; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also add support for ipv6?