Skip to content
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

Allow custom headers to be returned as part of pre-flight requests #423

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

miike
Copy link
Contributor

@miike miike commented Apr 17, 2024

Certain services (e.g., Datadog) have a requirement for adding non CORS-safelisted headers to network requests in order to record tracing information for later reporting.

Currently requests which included non-safelisted headers error out when performing the pre-flight OPTIONS request. This adds the ability to specify a custom list (in addition to the default headers) that are returned as part of the Access-Control-Allow-Headers response.

Context: https://discourse.snowplow.io/t/access-to-xmlhttprequest-has-been-blocked-by-cors-policy-request-header-field-traceparent-is-not-allowed-by-access-control-allow-headers-in-preflight-response/9922/3

@miike miike force-pushed the acah branch 2 times, most recently from 8d28487 to 2d7a3c8 Compare April 17, 2024 09:46
Comment on lines +143 to +144
val allowedHeaders = (List("Content-Type", "SP-Anonymous") ++ config.cors.allowedCorsHeaders).map(CIString(_))
val corsHeaders = NonEmptyList.fromListUnsafe(allowedHeaders)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we can remove the "unsafe" method. Which is harmless in this case, but stands out as worrisome when you see it in collector code.

val corsHeaders = NonEmptyList("Content-Type", "SP-Anonymous" :: config.cors.allowedCorsHeaders).map(CIString(_))

@@ -52,6 +52,7 @@

cors {
accessControlMaxAge = 60 minutes
allowedCorsHeaders = ["X-Example"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file provides defaults for the app. So by adding "X-Example" here, it means all pipelines everywhere will respond with "X-Example" in the list of allowed headers.

Guessing that's probably not what you want?

@@ -139,11 +140,13 @@ class Service[F[_]: Sync](
}

override def preflightResponse(req: Request[F]): F[Response[F]] = Sync[F].pure {
val allowedHeaders = (List("Content-Type", "SP-Anonymous") ++ config.cors.allowedCorsHeaders).map(CIString(_))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about just making Content-Type and SP-Anonymous a part of the allowedCorsHeaders setting? That would avoid rebuilding this list on every request. And actually allowedCorsHeaders is a misnomer if it does not include all allowed headers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean as part of the config? My only concern would be if people remove this from a default config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants