Problem/Motivation

asm89/stack-cors added support for using regex patterns to match the origin header. That allows to do partial-wildcard matching for CORS, which is very useful.

Steps to reproduce

N/A

Proposed resolution

Add default entry for cors.config.allowedOriginsPatterns in default.services.yml.

Remaining tasks

  • Update tests

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

default.services.yml now includes allowedOriginsPatterns, which allows to use regex patterns for CORS matching. No pattern is set by default, so existing sites will continue working the same, honoring allowedOrigins (whether customized or not).

Issue fork drupal-3205688

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jedihe created an issue. See original summary.

jedihe’s picture

jedihe’s picture

Issue summary: View changes
d34dman’s picture

Mentioned in : https://drupalcontributions.opensocial.site/node/66
Cross linking to make it easier to find :)

jedihe’s picture

Issue summary: View changes
Status: Active » Needs review

Tests pass, let's now ask for review :)

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record
Related issues: +#3128982: Upgrade asm89/stack-cors to ^2.0 to fix cacheability

++ to add

jedihe’s picture

Just wrote the draft for the CR. Added a TODO:

TODO: check if the default .* wildcard for allowedOriginsPatterns will silently cause existing sites restricting CORS access to now start accepting requests from any origin; if that is the case provide suggestions for properly handling the change.

jedihe’s picture

Just tried running core/tests/Drupal/FunctionalTests/HttpKernel/CorsIntegrationTest.php with allowedOriginsPatterns set to .* in a test where allowedOrigins is set to restrict the matches and found that .* gets all domains to match.

Given this, I think the best is to set allowedOriginsPatterns to an empty array by default so all existing sites continue working the same as they currently do:

  • If a site is still using * for allowedOrigins, all domains should still match.
  • If a site uses a customized allowedOrigins to restrict access, that restriction should still apply.
jedihe’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record

CR ready.

jedihe’s picture

Issue summary: View changes
jedihe’s picture

Title: Include allowedOriginsPatterns in default.services.yml » Include allowedOriginsPatterns in default.services.yml (regex matching for CORS)

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new149 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mr.baileys’s picture

Darren Oh made their first commit to this issue’s fork.

dpagini’s picture

Status: Needs work » Reviewed & tested by the community

I'm going to bump this RTBC to get some more eyes on this. I have been using this allowedOriginsPatterns in production for the better part of a year, with a note in my codebase that this is undocumented in Drupal core. I think this should go into core to let people know it's an available option for them.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new89 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

nod_’s picture

Need a patch/mr against 11.x

mr.baileys’s picture

Status: Needs work » Needs review

Tried to rebase but ran into too many issues, so I cherry-picked the commits and created a new MR. I have updated the CorsIntegrationTest: the original test in this issue was written against asm89/stack-cors 1.x, while 11.x uses 2.x. The latter no longer returns http status code 403 for disallowed origins (see #3128982: Upgrade asm89/stack-cors to ^2.0 to fix cacheability).

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Tweaked the CR a bit for 10.2

quietone’s picture

Status: Reviewed & tested by the community » Needs review

I'm triaging RTBC issues. I read the IS and the comments. I didn't find any unanswered questions.

However, I do not see any comments that someone has reviewed the code.

I am setting back to NR for a code review.

darren oh’s picture

Status: Needs review » Reviewed & tested by the community

I have reviewed the code many times. It is not necessary for someone to post a comment saying, "I have reviewed the code," for the RTBC status to be valid. Please do not unnecessarily block progress on issues.

  • longwave committed d9986292 on 10.2.x
    Issue #3205688 by jedihe, mr.baileys, dpagini: Include...

  • longwave committed 08b493a2 on 11.x
    Issue #3205688 by jedihe, mr.baileys, dpagini: Include...
longwave’s picture

Status: Reviewed & tested by the community » Fixed

The change is effectively one line of sample code and the additional test coverage is comprehensive.

Committed and pushed 08b493a202 to 11.x and d9986292c5 to 10.2.x. Thanks!

Also published the change record.

dpagini’s picture

Woohoo, thanks @longwave!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.