-
Notifications
You must be signed in to change notification settings - Fork 920
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
Fix a bug where CORS headers are not injected when a ServerErrorHandler
is set
#5939
Conversation
@@ -47,6 +50,8 @@ public final class CorsHeaderUtil { | |||
public static final String DELIMITER = ","; | |||
private static final Joiner HEADER_JOINER = Joiner.on(DELIMITER); | |||
|
|||
private static AttributeKey<Boolean> IS_CORS_SET = AttributeKey.valueOf(CorsService.class, "IS_CORS_SET"); |
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.
private static AttributeKey<Boolean> IS_CORS_SET = AttributeKey.valueOf(CorsService.class, "IS_CORS_SET"); | |
private static final AttributeKey<Boolean> IS_CORS_SET = AttributeKey.valueOf(CorsService.class, "IS_CORS_SET"); |
@@ -47,6 +50,8 @@ public final class CorsHeaderUtil { | |||
public static final String DELIMITER = ","; | |||
private static final Joiner HEADER_JOINER = Joiner.on(DELIMITER); | |||
|
|||
private static AttributeKey<Boolean> IS_CORS_SET = AttributeKey.valueOf(CorsService.class, "IS_CORS_SET"); | |||
|
|||
public static ResponseHeaders addCorsHeaders(ServiceRequestContext ctx, CorsConfig corsConfig, |
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.
Could this method be removed?
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.
Nice catch!
return oldRes.recover(HttpResponseException.class, ex -> { | ||
return ex.httpResponse() | ||
.mapHeaders(oldHeaders -> addCorsHeaders(ctx, corsService.config(), oldHeaders)); | ||
final CorsService corsService = ctx.findService(CorsService.class); |
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.
Note: this was a problem before also, but preflight requests won't be handled correctly if an exception occurs before reaching CorsService
.
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.
I've changed to apply CORS headers only for simple requests or main requests.
If there is an error in processing preflight requests, we may not need to set CORS headers.
return oldRes.recover(HttpResponseException.class, ex -> { | ||
return ex.httpResponse() | ||
.mapHeaders(oldHeaders -> addCorsHeaders(ctx, corsService.config(), oldHeaders)); | ||
final CorsService corsService = ctx.findService(CorsService.class); |
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.
Note: This is also a similar issue, but a debug level log will be printed every time an exception is thrown for an Origin which doesn't have a policy defined:
armeria/core/src/main/java/com/linecorp/armeria/internal/server/CorsHeaderUtil.java
Line 127 in 683bd74
logger.debug( |
Normally, this log wouldn't be printed if a request goes through CorsService
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.
👍 👍 👍
Motivation:
ServerErrorHandler.renderStatus()
is used inDefaultServerErrorHandler
and may not be called in a customServerErrorHandler
. Attempting to inject CORS headers viaCorsServerErrorHandler.renderStatus()
may not work.Modifications:
ctx.mutateAdditionalResponseHeaders()
to set CORS headers instead of mutating the response headers.CorsService
to ctx.attr().CorsServerErrorHandler
to set if missingResult:
ServerErrorHandler
is configured.CorsService
does not inject CORS headers to error responses #5493