Handle invalid Origin
header values in CorsMiddleware
#60
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.
Description
For requests with invalid
Origin
URIs (i.e. when usinglaminas/laminas-diactoros
, passingchrome-extension://asdkjasdkjasjkdasjkd
will throwInvalidOriginValueException
), theCorsMiddleware
is throwing an unhandled exception, which ends up in HTTP 500 in mezzio applications (unless the error is catched in the error handler and whyever some1 would then return a different HTTP status code).So to properly handle this (as it some PSR-7 implementations do allow any scheme), I've created laminas/laminas-diactoros#179.
However, after some feedback and more research, I found out that PSR-7 is actually quite "clear" on what MUST be supported and what MAY be supported:
It also states that unsupported schemes may end up in an
InvalidArgumentException
:So as a conclusion, I decided to handle these problems in
mezzio-cors
sinceInvalidOriginValueException
may also occur for other issues unrelated to thescheme
.So this patch handles
InvalidOriginValueException
inCorsMiddleware
when passing the request toCorsInterface#isCorsRequest
and returns an unauthorized response now.Prior this patch, an unhandled exception (
InvalidOriginValueException
) was bubbling up the middleware pipeline inmezzio
applications and thus, this change might not introduce a real BC break, WDYT?I am targeting the next minor as I also introduced a way to actually fetch the RAW
Origin
header value from theInvalidOriginValueException
so that consumers of theCorsInterface
can access the header value (so doesCorsMiddleware
).