Improper definition of exposedHeaders in sites/default/default.services.yml causes the following watchdog error.

Warning: implode(): Invalid arguments passed in Asm89\Stack\CorsService->addActualRequestHeaders() (line 85 of /my/drupal/vendor/asm89/stack-cors/src/Asm89/Stack/CorsService.php)
#0 /my/drupal/core/includes/bootstrap.inc(566): _drupal_error_handler_real(2, 'implode(): Inva...', '/my/drupal/...', 85, Array)
#1 [internal function]: _drupal_error_handler(2, 'implode(): Inva...', '/my/drupal/...', 85, Array)
#2 /my/drupal/vendor/asm89/stack-cors/src/Asm89/Stack/CorsService.php(85): implode(', ', true)
#3 /my/drupal/vendor/asm89/stack-cors/src/Asm89/Stack/Cors.php(53): Asm89\Stack\CorsService->addActualRequestHeaders(Object(Symfony\Component\HttpFoundation\JsonResponse), Object(Symfony\Component\HttpFoundation\Request))
#4 /my/drupal/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Asm89\Stack\Cors->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#5 /my/drupal/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(50): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#6 /my/drupal/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#7 /my/drupal/core/lib/Drupal/Core/DrupalKernel.php(656): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#8 /my/drupal/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #9 {main}.

This is due to the fact that asm89/stack-cors expects exposedHeaders to either be false, or an array. If you end up setting this to true it tries to implode a boolean and throws an error.

CommentFileSizeAuthor
#68 2905848-nr-bot.txt90 bytesneeds-review-queue-bot
#63 2905848-63.patch5.75 KBpradhumanjain2311
#60 2905848-58.patch5.53 KBurvashi_vora
#55 interdiff-44-55.txt1.29 KBadeshsharma
#55 2905848-55.patch5.53 KBadeshsharma
#51 interdiff_44-51.txt684 bytesurvashi_vora
#51 2905848-51.patch5.53 KBurvashi_vora
#50 interdiff_47_49.txt632 bytesRishabh Vishwakarma
#50 2905848-49.patch5.53 KBRishabh Vishwakarma
#48 interdiff_44-47.txt684 bytesurvashi_vora
#48 2905848-47.patch5.53 KBurvashi_vora
#44 interdiff-41-44.txt2.88 KBadeshsharma
#44 2905848-44.patch5.53 KBadeshsharma
#41 interdiff-38-41.txt2.54 KBadeshsharma
#41 2905848-41.patch5.49 KBadeshsharma
#38 interdiff_29-38.txt9.77 KBkkalashnikov
#38 improve_doc-2905848-38.patch5.34 KBkkalashnikov
#37 2905848-nr-bot.txt144 bytesneeds-review-queue-bot
#29 interdiff.txt2.48 KBshimpy
#29 2905848_29.patch5.25 KBshimpy
#26 2905848_26.patch2.65 KBMeenakshiG
#20 2905848_20.patch5.26 KBshimpy
#16 2905848_16.patch2.61 KBshimpy
#16 interdiff.txt2.08 KBshimpy
#9 2905848-9.patch2.49 KBalexpott
#5 issue-2905848.diff697 bytesmikejoconnor

Issue fork drupal-2905848

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sic created an issue. See original summary.

firewaller’s picture

Same here. +1

sic’s picture

In my case it was due to a wrong CORS setup. I just had the exposed headers set to true, which makes no sense :) Try to backtrace the cause.

johnhanley’s picture

I just encountered this today.

If you look at line 94 in CorsService.php (Drupal 8.4.4), it's expecting an array and not a Boolean.

$response->headers->set('Access-Control-Expose-Headers', implode(', ', $this->options['exposedHeaders']));

Defining 'exposedHeaders' as 'true' rightfully produces an error in the log.

So what's the purpose of this property? Should it contain the same header array as the allowedHeaders property?

mikejoconnor’s picture

Issue summary: View changes
FileSize
697 bytes

It seems like a good short term solution to this issue would be to provide some better help text in sites/default/default.services.yml.

mikejoconnor’s picture

Version: 8.3.5 » 8.6.x-dev
Component: contextual.module » base system
mikejoconnor’s picture

Status: Active » Needs review
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thank you for improving the documentation by giving an example too.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
2.49 KB
+++ b/sites/default/default.services.yml
@@ -166,7 +166,9 @@ parameters:
+    # Sets the Access-Control-Expose-Headers header. The default is false.
+    # To modify the exposed headers use a comma delimited list.
+    # i.e. ['Content-Type', 'Expires'] or ['*'] for all headers.
     exposedHeaders: false

+1 for improving the docs but I think it can be a little bit more specific. The exposedHeaders value expects a sequence - ie it can be:

    exposedHeaders:
      - Content-Type
      - Expires

or

    exposedHeaders: ['Content-Type', 'Expires']

I was thinking of just recommend changing To modify the exposed headers use a comma delimited list. to To modify the exposed headers use a comma delimited list within square brackets because the within square brackets and then I just ended up reading all the CORS docs and thought if we improve this one why not improve them all and require less cognitive overload on the person doing the configuring.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

littletiger’s picture

@alexpott
There seems to be an error in your patch? Code for exposed headers only handles the imploding of an array of values. There is no wildcard support for that property.

alexpott’s picture

@littletiger I was probably going on the patch in #5. But looking at the docs the situation is complex :) https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control...

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mr.baileys’s picture

Title: Warning: implode(): Invalid arguments passed in Asm89\Stack\CorsService->addActualRequestHeaders() » Improve documentation for cors configuration.
Status: Needs review » Needs work
Issue tags: +Novice
+++ b/sites/default/default.services.yml
@@ -160,15 +160,32 @@ parameters:
+    # Specify allowed headers and sets the Access-Control-Allow-Headers header.

Statement is switching between singular and plural (specify/sets). I think this should be either singular (specify/set) or plural (specifies/sets). Same goes for the allowedMethods comment.

There seems to be an error in your patch? Code for exposed headers only handles the imploding of an array of values. There is no wildcard support for that property.

This looks ok to me. Setting exposedHeaders: ['*'] will result in a Access-Control-Expose-Headers: * response header, which is valid according to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control....

+++ b/sites/default/default.services.yml
@@ -160,15 +160,32 @@ parameters:
+    # Sets the Access-Control-Max-Age header if set to an integer. This is the
+    # maximum number of seconds the results can be cached. For example, 600 to
+    # cache results of a preflight request for 10 minutes. See
+    # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age

Setting this value to '0' or false will omit the Access-Control-Max-Age header from the response. However, setting it to '-1' will explicitly disable caching, we might want to add that to the comment.

Simple changes to the existing patch, so tagging novice.

shimpy’s picture

Status: Needs work » Needs review
FileSize
2.08 KB
2.61 KB

I have created a patch with the mentioned suggestions in #15. Please review if more changes are required.

Status: Needs review » Needs work

The last submitted patch, 16: 2905848_16.patch, failed testing. View results

mr.baileys’s picture

Thanks @shimpy, changes look good. Before we can do a final review, we need to address the test failure: this is caused by #3067645: Add core scaffold assets to drupal/core's composer.json extra field, which landed a month ago. From now until 9.x, changes to default.services.yml will need to be made both in /sites/default/ and /core/assets/scaffold/files/ (the linked issue explains the reasoning for this).

mr.baileys’s picture

Still a novice issue, since the only required change is to copy /sites/default/default.services.yml to /core/assets/scaffold/files/default.services.yml and re-roll the patch with both files changed.

shimpy’s picture

Status: Needs work » Needs review
FileSize
5.26 KB

Hii @mr.baileys

Thanks for the suggestion. I have recreated a patch as per #19. Please review.

Status: Needs review » Needs work

The last submitted patch, 20: 2905848_20.patch, failed testing. View results

mr.baileys’s picture

Failed patch is a random test failure caused by #3041318: Various random fails due to mis-triggered Mink deprecation error, re-test requested.

mr.baileys’s picture

Status: Needs work » Needs review
shimpy’s picture

@mr.baileys

I have restested it.. It pass all tests.

longwave’s picture

Status: Needs review » Needs work
+++ b/core/assets/scaffold/files/default.services.yml
@@ -160,15 +160,33 @@ parameters:
+    # Setting Access-Control-Max-Age headers value to '0' or false will omit the this from the response.
+    # However, setting it to '-1' will explicitly disable caching. For example, 600 to
+    # cache results of a preflight request for 10 minutes. See

The wording here still needs a bit of work. "headers" when there is only one header, "the this" doesn't make sense either.

MeenakshiG’s picture

Status: Needs work » Needs review
FileSize
2.65 KB

Fixed the wordings

Status: Needs review » Needs work

The last submitted patch, 26: 2905848_26.patch, failed testing. View results

longwave’s picture

As per the previous patches we need to ensure that /sites/default/default.services.yml and /core/assets/scaffold/files/default.services.yml are changed in exactly the same way.

shimpy’s picture

I have updated both the files. Please review.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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.

larowlan’s picture

  1. +++ b/core/assets/scaffold/files/default.services.yml
    @@ -160,15 +160,33 @@ parameters:
    +    # list within square brackets. For example, ['Content-Type', 'Expires'] or
    +    # Setting exposedHeaders: ['*'] will result in a Access-Control-Expose-Headers:
    

    I think we should word this as per the above

    For example, ['Content-Type', 'Expires'] or ['*'] to expose all headers.

  2. +++ b/core/assets/scaffold/files/default.services.yml
    @@ -160,15 +160,33 @@ parameters:
    +    # However, setting it to '-1' will explicitly disable caching. For example, 600 to
    +    # cache results of a preflight request for 10 minutes. See
    

    I this this is missing some words before 600

    suggest. For example, setting the value to 600 will cache results of a preflight for 10 minutes.

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.

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
FileSize
144 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.

kkalashnikov’s picture

Patch for Drupal version 10.1.x

kkalashnikov’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Points in #33 were not addressed.

Not ready for review yet.

adeshsharma’s picture

Status: Needs work » Needs review
FileSize
5.49 KB
2.54 KB
smustgrave’s picture

Status: Needs review » Needs work

Now the strings are going over 80 characters per line.

adeshsharma’s picture

Assigned: Unassigned » adeshsharma
adeshsharma’s picture

Assigned: adeshsharma » Unassigned
Status: Needs work » Needs review
FileSize
5.53 KB
2.88 KB

Fixed docs' lines having characters above 80 in a single line.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks!

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/assets/scaffold/files/default.services.yml
@@ -200,18 +200,38 @@ parameters:
+    # ['https://www.drupal.org'] or ['*'] to allow any resource to access your
+    # resource. See
+    # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin

I think this should be 'allow any origin to access your resource' rather than 'resource to access your resource'

There's still a few lines that don't wrap at 80 chars too (At least according to dreditor)

urvashi_vora made their first commit to this issue’s fork.

urvashi_vora’s picture

Status: Needs work » Needs review
FileSize
5.53 KB
684 bytes

Hi @larowlan,

As per your suggestion, I have made changes with respect to patch #44.

Corrected the sentence. Please verify.

Setting the issue status to Needs Review.

Also, I am uploading a patch along with an interdiff file.

Thanks.

Status: Needs review » Needs work

The last submitted patch, 48: 2905848-47.patch, failed testing. View results

Rishabh Vishwakarma’s picture

Status: Needs work » Needs review
FileSize
5.53 KB
632 bytes

Resolved errors from #48

urvashi_vora’s picture

Shoot.
I guess some tech stack issue with my system probably.
Kindly, Ignore #44.

Also, @Rishabh, thanks for the patch and interdiff, but I believe @larowlan asked to change the sentence in "core/assets/scaffold/files/default.services.yml" but your interdiff showcases the changes in "sites/default/default.services.yml".

Please guide me, if I am getting this wrong anywhere.

Re-attaching the interdiff file with patch.

Thank You !

The last submitted patch, 50: 2905848-49.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 51: 2905848-51.patch, failed testing. View results

adeshsharma’s picture

Assigned: Unassigned » adeshsharma
adeshsharma’s picture

Assigned: adeshsharma » Unassigned
Status: Needs work » Needs review
FileSize
5.53 KB
1.29 KB

Fixed 'resource' to origin in both the files as suggested by larowlan in #46.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
edysmp’s picture

does this need issue summary update?

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 55: 2905848-55.patch, failed testing. View results

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.

urvashi_vora’s picture

Status: Needs work » Needs review
FileSize
5.53 KB

Hi,

I tested the patch of #55 in d11, it works fine, applies cleanly. Since, the issue has changed to 11.x, providing the patch for 11.x.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Hiding #60. Thank you but #55 applied cleanly to 11.x so #60 is just a re-upload from what I can tell, since there was no interdiff.

Can run the 11.x tests by clicking the "Add test/retest" link. Queued #55 for 11.x tests.

But #57 I believe is correct in that IS should be updated about what is being accomplished. Me marking RTBC in #56 was incorrect on my part.

mr.baileys’s picture

  1. +++ b/core/assets/scaffold/files/default.services.yml
    @@ -160,15 +160,33 @@ parameters:
    +    # Setting exposedHeaders: ['*'] will result in a Access-Control-Expose-Headers:
    +    # * response header. See
    

    This line should wrap at 80 characters. This will also fix the fact that header name and value are currently split over 2 lines.

  2. +++ b/core/assets/scaffold/files/default.services.yml
    @@ -160,15 +160,33 @@ parameters:
    +    # Setting Access-Control-Max-Age header value to '0' or false will omit this from the response.
    +    # However, setting it to '-1' will explicitly disable caching. For example, 600 to
    

    Line wrapping should be fixed.

It might make sense to merge #2905848: Improve documentation for cors configuration. and this issue, since that issue is about adding and documenting the new allowedOriginsPatterns-option to cors.config in default.services.yml.

pradhumanjain2311’s picture

Status: Needs work » Needs review
FileSize
5.75 KB

Made changes as per comment #62.
Please review.

needs-review-queue-bot’s picture

Status: Needs review » Needs work

The Needs Review Queue Bot tested this issue.

While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

sourabhjain made their first commit to this issue’s fork.

sourabhjain’s picture

Status: Needs work » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
90 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.

BramDriesen’s picture

sourabhjain’s picture

Hi @xjm @smustgrave
I intentionally deferred work on these issues. All the updates were made on November 14, 2023, and I've been addressing them sequentially in the Drupal issue queue without initially considering the tags assigned to each. I acknowledge this oversight and apologize for any confusion. Moving forward, I will ensure to prioritize and address issues based on their respective tags.