Repository navigation
Expand file tree
/
Copy pathgate.js
More file actions
165 lines (152 loc) · 7.99 KB
/
Copy pathgate.js
File metadata and controls
165 lines (152 loc) · 7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Pure request-gating policy: given a request path, decides how an anonymous
* visitor is treated. No Request/Response/fetch here - index.js is the
* platform adapter that reads the session cookie and turns a verdict into a
* proxied response or a 404. Authenticated callers bypass this entirely.
*
* The posture is default-allow: a page-like path an anonymous visitor requests
* is served unless the page itself opts into privacy. A page opts in with
* <meta name="audience" content="private"> in its <head>; because that lives in
* the fetched HTML, index.js applies it *after* proxying (see isPrivateHtml,
* used by index.js's processHtmlResponse). Whole paths/prefixes can also be
* marked private up front here, with no fetch, via PRIVATE_DENY_EXACT/PREFIX.
*
* AEM also serves every page as head-less variants - `<page>.plain.html` (body
* markup only) and `<page>.md` (Markdown) - which carry no <head> to scan. For
* those, getCanonicalPagePath maps the variant back to its page, and index.js
* decides privacy from the canonical page's HTML before serving the variant.
*/
// Paths that are private up front, before any fetch - an anonymous visitor
// gets a 404 and no page is proxied. These win over every allow/gate rule
// below, and (unlike the meta gate) can protect non-HTML resources too. Add
// exact paths here and whole sections to PRIVATE_DENY_PREFIX.
export const PRIVATE_DENY_EXACT = [];
// Private sections, matched by prefix (startsWith). e.g. '/internal/'.
export const PRIVATE_DENY_PREFIX = [
'/drafts/'
];
// Exact-match public paths (compared whole, not by prefix). The site root and
// /404.html are listed so they are served as-is without a meta parse: the
// homepage renders for everyone (its own client code calls /auth/session to
// reveal logged-in content) and /404.html is fetched by CloudFront's custom
// error response to render the real 404 page for gated/missing paths.
export const PUBLIC_ALLOW_EXACT = [];
// Public resources served to anyone, matched by prefix (startsWith).
export const PUBLIC_ALLOW_PREFIX = [
// TODO: Review if there are any public paths that need to be explicitly allowed
//
// '/blocks/',
// '/templates/',
// '/deps/',
// '/styles/',
// '/scripts/',
// '/img/',
// '/fragments/nav/',
// '/fragments/public/',
// '/fragments/404',
// // Telemetry endpoints (RUM / OpTel) beacon before and without login, so
// // they must stay reachable for anonymous visitors.
// '/.rum/',
// '/.optel/',
// // Specific paths
// '/favicon.ico',
// '/robots.txt',
];
// Data resources served to an anonymous visitor only after a content filter.
// Classified 'filter' here; index.js proxies them and strips the private
// entries instead of serving them raw: private rows and columns in query indexes
// (see transformQueryIndex / lib/query-index.js), and the sitemap's private URLs
// (see transformSitemap / lib/sitemap.js).
//
// Matched whole, never by prefix, so every index target needs its own entry.
// An index left out of this list falls through to 'allow' and is served raw,
// keeping the private rows and columns that listed query indexes strip.
export const PUBLIC_FILTER_PATHS = [
'/query-index.json',
'/web/components/query-index.json',
'/web/rsp/components/query-index.json',
'/web/swc/components/query-index.json',
'/mobile/components/query-index.json',
// Patterns sit beside components rather than under them, so this index does
// uses /patterns/ rather than the component indexes' /components/ paths.
'/patterns/query-index.json',
'/sitemap.xml',
];
// Media is always public, matched loosely by "/media_" anywhere in the path -
// deliberately looser than index.js's isMediaRequest, whose strict hashed-
// filename regex serves a different (search-param) purpose.
const isPublicMedia = (pathname) => pathname.includes('/media_');
// Extension of the last path segment, lowercased ('' for none or a dotfile).
// Mirrors index.js's getExtension - kept here so gate.js stays self-contained.
const getExtension = (pathname) => {
const basename = pathname.split('/').pop();
const pos = basename.lastIndexOf('.');
return (basename === '' || pos < 1) ? '' : basename.slice(pos + 1).toLowerCase();
};
// Head-less page variants AEM serves alongside every page. Neither carries the
// page's <head> metadata, so privacy must be read from the canonical page.
// Matched case-sensitively: AEM only serves the lowercase forms (anything else
// is an AEM 404), and the canonical lookup must target the same page AEM would.
const PAGE_VARIANT_SUFFIXES = ['.plain.html', '.md'];
// Canonical page path for a head-less page variant, or null when the path is
// not one. `/a/b.plain.html` -> `/a/b`, `/a/b.md` -> `/a/b`, and a folder index
// (`/index.md`, `/a/index.plain.html`) -> the folder (`/`, `/a/`), which is how
// AEM addresses index pages.
export const getCanonicalPagePath = (pathname) => {
const suffix = PAGE_VARIANT_SUFFIXES.find((s) => pathname.endsWith(s));
if (!suffix) { return null; }
const base = pathname.slice(0, -suffix.length);
if (base === '' || base.endsWith('/')) { return base || '/'; }
if (base === '/index' || base.endsWith('/index')) { return base.slice(0, -'index'.length); }
return base;
};
// A "page" is an extensionless path, an .html path, or a page variant (.md) -
// the responses that render a page and so must honour its audience gate.
// Everything else (json, xml, ...) is data, served by the default-allow
// fallthrough unless explicitly private.
export const isPageLike = (pathname) => {
const ext = getExtension(pathname);
return ext === '' || ext === 'html' || getCanonicalPagePath(pathname) !== null;
};
// Verdict for an anonymous visitor:
// 'allow' - serve as-is (no meta parse)
// 'filter' - JSON proxied, then private rows stripped before serving
// 'gate' - page-like: proxy, then 404 only if the HTML opts into privacy
// (for a .plain.html/.md variant, the canonical page's HTML)
// 'deny' - 404 up front, indistinguishable from a path that does not exist
// Order matters: an explicitly-private path is denied before any allow/gate.
export const classifyPublicPath = (pathname) => {
if (PRIVATE_DENY_EXACT.includes(pathname)) { return 'deny'; }
if (PRIVATE_DENY_PREFIX.some((prefix) => pathname.startsWith(prefix))) { return 'deny'; }
if (isPublicMedia(pathname)) { return 'allow'; }
if (PUBLIC_ALLOW_EXACT.includes(pathname)) { return 'allow'; }
if (PUBLIC_ALLOW_PREFIX.some((prefix) => pathname.startsWith(prefix))) { return 'allow'; }
if (PUBLIC_FILTER_PATHS.includes(pathname)) { return 'filter'; }
if (isPageLike(pathname)) { return 'gate'; }
return 'allow';
};
// Pull one attribute's value out of a single <meta ...> tag, lowercased and
// trimmed. Handles double/single quoted and unquoted values; returns null when
// the attribute is absent. `name` is a literal ('name' | 'content'), so it is
// safe to interpolate into the pattern.
const getMetaAttr = (tag, name) => {
const match = tag.match(new RegExp(`\\b${name}\\s*=\\s*("([^"]*)"|'([^']*)'|([^"'\\s>]+))`, 'i'));
if (!match) { return null; }
return (match[2] ?? match[3] ?? match[4] ?? '').trim().toLowerCase();
};
// True when the page opts into privacy via <meta name="audience"
// content="private"> in its <head>. Pure - index.js reads the proxied HTML (or,
// for a head-less variant, the canonical page's HTML) and calls this. Never
// call it on a .plain.html body: with no <head> it would scan the whole body
// and miss the page's metadata. The scan is scoped to the <head> so a stray
// example of the tag in body content can't gate the page; attribute order,
// quoting, and case are all tolerated.
export const isPrivateHtml = (html) => {
if (typeof html !== 'string' || html === '') { return false; }
const headEnd = html.toLowerCase().indexOf('</head>');
const head = headEnd === -1 ? html : html.slice(0, headEnd);
const tags = head.match(/<meta\b[^>]*>/gi);
if (!tags) { return false; }
return tags.some((tag) => getMetaAttr(tag, 'name') === 'audience'
&& getMetaAttr(tag, 'content') === 'private');
};