@simmalugnt-se/payload-consent 0.5.2 → 0.6.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.0
4
+
5
+ - `scanner.pages` sends the site's published pages from Payload with a scan, in place of its
6
+ sitemaps, so a site with search indexing off and empty sitemaps is scanned beyond its start page.
7
+ Needs a `consent-scanner` that takes `pages`; an older one ignores them and reads the sitemaps.
8
+
3
9
  ## 0.5.2
4
10
 
5
11
  - Adding a scan's finding to a service keeps what the scan could not see (`summary.problems`):
package/README.md CHANGED
@@ -124,10 +124,20 @@ consentPlugin({
124
124
  token: process.env.CONSENT_SCANNER_TOKEN,
125
125
  // Only when the scanner cannot reach serverURL, e.g. Docker beside a dev server:
126
126
  // callbackBaseUrl: 'http://host.docker.internal:3000',
127
+ // Optional: the pages to scan, in place of the site's sitemaps.
128
+ pages: [{ collection: 'pages', url: (doc) => (doc.slug === 'home' ? '/' : `/${doc.slug}`) }],
127
129
  },
128
130
  })
129
131
  ```
130
132
 
133
+ The scanner picks the pages to visit from the site's sitemaps. A site with search indexing off
134
+ often serves empty ones, and then only the start page is scanned. With `scanner.pages`, a scan
135
+ sends the published documents of those collections instead: read as a visitor would, in the
136
+ default locale, each turned into a path or address by `url` (return nothing to leave one out).
137
+ Paths are taken relative to the scanned address, and addresses on another origin are left out;
138
+ more than 500 are thinned evenly. The scanner picks from them as from one sitemap. Without the
139
+ option, or when no page is left, the scanner reads the sitemaps.
140
+
131
141
  Scan now scans the site's address (the field above the button, or `serverURL`) as a new visitor,
132
142
  after rejecting, after accepting and, with several optional categories, with each one alone. The
133
143
  plugin presets c15t's cookie for each state, so no click is needed. The scanner posts the result
package/dist/index.js CHANGED
@@ -39,6 +39,9 @@ export function consentPlugin(options = {}) {
39
39
  if (options.legalPages &&
40
40
  !config.collections?.some((collection) => collection.slug === options.legalPages?.collection))
41
41
  throw new Error(`Unknown legal pages collection: ${options.legalPages.collection}`);
42
+ for (const { collection } of options.scanner?.pages ?? [])
43
+ if (!config.collections?.some(({ slug }) => slug === collection))
44
+ throw new Error(`Unknown scanner pages collection: ${collection}`);
42
45
  // The site's own address spreads monthly scans over the month; the secret when it has none.
43
46
  const schedule = scanSchedule(config.serverURL || config.secret || CONSENT_GLOBAL);
44
47
  const usersSlug = config.admin?.user ?? "users";
package/dist/scans.d.ts CHANGED
@@ -9,6 +9,13 @@ import { type ConsentPluginOptions } from "./types.ts";
9
9
  * writes it, and the policy cookie, so the banner treats it as a choice already made.
10
10
  */
11
11
  export declare function scanStates(config: Pick<ConsentConfig, "categories" | "policyVersion">, now?: number): ScanState[];
12
+ /**
13
+ * The published pages of `scanner.pages` as addresses on the scanned site, read as a visitor would
14
+ * (`overrideAccess: false`) in the default locale. Paths are taken relative to `url`; addresses on
15
+ * another origin are left out, since the scanner refuses them. More than `MAX_SCAN_PAGES` are
16
+ * thinned evenly. `undefined` without any, so the scanner reads the sitemaps.
17
+ */
18
+ export declare function scanPages(payload: Payload, options: ConsentPluginOptions, url: string): Promise<string[] | undefined>;
12
19
  type Started = {
13
20
  id: number | string;
14
21
  } | {
package/dist/scans.js CHANGED
@@ -5,7 +5,7 @@ import { evaluateScan, scanProblems, } from "./evaluate.js";
5
5
  import { evidenceEndpoint } from "./evidence.js";
6
6
  import { notifyScan } from "./notify.js";
7
7
  import { readConsent, toConsentConfig, } from "./site-config.js";
8
- import { CONSENT_GLOBAL, CONSENT_SCAN_EVIDENCE, CONSENT_SCANS, CONSENT_SERVICES, POLICY_COOKIE, SCAN_TASK, } from "./types.js";
8
+ import { CONSENT_GLOBAL, CONSENT_SCAN_EVIDENCE, CONSENT_SCANS, CONSENT_SERVICES, MAX_SCAN_PAGES, POLICY_COOKIE, SCAN_TASK, } from "./types.js";
9
9
  /**
10
10
  * The states a scan runs: a new visitor, rejecting, accepting everything and, with more than one
11
11
  * optional category, each category on its own. Each state presets c15t's cookie as c15t 2.2.1
@@ -36,6 +36,49 @@ export function scanStates(config, now = Date.now()) {
36
36
  states.push(state(`only-${category}`, [category]));
37
37
  return states;
38
38
  }
39
+ /**
40
+ * The published pages of `scanner.pages` as addresses on the scanned site, read as a visitor would
41
+ * (`overrideAccess: false`) in the default locale. Paths are taken relative to `url`; addresses on
42
+ * another origin are left out, since the scanner refuses them. More than `MAX_SCAN_PAGES` are
43
+ * thinned evenly. `undefined` without any, so the scanner reads the sitemaps.
44
+ */
45
+ export async function scanPages(payload, options, url) {
46
+ const site = new URL(url);
47
+ const localization = payload.config.localization;
48
+ const locale = localization ? localization.defaultLocale : undefined;
49
+ const found = new Set();
50
+ for (const { collection, url: toUrl } of options.scanner?.pages ?? []) {
51
+ const drafts = Boolean(payload.config.collections.find(({ slug }) => slug === collection)?.versions?.drafts);
52
+ const { docs } = await payload.find({
53
+ collection: collection,
54
+ depth: 0,
55
+ draft: false,
56
+ pagination: false,
57
+ overrideAccess: false,
58
+ ...(drafts ? { where: { _status: { equals: "published" } } } : {}),
59
+ });
60
+ for (const doc of docs) {
61
+ let address;
62
+ try {
63
+ const path = toUrl(doc, locale);
64
+ if (!path)
65
+ continue;
66
+ address = new URL(path, site);
67
+ }
68
+ catch {
69
+ continue;
70
+ }
71
+ if (address.origin !== site.origin)
72
+ continue;
73
+ address.hash = "";
74
+ found.add(address.href);
75
+ }
76
+ }
77
+ const pages = [...found].sort();
78
+ if (pages.length <= MAX_SCAN_PAGES)
79
+ return pages.length ? pages : undefined;
80
+ return Array.from({ length: MAX_SCAN_PAGES }, (_, index) => pages[Math.round((index * (pages.length - 1)) / (MAX_SCAN_PAGES - 1))]);
81
+ }
39
82
  function sameToken(given, expected) {
40
83
  const a = Buffer.from(given);
41
84
  const b = Buffer.from(expected);
@@ -85,6 +128,11 @@ export async function startScan({ payload, options, url: given, trigger = "manua
85
128
  return { error: "Enter the site's address, starting with https://", status: 400 };
86
129
  const { doc, services } = await readConsent({ payload });
87
130
  const states = scanStates(toConsentConfig(doc, services, { locale: "en" }));
131
+ // A page list that cannot be read leaves the scanner to the sitemaps rather than stop the scan.
132
+ const pages = await scanPages(payload, options, url).catch((error) => {
133
+ payload.logger.error({ err: error, msg: "payload-consent: could not list the pages to scan" });
134
+ return undefined;
135
+ });
88
136
  const base = `${scanner.callbackBaseUrl ?? payload.config.serverURL}${payload.config.routes.api}/${CONSENT_SCANS}`;
89
137
  const response = await fetch(new URL("/scans", scanner.url), {
90
138
  method: "POST",
@@ -93,6 +141,7 @@ export async function startScan({ payload, options, url: given, trigger = "manua
93
141
  url,
94
142
  callbackUrl: `${base}/result`,
95
143
  ...(scanner.keepEvidence === 0 ? {} : { evidenceUrl: `${base}/evidence` }),
144
+ ...(pages ? { pages } : {}),
96
145
  states: states.map(({ id, cookies }) => ({ id, cookies })),
97
146
  }),
98
147
  signal: AbortSignal.timeout(20_000),
package/dist/types.d.ts CHANGED
@@ -54,6 +54,16 @@ export type ConsentPluginOptions = {
54
54
  * `consent-scan-evidence`. Defaults to 12; `0` asks the scanner for none.
55
55
  */
56
56
  keepEvidence?: number;
57
+ /**
58
+ * Collections whose published documents are the site's pages; `url` turns a document into its
59
+ * path or address, as in `legalPages`, or returns nothing to leave it out. A scan sends these
60
+ * pages to the scanner in place of the site's sitemaps, which a site with search indexing off
61
+ * leaves empty. Without it, the scanner reads the sitemaps.
62
+ */
63
+ pages?: {
64
+ collection: CollectionSlug;
65
+ url: (doc: Record<string, unknown>, locale: string | undefined) => string | null | undefined;
66
+ }[];
57
67
  };
58
68
  /** Change the services collection, for example to add hooks that revalidate the site's cache. */
59
69
  overrideServices?: (collection: CollectionConfig) => CollectionConfig;
@@ -75,3 +85,5 @@ export declare const POLICY_COOKIE = "c15t-policy";
75
85
  /** Three years, unless the host sets `records.retentionDays`. */
76
86
  export declare const DEFAULT_RETENTION_DAYS: number;
77
87
  export declare const DEFAULT_KEEP_EVIDENCE = 12;
88
+ /** The most pages a scan sends; the scanner takes up to 500. */
89
+ export declare const MAX_SCAN_PAGES = 500;
package/dist/types.js CHANGED
@@ -15,3 +15,5 @@ export const POLICY_COOKIE = "c15t-policy";
15
15
  /** Three years, unless the host sets `records.retentionDays`. */
16
16
  export const DEFAULT_RETENTION_DAYS = 3 * 365;
17
17
  export const DEFAULT_KEEP_EVIDENCE = 12;
18
+ /** The most pages a scan sends; the scanner takes up to 500. */
19
+ export const MAX_SCAN_PAGES = 500;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simmalugnt-se/payload-consent",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "Cookie consent for Payload sites: banner texts, services and scans in Admin",
5
5
  "keywords": [
6
6
  "payload",