AWS Lambda (Function URL) port of the ../website Cloudflare Worker. It sits
behind CloudFront, proxies the AEM/Edge Delivery origin, and enforces per-viewer
access rules: gating private pages, stripping audience-* content blocks, and
filtering query-index.json and sitemap.xml. See index.js for the pipeline and
lib/gate.js for the access policy.
For an existing Lambda redeployment, use the
preview deployment runbook or
production deployment runbook. They cover latest-main source
checks, the default AWS profile, preserving environment variables, cache
invalidation, and human or AI-assisted verification.
Set these on the Lambda (they are read from process.env):
SESSION_SECRET— HMAC key for the session cookie. Unset ⇒ every request is treated as anonymous (fails closed). Do not set this in plaintext env in AWS — provide it viaSESSION_SECRET_IDinstead (see "Secrets" below).AEM_ORG/AEM_SITE— the upstreammain--<site>--<org>.aem.liveorigin.IMS_CLIENT_ID/IMS_CLIENT_SECRET/IMS_SCOPE— service credential for the DA visitor allowlist read (required by/auth/session). LikeSESSION_SECRET,IMS_CLIENT_SECRETshould come fromIMS_CLIENT_SECRET_ID, not plaintext env.ALLOWED_ORIGINS— comma-separated origins permitted to call/auth/session(CSRF defense). Set this explicitly in production. When unset the check falls back to the request's own origin (derived from the public host), which is only trustworthy behind CloudFront/OAC; a missing value is logged once per container.PUBLIC_HOST— the canonical public host (e.g.preview.spectrum.adobe.com), host only, no scheme. It overrides the request headers when set, so the worker reports one public origin regardless of which URL reached it; it drives thex-forwarded-hostsent to AEM (absolute URLs, redirects, sitemap) and theALLOWED_ORIGINSfallback. Behind CloudFront's AllViewerExceptHostHeader origin policy the viewer Host isn't forwarded, so set this (or a CloudFrontX-Forwarded-Hostorigin header) — otherwise the worker only sees the Function URL host.AEM_HOST_SUFFIX— the AEM tier to proxy:aem.live(published, the default) oraem.page(preview). The stage Lambda (spectrum-stage-lambda-proxy) sets this toaem.page; prod leaves it unset.ANON_CACHE_MAX_AGE— browser TTL (max-age, seconds, default 300) for anonymous HTML,/query-index.json, and/sitemap.xml. When both this andANON_EDGE_MAX_AGEare0, anonymous responses areno-store— used on stage, which caches nothing.ANON_EDGE_MAX_AGE— CloudFront TTL (s-maxage, seconds) for the same responses. Defaults toANON_CACHE_MAX_AGE. Prod sets it to86400, since a publish purges the CloudFront copy through push invalidation (see "Content caching").- Optional:
ORIGIN(dev origin override),ORIGIN_AUTHENTICATION,IMS_ENV,SESSION_MAX_AGE_MS,PUSH_INVALIDATION.
Setup, rotation, console steps, and the
set-secrets.shhelper are inSECRETS.md.
Adobe's AWS Security Standard §3.5.8.5 prohibits plaintext secrets in Lambda environment
variables. So the secret values never live in env.json; instead the env carries only a
non-secret id and the function fetches the value at runtime from Secrets Manager via its
execution role:
SESSION_SECRET_ID— Secrets Manager name/ARN holding theSESSION_SECRETvalue.IMS_CLIENT_SECRET_ID— Secrets Manager name/ARN holding theIMS_CLIENT_SECRETvalue.
At the first request on a cold container handler calls resolveSecrets(), which pulls each
configured id and assigns the value onto process.env under the plain name (SESSION_SECRET /
IMS_CLIENT_SECRET) — so the rest of the code reads it unchanged. Behavior:
- Bounded TTL cache (
SECRET_TTL_MS, 10 min): a rotated secret propagates within that window with no redeploy. For an immediate rotation (compromise), redeploy/update the function to force fresh cold starts. - Fail-closed: if the fetch fails on a cold container the secret stays unset, so
isAuthenticatedreturns false (everyone anonymous) and/auth/sessionreturns 500 — public pages still serve; nothing degrades open. A transient failure after a good fetch keeps the last-known-good value and retries after a short backoff (SECRET_FAILURE_BACKOFF_MS). - Migration-safe: if a
*_SECRET_IDis unset the resolver leaves any existing plaintextenv[name]in place, so the code can ship before the AWS-side secrets are created.
The SDK (@aws-sdk/client-secrets-manager) is a devDependency only and is imported
dynamically — the nodejs22.x runtime provides AWS SDK v3, so it is not bundled. If a runtime
ever lacks it, the dynamic import degrades to fail-closed instead of crashing init; the fallback is
to npm i it and include node_modules in the deploy zip.
AEM serves every page in two head-less variants as well: <page>.plain.html
(body markup only) and <page>.md (Markdown). Neither includes the page's
<head>, so the <meta name="audience" content="private"> check can't run on
the variant itself. For anonymous visitors the Lambda handles them as follows:
lib/gate.jsgetCanonicalPagePathmaps the variant to its page (/a/b.plain.html→/a/b,/index.md→/,/a/index.md→/a/).- Before proxying the variant,
index.jsisPublicCanonicalPagefetches that page from AEM and runsisPrivateHtmlon it. The lookup fails closed: a private page, a non-200or redirect, a non-HTML response, or a fetch error returns404, and the variant is never fetched. - A public variant is then served with its audience blocks stripped:
filterAudienceBlockshandles.plain.htmlmarkup (no<main>wrapper), andfilterAudienceMarkdownremovesName (audience private)block tables from.md.
The Markdown variant also retains the authored Metadata table. For anonymous
viewers, filterAudienceMarkdown removes its card-steward,
card-design-resources, and card-contact rows, including wrapped values and
link references used only by those rows. Public metadata and surrounding content
remain unchanged. Authenticated viewers retain every metadata row.
Authenticated requests skip the canonical lookup. For anonymous requests it adds one origin fetch per variant request that reaches the Lambda; the result is cached like any other anonymous page (see "Content caching"). Both variants fall under the default CloudFront behavior, so they always reach the Lambda.
Only HTML, Markdown (.md), and JSON vary by viewer, so only they go through the Lambda. Everything
else goes straight from CloudFront to AEM.
| Behavior (in order) | Origin | Cache policy (prod) | Cache policy (stage) |
|---|---|---|---|
/.rum/*, /.optel/* (all methods) |
AEM | CachingDisabled + spectrum-rum |
CachingDisabled + spectrum-rum |
*/media_*, /media_* |
AEM | spectrum-media |
CachingDisabled + spectrum-media-query |
*.js, *.mjs, *.css, *.svg, *.ico, *.png, *.jpg, *.jpeg, *.gif, *.webp, *.avif, *.woff, *.woff2, *.ttf, *.otf, *.xml, *.txt |
AEM | spectrum-assets |
CachingDisabled |
| Default (HTML, JSON, anything else) | Lambda | spectrum-content |
CachingDisabled |
*.xml also matches /sitemap.xml, which must reach the Lambda to be filtered.
Add an exact /sitemap.xml behavior ahead of *.xml with
add-sitemap-behavior.sh (see "Keep /sitemap.xml
on the Lambda").
set-content-caching.sh manages all of this. It's
idempotent, so rerun it after changing the extension list or a policy:
# prod
DIST_ID=E3VFWCMFUVXVV ./set-content-caching.sh
# stage: cache nothing (AEM push invalidation only covers main--*.aem.live)
NO_CACHE=1 DIST_ID=E2RMZZGQ0O3SJ1 ./set-content-caching.sh
# undo: assets back through the Lambda, default behavior on CachingDisabled
REVERT=1 DIST_ID=<id> ./set-content-caching.shThe asset behaviors are cloned from the */media_* behavior, so they share its
AEM origin headers (including Authorization) and the spectrum-strip-headers
function. Run add-media-behavior.sh first on a new
distribution.
CloudFront doesn't match root-level images (/media_<sha>.png) to */media_*,
so the script adds a /media_* copy before the asset behaviors. Without it,
*.png would catch them, drop width and format, and serve the full-size
original. On stage, CachingDisabled forwards no query strings, so both media
behaviors also use the spectrum-media-query origin request policy, which
forwards only the image parameters.
RUM beacons (/.rum/*, /.optel/*) go straight to AEM too, uncached and with
POST allowed. Through the Lambda they'd fail with a 403: the function URL
rejects POSTs without an x-amz-content-sha256 body hash, and sendBeacon
can't send one. The spectrum-rum origin request policy forwards
content-type, user-agent, referer, origin and the query string, but no
cookies.
Assets skip the Lambda gate. A non-HTML file under /drafts/ (for example,
/drafts/diagram.svg) is public. HTML and JSON under /drafts/ still go through
the Lambda and stay private.
The script also turns on Origin Shield for both origins (off with NO_CACHE=1),
so CloudFront edge locations share one regional cache in front of each origin.
Prod also answers on s2.spectrum.adobe.com. A viewer-request CloudFront
Function (cloudfront-functions/canonical-host.js)
301s it to https://spectrum.adobe.com, keeping the path and query string. It
runs before the cache, so these requests never reach the Lambda or AEM.
set-canonical-host-redirect.sh publishes
the function and attaches it to every behavior:
DIST_ID=E3VFWCMFUVXVV ./set-canonical-host-redirect.sh
# undo: detach it everywhere
REMOVE=1 DIST_ID=E3VFWCMFUVXVV ./set-canonical-host-redirect.shset-content-caching.sh clones its behaviors from */media_*, so they keep the
function when it's re-run. Re-run this script after adding behaviors any other
way.
Immutable, content-hashed media (media_<sha>.<ext>) is public, so it is served
straight from the AEM origin on its own */media_* CloudFront behavior,
bypassing the Lambda (and its 6 MB buffered-response cap + memory/CPU cost).
Gated HTML/JSON stays on the Lambda. add-media-behavior.sh
retrofits an existing distribution; setup-preview-distribution.sh
bakes it into new ones. Both are idempotent; REVERT=1 ./add-media-behavior.sh
sends media back through the Lambda. Modelled on Adobe's
BYO-CDN CloudFront guide:
- Origin headers
X-BYO-CDN-Type: cloudfront,X-Push-Invalidation: enabled, andX-Forwarded-Host(value fromFORWARDED_HOST, defaulting to the distribution's own*.cloudfront.netdomain). - Origin TLS
TLSv1/1.1/1.2,IpAddressType: ipv4, Origin Shieldus-east-1— matching a known-good CloudFront→aem.live origin. - A shared, account-global CloudFront Function
spectrum-strip-headers(cloudfront-functions/strip-headers.js) attached viewer-response on the media behavior, replicating the Lambda'sAge/X-Robots-Tagstripping (handlers/aem.js) for the direct path. - A custom cache policy
spectrum-media(created idempotently) that whitelists the image query params (width/height/format/optimize/quality) with a long TTL.
⚠️ Cache-policy gotcha (the thing that cost us a day): the managedUseOriginCacheControlHeaders-QueryStringspolicy (4cc15a8a…,QueryStringBehavior=all) makes CloudFront fail the origin connection to aem.live/aem.page with a502 "can't connect"— not a caching bug, a hard connectivity failure. A whitelist query-string policy (whatspectrum-mediais) connects fine and still caches per variant. Do not switch the media behavior to the…-QueryStringsmanaged policy. (Direct CloudFront→aem.live needs no BYO-CDN onboarding, contrary to an earlier assumption — a plain distribution pulls from the origin once the cache policy is right.)
Deliberate choices: no origin-request policy on the media behavior (so
spectrum_session is never forwarded to the public origin), and no cache-tag
push invalidation (immutable media never needs it). Because media bypasses the
Lambda, token-based origin auth needs the token in both places: the Lambda
path resolves ORIGIN_AUTHENTICATION from Secrets Manager
(ORIGIN_AUTHENTICATION_ID in the env file — see SECRETS.md),
and the media behavior carries a literal Authorization: token hlx_… origin
header. Pass the token to add-media-behavior.sh to add it:
ORIGIN_AUTHENTICATION=hlx_… DIST_ID=<stage-dist> AEM_HOST_SUFFIX=aem.page ./add-media-behavior.sh
(CloudFront origin custom headers can't reference Secrets Manager, so this value
lives in the distribution config).
A distribution that also sends whole file types straight to AEM (extension
behaviors such as *.xml, *.txt, or *.js targeting aem-media-origin)
catches /sitemap.xml with *.xml. The sitemap then skips the Lambda: private
pages stay listed and preview URLs keep their main--…aem.page host.
add-sitemap-behavior.sh fixes this by adding an
exact /sitemap.xml behavior, cloned from the default (Lambda) behavior, ahead
of every other pattern. CloudFront uses the first matching behavior, so other
.xml files still go straight to AEM. The script is idempotent and supports
DRY_RUN=1 and REVERT=1:
DRY_RUN=1 DIST_ID=<dist-id> ./add-sitemap-behavior.sh # preview the behavior order
DIST_ID=<dist-id> ./add-sitemap-behavior.shOnce the distribution is deployed, invalidate /sitemap.xml.
set-content-caching.sh adds a *.xml behavior, so
run this script after it. The behavior copies the default behavior's cache
policy at the time it runs, so rerun it whenever set-content-caching.sh
changes that policy (for example, switching between NO_CACHE=1 and cached).
A distribution with no extension behaviors doesn't need this.
HTML and JSON can differ by viewer: private pages, audience-* blocks, private
card metadata, private rows and columns of query indexes, and the private
entries of /sitemap.xml.
The spectrum-content policy keeps them apart:
- Cache key: the
spectrum_sessioncookie, plus the query params the Lambda uses (compact,limit,offset,sheet). Anonymous viewers (no cookie) share one entry. Other query strings are still forwarded but don't create new entries. - TTL: the policy follows the Lambda's
Cache-Control(DefaultTTL 0), so a response caches only when the Lambda allows it.
Every query index listed in PUBLIC_FILTER_PATHS receives the same filtering
and gated-validator handling as /query-index.json. This includes the web
roll-up, RSP, SWC, mobile roll-up, and patterns indexes. Anonymous responses
omit private rows and the audience, card-steward, card-design-resources,
and card-contact columns. Authenticated responses retain those values.
?compact=true projects either viewer's response to path and title.
/sitemap.xml URLs in <loc> and alternate links are rewritten from AEM
origins to the environment's public origin for all viewers. For anonymous
viewers, <url> entries are also removed when the query index marks the path
audience: private or the gate denies it (PRIVATE_DENY_*). The sitemap
carries no audience data, so the Lambda reads the full query index from AEM for
each anonymous sitemap request. A private page that isn't in the query index
can't be detected and stays listed (the page itself still 404s). Fails closed:
if the index can't be read, or the sitemap isn't a <urlset>, anonymous callers
get a 404. A sitemap index (<sitemapindex>) is not supported; its child
sitemaps would need their own entries in PUBLIC_FILTER_PATHS.
What the Lambda sends:
| Response | Cache-Control |
Cached by CloudFront |
|---|---|---|
Anonymous HTML, listed query indexes, and /sitemap.xml |
public, max-age=<ANON_CACHE_MAX_AGE>, s-maxage=<ANON_EDGE_MAX_AGE> |
Yes, one shared copy |
Authenticated HTML, JSON, and /sitemap.xml |
private, no-store |
No |
| Gate 404s and failed query-index or sitemap filtering | no-store |
No |
| Redirects that carry the viewer's query string | no-store |
No |
For authenticated requests the Lambda removes If-None-Match and
If-Modified-Since before calling AEM. A browser can still hold the anonymous
copy of a page from before sign-in; without this, AEM could answer 304 and the
browser would keep showing that copy.
Assets keep AEM's ETag, so CloudFront's post-TTL revalidation is a cheap
conditional 304. Anonymous pages and the listed query indexes get a
gated ETag instead (lib/etag.js): AEM's tag with a
--gate-v<N> suffix, and no Last-Modified. On revalidation the Lambda forwards
only tags carrying the current suffix (suffix stripped) and never
If-Modified-Since, so a 304 only ever confirms a copy this gate version
produced. Any other conditional — a copy cached before the gate existed, under an
older gate version, or an existence probe for a private page — is dropped, AEM
returns a full 200, and the gate runs again. An anonymous 304 that arrives
without a gated tag being forwarded is turned into a 404 (fail closed).
Range/If-Range are stripped for pages and the filtered paths, and an
anonymous page answered with any other 2xx (e.g. a 206 slice) is a 404: only
a whole 200 body can be gated.
Revalidation stays cheap for current copies. The filtered anonymous sitemap drops
its ETag and Last-Modified entirely and always fetches a full 200, because
its validators track the sitemap alone, not the query index that decides which
entries are removed.
⚠️ BumpGATE_ETAG_VERSIONon filtering-logic deploys. AEM's ETag tracks the page, not this Lambda's filtering/gating code. When you changefilterAudienceBlocks/isPrivateHtml/ the gate / the query-index filter, incrementGATE_ETAG_VERSIONin lib/etag.js. On revalidation, anonymous copies bearing older tags are re-fetched and re-filtered. This does not evict fresh cached responses. Clients and edges keep those copies until their TTL expires (s-maxageat the edge — a day on prod); for a security-relevant fix, also runaws cloudfront create-invalidation --distribution-id <id> --paths "/*"(this uses your role's permission, so it works in the klam-federated account).
Gate version 3 adds private card metadata in HTML and Markdown, plus query-index column filtering. It rejects version 2 validators so previously cached, unstripped responses must be fetched and filtered again. Invalidate existing shared copies when deploying this change; the version bump alone does not evict fresh cache entries.
⚠️ Test for leaks before changing prod. With a realspectrum_sessioncookie, confirm that anonymous pages and/query-index.jsonreturnX-Cache: Hiton repeat with private content removed, that authenticated requests are never aHit, and that a private page is a404for anonymous viewers and real content for authenticated ones.
- Browser copies after sign-in. Anonymous responses have a browser
max-age. After sign-in, the page reloads and is fetched fresh, but subresources the page fetches (such as/query-index.json) can come from the browser's anonymous copy until it expires. Sendingmax-age=0to browsers (withs-maxagefor CloudFront) andVary: Cookiewould close this. - Authenticated HTML and JSON aren't cached. Sharing one cached copy across
signed-in viewers would need CloudFront to verify the session cookie, which
means storing
SESSION_SECREToutside Secrets Manager.
With AEM push invalidation,
a publish purges CloudFront, so ANON_EDGE_MAX_AGE can be raised well above the
default. It only covers main--*.aem.live, so it doesn't apply to stage. Prod
(E3VFWCMFUVXVV) has it set up:
- Cache tags are on (
CacheTagConfig, headerx-amz-meta-cache-tag), set byset-content-caching.sh, so AEM can purge by tag instead of purging everything. Both the Lambda and AEM asset responses carry the header. - AEM calls CloudFront as the IAM user
invalidator(groupInvalidator), whichadobe.designalso uses. To check that publishes reach CloudFront, look in CloudTrail forCreateInvalidationevents frominvalidator. - The
cdn.prodconfig (host,type: cloudfront,distributionId,accessKeyId,secretAccessKey,tagInvalidationEnabled: true) is posted to the AEM config service.
The preview distribution is restricted to Adobe corporate VPN traffic with AWS
WAF. This is a network gate in front of the app's own IMS/DA auth: a request from
any other IP gets a 403 before it reaches the Lambda.
set-vpn-allowlist.sh manages the whole setup and is
safe to re-run. It has three phases:
- Preview (default): extract and print the CIDRs; change nothing.
APPLY=1: create or refresh the WAF IP set (adobe-vpn-egress, plusadobe-vpn-egress-v6when the source has IPv6) from the CIDRs.ENFORCE=1: create or update the Web ACL (preview-vpn-only, default action Block, one rule that allows the IP set), associate it with the preview distribution, and disable IPv6 on that distribution (KEEP_IPV6=1keeps IPv6 on).
Blocked viewers see vpn-required.html, which tells them
to connect to Adobe VPN, instead of CloudFront's generic 403 page. ENFORCE=1
uploads it as a WAF custom response body and sends it with a 403 and
Cache-Control: no-store, so the page doesn't stick around after the viewer
connects. To change the message, edit the file and re-run ENFORCE=1. Keep the
page self-contained (inline CSS, no images or scripts from the preview host,
since those requests are blocked too) and under WAF's 4 KB limit; the script
checks the size before calling AWS.
The CIDRs come from the IT-Network egress list,
git.corp.adobe.com/IT-Network/egress/blob/master/nets.json — the source of
truth for Adobe corporate egress. Download it (corp git needs your credentials,
so the script does not fetch it) and pass the path. Extraction is
schema-agnostic: every valid IPv4/IPv6 address or CIDR in the file is used, and
bare IPs become /32 or /128.
# preview only
NETS_FILE=~/Downloads/nets.json ./set-vpn-allowlist.sh
# first-time setup: write the IP set, create + associate the Web ACL, turn IPv6 off
APPLY=1 ENFORCE=1 NETS_FILE=~/Downloads/nets.json ./set-vpn-allowlist.sh
# later: refresh the CIDRs only (the Web ACL keeps pointing at the same IP set)
APPLY=1 NETS_FILE=~/Downloads/nets.json ./set-vpn-allowlist.shNotes:
- WAF for CloudFront is global — the script uses
--scope CLOUDFRONT --region us-east-1. Only the preview distribution is changed; prod is untouched. - IPv6 is disabled so every viewer presents an IPv4 address the allowlist can
match. The egress list is IPv4, so leaving IPv6 on would
403anyone arriving over it. UseKEEP_IPV6=1(with IPv6 ranges in the source) to keep it on. - True client IP: WAF matches the viewer's source IP — the VPN egress IP for your users — so no forwarded-header setup is needed.
- Overrides:
IPSET_NAME,WEB_ACL_NAME,DIST_DOMAIN,KEEP_IPV6,BLOCK_PAGE. - To lift the restriction: clear the distribution's
WebACLId(and re-enable IPv6 if you turned it off). - Cost: about $5/month per Web ACL, plus $1/rule and per-request charges.