-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Fix CORS throwing 500 upon encountering a malformed URL #33682 #33688
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
IgorOffline
wants to merge
6
commits into
spring-projects:main
from
IgorOffline:igoroffline/fix-33682
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
25caaf0
replace BeforeEach with individual setup step
IgorOffline 0a92d0f
Process CORS request is malformed URL aware
IgorOffline ca14494
replace and simplify malformed url validation, improve javadoc
IgorOffline 775ad7e
solution applied to reactivity
IgorOffline 43457fb
fix: javadoc
IgorOffline 517e5bb
Merge branch 'spring-projects:main' into igoroffline/fix-33682
IgorOffline 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import org.springframework.http.HttpMethod; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.ObjectUtils; | ||
import org.springframework.web.util.InvalidUrlException; | ||
import org.springframework.web.util.UriComponents; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
|
@@ -30,27 +31,35 @@ | |
* <a href="https://www.w3.org/TR/cors/">CORS W3C recommendation</a>. | ||
* | ||
* @author Sebastien Deleuze | ||
* @author Igor Durbek | ||
* @since 4.2 | ||
*/ | ||
public abstract class CorsUtils { | ||
|
||
/** | ||
* Returns {@code true} if the request is a valid CORS one by checking {@code Origin} | ||
* header presence and ensuring that origins are different. | ||
* Returns {@code IsCorsRequestResult.IS_CORS_REQUEST} if the request is a valid CORS one by checking {@code Origin} | ||
* header presence and ensuring that origins are different. Returns {@code IsCorsRequestResult.IS_NOT_CORS_REQUEST} | ||
* otherwise, or {@code IsCorsRequestResult.MALFORMED_ORIGIN} if the origin url is malformed. | ||
*/ | ||
public static boolean isCorsRequest(HttpServletRequest request) { | ||
public static IsCorsRequestResult isCorsRequest(HttpServletRequest request) { | ||
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. Is it OK to introduce a breaking public API change for this bugfix? If so, it's probably worth documenting in the release notes. |
||
String origin = request.getHeader(HttpHeaders.ORIGIN); | ||
if (origin == null) { | ||
return false; | ||
return IsCorsRequestResult.IS_NOT_CORS_REQUEST; | ||
} | ||
try { | ||
UriComponentsBuilder.fromUriString(origin); | ||
} | ||
catch (InvalidUrlException ex) { | ||
return IsCorsRequestResult.MALFORMED_ORIGIN; | ||
} | ||
UriComponents originUrl = UriComponentsBuilder.fromUriString(origin).build(); | ||
String scheme = request.getScheme(); | ||
String host = request.getServerName(); | ||
int port = request.getServerPort(); | ||
return !(ObjectUtils.nullSafeEquals(scheme, originUrl.getScheme()) && | ||
boolean isCorsRequest = !(ObjectUtils.nullSafeEquals(scheme, originUrl.getScheme()) && | ||
ObjectUtils.nullSafeEquals(host, originUrl.getHost()) && | ||
getPort(scheme, port) == getPort(originUrl.getScheme(), originUrl.getPort())); | ||
|
||
return isCorsRequest ? IsCorsRequestResult.IS_CORS_REQUEST : IsCorsRequestResult.IS_NOT_CORS_REQUEST; | ||
} | ||
|
||
private static int getPort(@Nullable String scheme, int port) { | ||
|
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
42 changes: 42 additions & 0 deletions
42
spring-web/src/main/java/org/springframework/web/cors/IsCorsRequestResult.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,42 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.springframework.web.cors; | ||
|
||
/** | ||
* Used to enumerate the CORS request result. | ||
* | ||
* @author Igor Durbek | ||
* @since 6.2 | ||
*/ | ||
public enum IsCorsRequestResult { | ||
|
||
/** | ||
* Is CORS request. | ||
*/ | ||
IS_CORS_REQUEST, | ||
|
||
/** | ||
* Is not a CORS request. | ||
*/ | ||
IS_NOT_CORS_REQUEST, | ||
|
||
/** | ||
* Invalid origin - reject the request. | ||
* See test for an example of a malformed origin request. | ||
*/ | ||
MALFORMED_ORIGIN | ||
} |
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
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
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.