-
Notifications
You must be signed in to change notification settings - Fork 429
OAK-12259: oak-http: fix HTTP Basic credential parsing in OakServlet #2957
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
Closed
ciechanowiec
wants to merge
1
commit into
apache:trunk
from
ciechanowiec:OAK-12259/OAK-http-basic-auth-parsing
Closed
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
142 changes: 142 additions & 0 deletions
142
oak-http/src/test/java/org/apache/jackrabbit/oak/http/OakServletTest.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,142 @@ | ||
| /* | ||
| * 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.jackrabbit.oak.http; | ||
|
|
||
| import javax.jcr.Credentials; | ||
| import javax.jcr.NoSuchWorkspaceException; | ||
| import javax.jcr.SimpleCredentials; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| import org.apache.jackrabbit.oak.api.ContentRepository; | ||
| import org.apache.jackrabbit.util.Base64; | ||
| import org.junit.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
|
|
||
| import static org.junit.Assert.assertArrayEquals; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.isNull; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| public class OakServletTest { | ||
|
|
||
| private static String basicHeader(String userId, String password) { | ||
| return "Basic " + Base64.encode(userId + ":" + password); | ||
| } | ||
|
|
||
| /** | ||
| * Drives {@link OakServlet#service} and captures the credentials it derives | ||
| * from the {@code Authorization} header. The repository is stubbed to throw | ||
| * {@link NoSuchWorkspaceException} so the request short-circuits (handled as | ||
| * a 404) right after the credentials are parsed. | ||
| */ | ||
| private static SimpleCredentials parsedCredentials(String authorization) | ||
| throws Exception { | ||
| ContentRepository repository = mock(ContentRepository.class); | ||
| ArgumentCaptor<Credentials> captor = | ||
| ArgumentCaptor.forClass(Credentials.class); | ||
| when(repository.login(captor.capture(), isNull())) | ||
| .thenThrow(new NoSuchWorkspaceException()); | ||
|
|
||
| HttpServletRequest request = mock(HttpServletRequest.class); | ||
| when(request.getHeader("Authorization")).thenReturn(authorization); | ||
| HttpServletResponse response = mock(HttpServletResponse.class); | ||
|
|
||
| new OakServlet(repository).service(request, response); | ||
|
|
||
| verify(response).sendError(HttpServletResponse.SC_NOT_FOUND); | ||
| Credentials credentials = captor.getValue(); | ||
| assertTrue(credentials instanceof SimpleCredentials); | ||
| return (SimpleCredentials) credentials; | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that a malformed {@code Authorization} header is rejected with a | ||
| * 401 challenge and never reaches the repository login. | ||
| */ | ||
| private static void assertUnauthorized(String authorization) | ||
| throws Exception { | ||
| ContentRepository repository = mock(ContentRepository.class); | ||
| HttpServletRequest request = mock(HttpServletRequest.class); | ||
| when(request.getHeader("Authorization")).thenReturn(authorization); | ||
| HttpServletResponse response = mock(HttpServletResponse.class); | ||
|
|
||
| new OakServlet(repository).service(request, response); | ||
|
|
||
| verify(response).setHeader("WWW-Authenticate", "Basic realm=\"Oak\""); | ||
| verify(response).sendError(HttpServletResponse.SC_UNAUTHORIZED); | ||
| verify(repository, never()).login(any(), any()); | ||
| } | ||
|
Comment on lines
+76
to
+88
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. Nits : Move the helper method to the end of Test file. |
||
|
|
||
| @Test | ||
| public void parsesSimpleCredentials() throws Exception { | ||
| SimpleCredentials credentials = parsedCredentials(basicHeader("admin", "secret")); | ||
| assertEquals("admin", credentials.getUserID()); | ||
| assertArrayEquals("secret".toCharArray(), credentials.getPassword()); | ||
| } | ||
|
|
||
| /** | ||
| * Core security regression: a password may contain colons (RFC 7617), and | ||
| * the whole password must be preserved. The previous {@code split(":")} | ||
| * implementation silently truncated it, weakening authentication. | ||
| */ | ||
| @Test | ||
| public void preservesColonsInPassword() throws Exception { | ||
| SimpleCredentials credentials = parsedCredentials(basicHeader("admin", "p4ss:w0rd:!")); | ||
| assertEquals("admin", credentials.getUserID()); | ||
| assertArrayEquals("p4ss:w0rd:!".toCharArray(), credentials.getPassword()); | ||
| } | ||
|
|
||
| @Test | ||
| public void acceptsEmptyPassword() throws Exception { | ||
| SimpleCredentials credentials = parsedCredentials(basicHeader("admin", "")); | ||
| assertEquals("admin", credentials.getUserID()); | ||
| assertArrayEquals(new char[0], credentials.getPassword()); | ||
| } | ||
|
|
||
| @Test | ||
| public void acceptsEmptyUserId() throws Exception { | ||
| SimpleCredentials credentials = parsedCredentials(basicHeader("", "secret")); | ||
| assertEquals("", credentials.getUserID()); | ||
| assertArrayEquals("secret".toCharArray(), credentials.getPassword()); | ||
| } | ||
|
|
||
| /** | ||
| * A decoded value without a colon must be rejected cleanly (401) rather than | ||
| * throwing an unhandled {@link ArrayIndexOutOfBoundsException} that would | ||
| * surface as an HTTP 500. | ||
| */ | ||
| @Test | ||
| public void rejectsMissingColon() throws Exception { | ||
| assertUnauthorized("Basic " + Base64.encode("nocolon")); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsMissingHeader() throws Exception { | ||
| assertUnauthorized(null); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsNonBasicScheme() throws Exception { | ||
| assertUnauthorized("Bearer sometoken"); | ||
| } | ||
| } | ||
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.
Shouldn't we explicitly check for whether the username has a
:or not, and throw an exception if it has ?cc @anchela @Amoratinos @reschke