@simmalugnt-se/payload-consent 0.5.2 → 0.6.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.1
4
+
5
+ - `scanner.pages` sends a list per collection, and the scanner picks from each as from one sitemap:
6
+ in one list, a large collection such as news took nearly every visited page from the others.
7
+ Needs `consent-scanner` at `fcf4a71` or later: the scanner 0.6.0 was made for (`eef412e`) refuses
8
+ lists, and an older one ignores them and reads the sitemaps.
9
+
10
+ ## 0.6.0
11
+
12
+ - `scanner.pages` sends the site's published pages from Payload with a scan, in place of its
13
+ sitemaps, so a site with search indexing off and empty sitemaps is scanned beyond its start page.
14
+ Needs a `consent-scanner` that takes `pages`; an older one ignores them and reads the sitemaps.
15
+
3
16
  ## 0.5.2
4
17
 
5
18
  - Adding a scan's finding to a service keeps what the scan could not see (`summary.problems`):
package/README.md CHANGED
@@ -124,10 +124,21 @@ 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
+ Each collection goes as its own list, and the scanner picks from each as from one sitemap, so a
139
+ thousand news articles do not crowd out the pages; the lists share at most 500 addresses, each
140
+ thinned evenly. Without the option, or when no page is left, the scanner reads the sitemaps.
141
+
131
142
  Scan now scans the site's address (the field above the button, or `serverURL`) as a new visitor,
132
143
  after rejecting, after accepting and, with several optional categories, with each one alone. The
133
144
  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,15 @@ 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`, a list per collection, as addresses on the scanned site,
14
+ * read as a visitor would (`overrideAccess: false`) in the default locale. The scanner picks from
15
+ * each list as from one sitemap, so a large collection does not crowd out a small one. Paths are
16
+ * taken relative to `url`; addresses on another origin are left out, since the scanner refuses
17
+ * them. The lists share `MAX_SCAN_PAGES`, each thinned evenly to its share. `undefined` without
18
+ * any page, so the scanner reads the sitemaps.
19
+ */
20
+ export declare function scanPages(payload: Payload, options: ConsentPluginOptions, url: string): Promise<string[][] | undefined>;
12
21
  type Started = {
13
22
  id: number | string;
14
23
  } | {
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,61 @@ 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`, a list per collection, as addresses on the scanned site,
41
+ * read as a visitor would (`overrideAccess: false`) in the default locale. The scanner picks from
42
+ * each list as from one sitemap, so a large collection does not crowd out a small one. Paths are
43
+ * taken relative to `url`; addresses on another origin are left out, since the scanner refuses
44
+ * them. The lists share `MAX_SCAN_PAGES`, each thinned evenly to its share. `undefined` without
45
+ * any page, so the scanner reads the sitemaps.
46
+ */
47
+ export async function scanPages(payload, options, url) {
48
+ const site = new URL(url);
49
+ const localization = payload.config.localization;
50
+ const locale = localization ? localization.defaultLocale : undefined;
51
+ const collections = options.scanner?.pages ?? [];
52
+ const share = Math.floor(MAX_SCAN_PAGES / Math.max(collections.length, 1));
53
+ const lists = [];
54
+ for (const { collection, url: toUrl } of collections) {
55
+ const drafts = Boolean(payload.config.collections.find(({ slug }) => slug === collection)?.versions?.drafts);
56
+ const { docs } = await payload.find({
57
+ collection: collection,
58
+ depth: 0,
59
+ draft: false,
60
+ pagination: false,
61
+ overrideAccess: false,
62
+ ...(drafts ? { where: { _status: { equals: "published" } } } : {}),
63
+ });
64
+ const found = new Set();
65
+ for (const doc of docs) {
66
+ let address;
67
+ try {
68
+ const path = toUrl(doc, locale);
69
+ if (!path)
70
+ continue;
71
+ address = new URL(path, site);
72
+ }
73
+ catch {
74
+ continue;
75
+ }
76
+ if (address.origin !== site.origin)
77
+ continue;
78
+ address.hash = "";
79
+ found.add(address.href);
80
+ }
81
+ const pages = [...found].sort();
82
+ if (pages.length)
83
+ lists.push(evenlySpaced(pages, share));
84
+ }
85
+ return lists.length ? lists : undefined;
86
+ }
87
+ function evenlySpaced(values, maximum) {
88
+ if (values.length <= maximum)
89
+ return values;
90
+ if (maximum === 1)
91
+ return values.slice(0, 1);
92
+ return Array.from({ length: maximum }, (_, index) => values[Math.round((index * (values.length - 1)) / (maximum - 1))]);
93
+ }
39
94
  function sameToken(given, expected) {
40
95
  const a = Buffer.from(given);
41
96
  const b = Buffer.from(expected);
@@ -85,6 +140,11 @@ export async function startScan({ payload, options, url: given, trigger = "manua
85
140
  return { error: "Enter the site's address, starting with https://", status: 400 };
86
141
  const { doc, services } = await readConsent({ payload });
87
142
  const states = scanStates(toConsentConfig(doc, services, { locale: "en" }));
143
+ // A page list that cannot be read leaves the scanner to the sitemaps rather than stop the scan.
144
+ const pages = await scanPages(payload, options, url).catch((error) => {
145
+ payload.logger.error({ err: error, msg: "payload-consent: could not list the pages to scan" });
146
+ return undefined;
147
+ });
88
148
  const base = `${scanner.callbackBaseUrl ?? payload.config.serverURL}${payload.config.routes.api}/${CONSENT_SCANS}`;
89
149
  const response = await fetch(new URL("/scans", scanner.url), {
90
150
  method: "POST",
@@ -93,6 +153,7 @@ export async function startScan({ payload, options, url: given, trigger = "manua
93
153
  url,
94
154
  callbackUrl: `${base}/result`,
95
155
  ...(scanner.keepEvidence === 0 ? {} : { evidenceUrl: `${base}/evidence` }),
156
+ ...(pages ? { pages } : {}),
96
157
  states: states.map(({ id, cookies }) => ({ id, cookies })),
97
158
  }),
98
159
  signal: AbortSignal.timeout(20_000),
package/dist/types.d.ts CHANGED
@@ -54,6 +54,17 @@ 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, and the scanner picks from each collection as from one sitemap. Without it,
62
+ * the scanner reads the sitemaps.
63
+ */
64
+ pages?: {
65
+ collection: CollectionSlug;
66
+ url: (doc: Record<string, unknown>, locale: string | undefined) => string | null | undefined;
67
+ }[];
57
68
  };
58
69
  /** Change the services collection, for example to add hooks that revalidate the site's cache. */
59
70
  overrideServices?: (collection: CollectionConfig) => CollectionConfig;
@@ -75,3 +86,5 @@ export declare const POLICY_COOKIE = "c15t-policy";
75
86
  /** Three years, unless the host sets `records.retentionDays`. */
76
87
  export declare const DEFAULT_RETENTION_DAYS: number;
77
88
  export declare const DEFAULT_KEEP_EVIDENCE = 12;
89
+ /** The most pages a scan sends, in all its lists; the scanner takes up to 500. */
90
+ 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, in all its lists; 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.1",
4
4
  "description": "Cookie consent for Payload sites: banner texts, services and scans in Admin",
5
5
  "keywords": [
6
6
  "payload",