carlin 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.mjs +52 -2
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -3462,6 +3462,39 @@ const BASELINE_RESPONSE_HEADERS_CONFIG = {
3462
3462
  }
3463
3463
  }
3464
3464
  };
3465
+ /**
3466
+ * The headers of `BASELINE_RESPONSE_HEADERS_CONFIG.CorsConfig` expressed as
3467
+ * custom headers.
3468
+ *
3469
+ * CloudFront's CORS handling owns `Vary`: with a `CorsConfig` in the policy it
3470
+ * sends `Vary: Origin` on a plain request and removes `Vary` altogether on a
3471
+ * cross-origin one, so a caller-defined `Vary` only survives when the policy
3472
+ * has no `CorsConfig`. The baseline allows a static `*`, which nothing varies
3473
+ * by, so the same headers can be emitted as custom headers and the caller's
3474
+ * `Vary` becomes both true and stable.
3475
+ *
3476
+ * `Access-Control-Allow-Credentials` is omitted because the baseline sets it
3477
+ * to `false`, which is the absence of the header, and `Access-Control-Max-Age`
3478
+ * because the baseline doesn't set it.
3479
+ */
3480
+ const CORS_AS_CUSTOM_HEADERS = [
3481
+ {
3482
+ header: "access-control-allow-origin",
3483
+ value: "*"
3484
+ },
3485
+ {
3486
+ header: "access-control-allow-methods",
3487
+ value: "DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT"
3488
+ },
3489
+ {
3490
+ header: "access-control-allow-headers",
3491
+ value: "*"
3492
+ },
3493
+ {
3494
+ header: "access-control-expose-headers",
3495
+ value: "*"
3496
+ }
3497
+ ];
3465
3498
  const validateResponseHeaders = (responseHeaders) => {
3466
3499
  const seen = /* @__PURE__ */ new Set();
3467
3500
  for (const { header } of responseHeaders) {
@@ -3524,10 +3557,27 @@ const getResponseHeadersPolicyConfig = ({ responseHeaders }) => {
3524
3557
  const securityHeadersConfigKey = SECURITY_HEADERS_CONFIG_KEYS[header.toLowerCase()];
3525
3558
  if (securityHeadersConfigKey) delete securityHeadersConfig[securityHeadersConfigKey];
3526
3559
  }
3560
+ /**
3561
+ * A `CorsConfig` makes CloudFront manage `Vary` itself, which silently
3562
+ * discards a user-defined one, so the CORS headers move to
3563
+ * `CustomHeadersConfig` when `vary` is defined. The one behavior lost is the
3564
+ * automatic `OPTIONS` preflight response of `CorsConfig`; simple
3565
+ * cross-origin requests, which is what a static app serves, don't preflight.
3566
+ */
3567
+ const definesVary = responseHeaders.some(({ header }) => {
3568
+ return header.toLowerCase() === "vary";
3569
+ });
3570
+ const customHeaders = definesVary ? [...responseHeaders, ...CORS_AS_CUSTOM_HEADERS.map(({ header, value }) => {
3571
+ return {
3572
+ header,
3573
+ override: true,
3574
+ value
3575
+ };
3576
+ })] : responseHeaders;
3527
3577
  return {
3528
3578
  Comment: { "Fn::Sub": ["Response headers policy for ${Project} project.", { Project: { Ref: "Project" } }] },
3529
- CorsConfig: BASELINE_RESPONSE_HEADERS_CONFIG.CorsConfig,
3530
- CustomHeadersConfig: { Items: responseHeaders.map(({ header, value, override }) => {
3579
+ ...definesVary ? {} : { CorsConfig: BASELINE_RESPONSE_HEADERS_CONFIG.CorsConfig },
3580
+ CustomHeadersConfig: { Items: customHeaders.map(({ header, value, override }) => {
3531
3581
  return {
3532
3582
  Header: header,
3533
3583
  Override: override,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "carlin",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "homepage": "https://ttoss.dev/docs/carlin/",