Skip to content

fix(core): let an empty api.subDomainPrefix replace the default - #3577

Merged
hellofanny merged 5 commits into
devfrom
fix/subdomain-prefix-override
Nov 9, 2026
Merged

hellofanny merged 5 commits into
devfrom
fix/subdomain-prefix-override

Conversation

@hellofanny

@hellofanny hellofanny commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

What's the purpose of this pull request?

A store that sets api.subDomainPrefix: [] in discovery.config.js still ends up with ['www'], so there is no way to stop www. from being stripped from the host sent to Checkout.

discovery.config.js merges the default config with the store config using deepmerge, which concatenates arrays. The default is subDomainPrefix: ['www'], so [] becomes ['www'].

The commerce client uses that array to remove the prefix from x-forwarded-host. With www always present, Checkout receives the root domain and sets checkout.vtex.com, CheckoutOrderFormOwnership, CheckoutLocale and CheckoutDataAccess on it. Calls made by the browser straight to /api/checkout set the same cookies on www.<domain>, so they are sent duplicated and the Cookie header can exceed the server header limit.

How it works?

  • New mergeDiscoveryConfig helper (src/utils/mergeDiscoveryConfig.js), used for every merge in discovery.config.js. It works like deepmerge, except that an empty subDomainPrefix replaces the default.
  • Any non-empty subDomainPrefix is still appended to ['www'], exactly as today. This keeps stores that set a custom prefix (e.g. ['shop']) and rely on www being stripped too working unchanged.
  • next.config.js prints a warning at build/dev time when subDomainPrefix is [] and secureSubdomain is on another host, since Checkout cookies set on the storefront host are not sent to it.

Behavior change

Only stores that set api.subDomainPrefix: [] are affected: today that value is ignored, and after this change www. is no longer stripped, so Checkout cookies are set on www.<domain> instead of <domain>. Stores that don't set the field, or set any non-empty value, keep the current behavior.

Cookies already set on the root domain stay in the browser until they expire or are removed, so returning visitors keep sending both copies for a while.

How to test it?

  • cd packages/core && pnpm vitest run test/utils/mergeDiscoveryConfig.test.ts
  • Set api.subDomainPrefix: [] in a store, call /api/graphql (e.g. ValidateCartMutation) with Host: www.<domain> and check that the Checkout cookies come with domain=www.<domain> instead of <domain>. Without the field, or with ['shop'], they stay on <domain>.
  • Set api.subDomainPrefix: [] with secureSubdomain on another host (e.g. https://secure.<domain>) and check the warning on next build / next dev.

Starters Deploy Preview

References

  • Checkout sets the cookie domain from X-FORWARDED-HOST (checked against the public orderForm endpoint).

Checklist

PR Title and Commit Messages

PR Description

  • Added a label according to the PR goal - bug

Dependencies

  • No changes to pnpm-lock.yaml

Documentation

  • PR description

🤖 Generated with Claude Code

deepmerge concatenates arrays, so the default ['www'] was always kept and
a store setting api.subDomainPrefix: [] still ended up with ['www']. That
made the commerce client strip "www." from X-FORWARDED-HOST, so Checkout
cookies were set on the root domain and duplicated the ones set on www.

Merge discovery configs through a helper that takes the store value for
subDomainPrefix as is. Other arrays keep being concatenated.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@hellofanny hellofanny added the bug Something isn't working label Nov 6, 2026
@dk-pr-review

dk-pr-review Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

DK Review — Audit Summary

Verdict: ⚠️ RESTRICTED

Severity Count
BLOCK 0
RESTRICT 1
SUGGEST 3

Only BLOCK and RESTRICT findings anchored to a changed line are commented inline on the diff. The other 3 findings below open no review thread.

Scenarios evaluated: feature-flags, general-review, quality-ratchet
Scenarios skipped: dependency-governance (no matching files), pipeline-config (no matching files), agent-skills-review (no matching files)


📋 Findings (4)

RESTRICT

  1. [Evolvability.SolutionApproach] packages/core/test/utils/mergeDiscoveryConfig.test.ts:58 — The tests show mergeDiscoveryConfig hardcodes a domain-specific exception for the single path api.subDomainPrefix (empty array ⇒ clear, non-empty ⇒ append), while an identically-shaped other.subDomainPrefix field at a different path is merged by plain concatenation instead. A generic config-merging utility now carries field-path-specific knowledge, so every future field needing 'clear with empty array' semantics will require another hardcoded branch in the same generic utility, and the inconsistent array semantics (same value shape, different merge rule depending on key path) is easy to misuse by accident.
    → Extract the 'clearable array' semantics into an explicit, reusable mechanism (e.g. a sentinel/wrapper value or a declared list of clearable paths) instead of special-casing api.subDomainPrefix by name inside the generic merge function.

SUGGEST

  1. [general.clarity] packages/core/AGENTS.md:148 — The doc states a non-empty subDomainPrefix is always 'appended to the default ['www']', but with chained merges (discovery.config.js merges starter config then discovery.config.local) that's only true for the first merge step. If a starter config clears it to [] and discovery.config.local later sets a non-empty value, mergeDiscoveryConfig (src/utils/mergeDiscoveryConfig.js) concatenates onto the already-cleared array, so the result is just that value, not ['www', ...]. The current wording could mislead someone debugging why 'www' isn't being stripped despite a non-empty override.
    → Clarify that the non-empty-array case appends to whatever the current intermediate config holds at that merge step, not necessarily the literal default ['www'], since an earlier merge step may have already cleared it.
  2. [quality.new-logic-enforcement] packages/core/test/utils/mergeDiscoveryConfig.test.ts:52 — 'keeps concatenating the other arrays' and 'only clears subDomainPrefix under api' only merge a non-empty default array with an EMPTY override array (e.g. ['a'] with []), which is also the expected result under a 'keep default when override is empty' rule, not just under true concatenation. No test merges two non-empty arrays (e.g. default ['a'] with override ['b']) to confirm the generic path actually concatenates rather than discarding or replacing the override.
    → Add a case merging two non-empty arrays on a non-subDomainPrefix path and assert the result is the concatenation of both (e.g. ['a','b']).
  3. [quality.new-logic-enforcement] packages/core/test/utils/subDomainPrefixWarning.test.ts:32 — The 'not set' case for secureSubdomain is only tested with an empty string (''), but since api.subDomainPrefix and other store config fields are optional elsewhere in these tests (subDomainPrefix: undefined is explicitly tested), secureSubdomain is plausibly undefined rather than '' when not configured. That path through getSubDomainPrefixWarning is untested.
    → Add a case calling getSubDomainPrefixWarning with secureSubdomain as undefined to confirm it is treated the same as an empty string (no warning).

DK Review v1.0.0 | To contest a finding, reply in its thread

Addresses the Sonar finding on mergeDiscoveryConfig.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@pkg-pr-new

pkg-pr-new Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@faststore/api

npm i https://pkg.pr.new/vtex/faststore/@faststore/api@cad5c20

@faststore/cli

npm i https://pkg.pr.new/vtex/faststore/@faststore/cli@cad5c20

@faststore/components

npm i https://pkg.pr.new/vtex/faststore/@faststore/components@cad5c20

@faststore/core

npm i https://pkg.pr.new/vtex/faststore/@faststore/core@cad5c20

@faststore/diagnostics

npm i https://pkg.pr.new/vtex/faststore/@faststore/diagnostics@cad5c20

@faststore/lighthouse

npm i https://pkg.pr.new/vtex/faststore/@faststore/lighthouse@cad5c20

@faststore/sdk

npm i https://pkg.pr.new/vtex/faststore/@faststore/sdk@cad5c20

@faststore/ui

npm i https://pkg.pr.new/vtex/faststore/@faststore/ui@cad5c20

commit: cad5c20

function mergeDiscoveryConfig(target, source) {
return deepmerge(target, source, {
customMerge: (key) => (OVERRIDE_KEYS.has(key) ? overwrite : undefined),
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[Functional.Logic] 🟡 RESTRICT

deepmerge's customMerge callback is invoked with only the bare key name, not the full path, so OVERRIDE_KEYS.has(key) matches ANY key named 'subDomainPrefix' anywhere in the merged tree, not just api.subDomainPrefix. If the discovery config schema ever gains another field with that same name nested elsewhere (e.g. under a future cdn or routes section), it would silently be replaced instead of merged, diverging from deepmerge's normal behavior without any indication at the call site.

Action: Scope the override to the intended path, e.g. by checking the full key path (some deepmerge versions/wrappers expose it) or by pre-merging only the api subtree with the override logic and deepmerge-ing the rest normally.

To contest this finding, reply in this thread

Overriding every subDomainPrefix value changed the behavior of stores
that set a custom prefix (e.g. ['shop']) and still relied on 'www' being
appended. Now only [] replaces the default; any other value is appended
to ['www'] as before, so existing configs keep working.

Also warn at build time when subDomainPrefix is [] and secureSubdomain
is on another host, since Checkout cookies set on the storefront host
are not sent to it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@hellofanny hellofanny changed the title fix(core): let the store config override api.subDomainPrefix fix(core): let an empty api.subDomainPrefix replace the default Nov 9, 2026
const mergeSubDomainPrefix = (target, source) =>
Array.isArray(source) && source.length === 0
? source
: deepmerge(target, source)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[general.security-guidance] 🟡 RESTRICT

This PR is what actually enables api.subDomainPrefix: [] to take effect (previously deepmerge's array concatenation meant an empty array merged with the default ['www'] had no effect). Clearing this list changes where Checkout authentication cookies are scoped across subdomains — a security-sensitive, auth-cookie-adjacent change. Root AGENTS.md requires explicit human approval recorded in the PR description for modifications to authentication-related mechanisms; the only safeguard shipped here is a build-time console.warn.

Action: Confirm and note in the PR description that this change to cross-subdomain cookie scoping was reviewed/approved per the root AGENTS.md Security & Data Handling policy, or clarify why it falls outside that policy's scope.

To contest this finding, reply in this thread

}

const getHostname = (url) => {
try {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[general.error-handling] 🟡 RESTRICT

getHostname swallows any URL parsing error silently (empty catch, no logging), so a malformed/missing storeUrl or secureSubdomain causes getSubDomainPrefixWarning to just return undefined with no signal that the config is actually broken. A store that typos secureSubdomain (e.g. omits the protocol) will get no cross-subdomain-cookie warning at all, even though the underlying misconfiguration is exactly the kind of thing this warning exists to catch.

Action: Log a distinct diagnostic (or include the parse failure in the returned warning) when storeUrl/secureSubdomain are present but fail to parse, instead of treating parse failure the same as "nothing to warn about".

To contest this finding, reply in this thread

@hellofanny
hellofanny marked this pull request as ready for review November 9, 2026 14:35
@hellofanny
hellofanny requested a review from a team as a code owner November 9, 2026 14:35
@hellofanny
hellofanny requested review from lemagnetic and lucasfp13 and removed request for a team November 9, 2026 14:35
hellofanny and others added 2 commits November 9, 2026 12:00
…lid URLs

Apply the empty-array override only to api.subDomainPrefix instead of any
key with that name, and make the build warning report a storeUrl or
secureSubdomain that cannot be parsed instead of silently skipping it.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Sonar was reporting 47.5% coverage on new code even though the unit tests
cover mergeDiscoveryConfig.js fully: other test files load that module
through discovery.config.js, and the coverage of the lines only exercised
by its own test was lost when the reports were merged.

Keep the warning in a module that only next.config.js and its own test
load, so its coverage is reported correctly. Behavior is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sonar-workflows

Copy link
Copy Markdown

})

it('only clears subDomainPrefix under api', () => {
const config = mergeDiscoveryConfig(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[Evolvability.SolutionApproach] 🟡 RESTRICT

The tests show mergeDiscoveryConfig hardcodes a domain-specific exception for the single path api.subDomainPrefix (empty array ⇒ clear, non-empty ⇒ append), while an identically-shaped other.subDomainPrefix field at a different path is merged by plain concatenation instead. A generic config-merging utility now carries field-path-specific knowledge, so every future field needing 'clear with empty array' semantics will require another hardcoded branch in the same generic utility, and the inconsistent array semantics (same value shape, different merge rule depending on key path) is easy to misuse by accident.

Action: Extract the 'clearable array' semantics into an explicit, reusable mechanism (e.g. a sentinel/wrapper value or a declared list of clearable paths) instead of special-casing api.subDomainPrefix by name inside the generic merge function.

To contest this finding, reply in this thread

@hellofanny
hellofanny merged commit b3c3cc6 into dev Nov 9, 2026
13 of 15 checks passed
@eduardoformiga eduardoformiga mentioned this pull request Nov 10, 2026
3 of 8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants