@cookiemunch/sdk 0.21.1 → 0.23.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/dist/index.cjs CHANGED
@@ -37,6 +37,7 @@ __export(src_exports, {
37
37
  parseConsentValue: () => parseConsentValue,
38
38
  readConsent: () => readConsent,
39
39
  runExecutorConformance: () => runExecutorConformance,
40
+ secretPaths: () => secretPaths,
40
41
  verifyDecisionSignature: () => verifyDecisionSignature,
41
42
  verifyToken: () => verifyToken,
42
43
  verifyWebhookSignature: () => verifyWebhookSignature
@@ -279,6 +280,28 @@ var CONFORMANCE_RULES = [
279
280
  { id: "version.header", title: "Answers carry X-Version (the highest contract version the executor supports)." }
280
281
  ];
281
282
  var TEST_SUBJECT_RULE = { id: "lookup.test_subject.found", title: "A real test subject of yours (testSubjectEmail) is found: this rule alone reaches your own store through createRightsHandlers, which answers the canary itself." };
283
+ var EXPORT_RULE = { id: "requests.access.no_secrets", title: "An access request for your test subject (testSubjectEmail) finishes, and its export carries no credential: no password, password hash, secret, API key, token or private key, by field name or by shape." };
284
+ var SECRET_FIELD = /^(password|passwd|passwordhash|passhash|hashedpassword|secret|clientsecret|apikey|apisecret|accesstoken|refreshtoken|sessiontoken|idtoken|authtoken|privatekey|totpsecret|otpsecret|mfasecret|recoverycodes|salt)$/;
285
+ var SECRET_VALUE = /^(sk_(live|test)_|rk_(live|test)_|cmk_|ghp_|gho_|github_pat_|xox[abpr]-|AKIA[0-9A-Z]{16}|-----BEGIN [A-Z ]*PRIVATE KEY-----|\$2[aby]\$\d\d\$|\$argon2)/;
286
+ function secretPaths(v, path = "$", out = []) {
287
+ if (out.length >= 20) return out;
288
+ if (typeof v === "string") {
289
+ if (SECRET_VALUE.test(v.trim())) out.push(path);
290
+ return out;
291
+ }
292
+ if (Array.isArray(v)) {
293
+ v.forEach((x, i) => secretPaths(x, `${path}[${i}]`, out));
294
+ return out;
295
+ }
296
+ if (v && typeof v === "object") {
297
+ for (const [k, x] of Object.entries(v)) {
298
+ const p = `${path}.${k}`;
299
+ if (SECRET_FIELD.test(k.toLowerCase().replace(/[_-]/g, "")) && x !== null && x !== "" && x !== void 0) out.push(p);
300
+ else secretPaths(x, p, out);
301
+ }
302
+ }
303
+ return out;
304
+ }
282
305
  var ERASE_RULE = { id: "requests.erase.removes_subject", title: "A real erasure of your throwaway subject (eraseSubjectEmail) finishes, and a lookup no longer finds them: this rule alone runs your own erase code." };
283
306
  function randomTag() {
284
307
  const bytes = new Uint8Array(8);
@@ -315,7 +338,7 @@ async function runExecutorConformance(o) {
315
338
  const results = /* @__PURE__ */ new Map();
316
339
  let version;
317
340
  const set = (id, status, detail) => {
318
- const rule = [...CONFORMANCE_RULES, TEST_SUBJECT_RULE, ERASE_RULE].find((r) => r.id === id);
341
+ const rule = [...CONFORMANCE_RULES, TEST_SUBJECT_RULE, EXPORT_RULE, ERASE_RULE].find((r) => r.id === id);
319
342
  results.set(id, { id, title: rule.title, status, ...detail ? { detail } : {} });
320
343
  };
321
344
  const pass = (id) => set(id, "pass");
@@ -374,6 +397,9 @@ async function runExecutorConformance(o) {
374
397
  const seeded = await call("POST", "/lookup", { emails: [o.testSubjectEmail] }, { "Idempotency-Key": key("lookup-seeded") });
375
398
  if (seeded.status === 200 && field(seeded.json, "status") === "found") pass("lookup.test_subject.found");
376
399
  else fail("lookup.test_subject.found", `${o.testSubjectEmail}: ${said(seeded)}`);
400
+ const seededId = field(seeded.json, "subjectId");
401
+ if (typeof seededId === "string" && seededId) await exportRule(seededId);
402
+ else skip(EXPORT_RULE.id, "needs the test subject, which the lookup did not find");
377
403
  }
378
404
  const bothNames = (a) => a.status === 400 ? " A body carries `identity` and `atlas` together, identical, and `emails` can be empty: accept all of that (see the v2 contract)." : "";
379
405
  const byIdentity = await call("POST", "/lookup", { emails: [], identity, atlas: identity }, { "Idempotency-Key": key("lookup-identity") });
@@ -467,8 +493,42 @@ async function runExecutorConformance(o) {
467
493
  if (gone.status === 200 && (field(gone.json, "status") === "no_match" || st === "completed_with_retention")) pass(rule);
468
494
  else fail(rule, `the erasure said "${String(st)}", but a lookup afterwards: ${said(gone)}`);
469
495
  }
496
+ async function exportRule(subject) {
497
+ const rule = EXPORT_RULE.id;
498
+ const opened = await call("POST", "/requests", { subjectId: subject, type: "access", caseId: `conformance-access-${run}`, reason: "Cookie Munch conformance check (test subject export)" }, { "Idempotency-Key": key("access") });
499
+ const id = field(opened.json, "id");
500
+ if (opened.status !== 200 && opened.status !== 201 || typeof id !== "string") return fail(rule, `the access request was not accepted: ${said(opened)}`);
501
+ let answer = opened;
502
+ const deadline = Date.now() + (o.eraseTimeoutMs ?? 3e4);
503
+ while (["pending", "processing"].includes(String(field(answer.json, "status"))) && Date.now() < deadline) {
504
+ await new Promise((r) => setTimeout(r, 500));
505
+ answer = await call("GET", `/requests/${encodeURIComponent(id)}`, void 0, { "Idempotency-Key": key("access-get") });
506
+ }
507
+ const st = field(answer.json, "status");
508
+ if (st !== "completed" && st !== "completed_with_retention") return fail(rule, `the access request ended as "${String(st)}", not completed`);
509
+ let bundle = field(answer.json, "result");
510
+ const url2 = field(bundle, "downloadUrl");
511
+ if (typeof url2 === "string") {
512
+ try {
513
+ const res = await doFetch(url2, { headers: { accept: "application/json" } });
514
+ const text = await res.text();
515
+ if (text.length > 5e6) return fail(rule, "the export is over 5 MB");
516
+ try {
517
+ bundle = JSON.parse(text);
518
+ } catch {
519
+ bundle = text;
520
+ }
521
+ } catch (err) {
522
+ return fail(rule, `the export's downloadUrl could not be read: ${err instanceof Error ? err.message : String(err)}`);
523
+ }
524
+ }
525
+ if (bundle === void 0) return fail(rule, "the completed access request carried no result (inline, or { downloadUrl })");
526
+ const found = typeof bundle === "string" ? bundle.split(/\s+/).filter((w) => SECRET_VALUE.test(w)).map(() => "$ (text)") : secretPaths(bundle);
527
+ if (found.length) fail(rule, `the export carries what looks like a credential at ${[...new Set(found)].join(", ")}; leave credentials out of an access export`);
528
+ else pass(rule);
529
+ }
470
530
  function report() {
471
- const rules = [...CONFORMANCE_RULES, ...o.testSubjectEmail ? [TEST_SUBJECT_RULE] : [], ...o.eraseSubjectEmail ? [ERASE_RULE] : []].map((r) => results.get(r.id) ?? { id: r.id, title: r.title, status: "skip", detail: "not reached" });
531
+ const rules = [...CONFORMANCE_RULES, ...o.testSubjectEmail ? [TEST_SUBJECT_RULE, EXPORT_RULE] : [], ...o.eraseSubjectEmail ? [ERASE_RULE] : []].map((r) => results.get(r.id) ?? { id: r.id, title: r.title, status: "skip", detail: "not reached" });
472
532
  const count = (s) => rules.filter((r) => r.status === s).length;
473
533
  return { ok: count("fail") === 0, passed: count("pass"), failed: count("fail"), skipped: count("skip"), blocked: count("blocked"), rules, ...version ? { version } : {} };
474
534
  }
@@ -511,7 +571,7 @@ async function exchangeOperatorToken(input) {
511
571
  }
512
572
 
513
573
  // src/index.ts
514
- var SDK_VERSION = "0.21.1";
574
+ var SDK_VERSION = "0.23.0";
515
575
  var CookieMunchApiError = class extends Error {
516
576
  constructor(status, message, code, body) {
517
577
  super(message);
@@ -601,6 +661,7 @@ function createCookieMunch(opts) {
601
661
  orgs: {
602
662
  list: (o) => get(`/orgs${metaQs(o)}`),
603
663
  create: (name, opts2) => request("POST", "/orgs", { name, ...opts2 ?? {} }),
664
+ createDryRun: (name, opts2) => request("POST", "/orgs?dryRun=1", { name, ...opts2 ?? {} }),
604
665
  deleteSandbox: (id) => request("DELETE", `/orgs/${enc(id)}?hard=1`)
605
666
  },
606
667
  metadata: {
@@ -635,7 +696,7 @@ function createCookieMunch(opts) {
635
696
  ),
636
697
  lintConfig: (cbid, config, opts2) => request("POST", `/sites/${enc(cbid)}/config/lint${qs({ replace: opts2?.replace ? 1 : void 0 })}`, config),
637
698
  cookies: (cbid) => get(`/sites/${enc(cbid)}/cookies`),
638
- scan: (cbid, o) => request("POST", `/sites/${enc(cbid)}/scan${qs({ wait: o?.wait })}`),
699
+ scan: (cbid, o) => request("POST", `/sites/${enc(cbid)}/scan${qs({ wait: o?.wait })}`, o?.cookies !== void 0 ? { cookies: o.cookies } : void 0),
639
700
  scanStatus: (cbid, o) => get(`/sites/${enc(cbid)}/scan${qs({ wait: o?.wait })}`),
640
701
  waitForScan: async (cbid, o) => {
641
702
  const until = Date.now() + (o?.timeoutMs ?? 12e4);
@@ -652,7 +713,7 @@ function createCookieMunch(opts) {
652
713
  ab: (cbid) => get(`/sites/${enc(cbid)}/ab`),
653
714
  policy: (cbid, o) => request(
654
715
  "GET",
655
- `/sites/${enc(cbid)}/policy${qs({ contactEmail: o?.contactEmail, effectiveDate: o?.effectiveDate, jurisdictions: o?.jurisdictions?.join(","), fragment: o?.fragment, format: o?.format, lang: o?.lang })}`,
716
+ `/sites/${enc(cbid)}/policy${qs({ contactEmail: o?.contactEmail, effectiveDate: o?.effectiveDate, jurisdictions: o?.jurisdictions?.join(","), fragment: o?.fragment, format: o?.format, include: o?.include, lang: o?.lang })}`,
656
717
  void 0,
657
718
  true
658
719
  ),
@@ -863,6 +924,7 @@ function createCookieMunch(opts) {
863
924
  upsertExecutor: (input, o) => request("PUT", `/dsar/executors${qs({ verify: o?.verify === true ? "1" : o?.verify })}`, input),
864
925
  dryRunConnectExecutor: (input) => request("POST", "/dsar/executors?dryRun=1", input),
865
926
  executorPresets: () => get("/dsar/executors/presets"),
927
+ executorSuggestions: (cbid) => get(`/dsar/executors/suggestions${qs({ cbid })}`),
866
928
  upgradeExecutorPreset: (id, input = {}) => request("POST", `/dsar/executors/${enc(id)}/upgrade-preset`, input),
867
929
  dryRunUpgradeExecutorPreset: (id, input = {}) => request("POST", `/dsar/executors/${enc(id)}/upgrade-preset?dryRun=1`, input),
868
930
  dryRunUpdateExecutor: (id, patch) => request("PATCH", `/dsar/executors/${enc(id)}?dryRun=1`, patch),
@@ -879,6 +941,7 @@ function createCookieMunch(opts) {
879
941
  recheckExecutor: (id) => request("POST", `/dsar/executors/${enc(id)}/recheck`),
880
942
  executorHealthHistory: (id, opts2 = {}) => get(`/dsar/executors/${enc(id)}/health/history${qs({ limit: opts2.limit })}`),
881
943
  executorConformance: (id) => request("POST", `/dsar/executors/${enc(id)}/test`, { conformance: true }),
944
+ getExecutorCompletionSecret: (id) => request("GET", `/dsar/executors/${enc(id)}/completion-secret`),
882
945
  rollExecutorCompletionSecret: (id) => request("POST", `/dsar/executors/${enc(id)}/completion-secret/roll`)
883
946
  },
884
947
  preferences: {
@@ -963,7 +1026,7 @@ function createCookieMunch(opts) {
963
1026
  remove: () => request("DELETE", "/org/rights-identity")
964
1027
  }
965
1028
  },
966
- audit: (limit, opts2) => get(`/audit${qs({ limit, actor: opts2?.actor })}`),
1029
+ audit: (limit, opts2) => get(`/audit${qs({ limit, actor: opts2?.actor, since: opts2?.since })}`),
967
1030
  auditSink: {
968
1031
  get: () => get("/audit/sink"),
969
1032
  set: (input) => request("PUT", "/audit/sink", input),
@@ -1020,8 +1083,9 @@ function createCookieMunch(opts) {
1020
1083
  },
1021
1084
  rightsIdentity: {
1022
1085
  get: (cbid) => get(`/sites/${enc(cbid)}/rights-identity`),
1023
- set: (cbid, input) => request("PUT", `/sites/${enc(cbid)}/rights-identity`, input),
1024
- remove: (cbid) => request("DELETE", `/sites/${enc(cbid)}/rights-identity`)
1086
+ set: ((cbid, input, o) => request("PUT", `/sites/${enc(cbid)}/rights-identity${qs({ dryRun: o?.dryRun ? "1" : void 0 })}`, input)),
1087
+ remove: (cbid) => request("DELETE", `/sites/${enc(cbid)}/rights-identity`),
1088
+ check: (cbid, token) => request("POST", `/sites/${enc(cbid)}/rights-identity/check`, { token })
1025
1089
  },
1026
1090
  dsarSettings: {
1027
1091
  get: (cbid) => get(`/sites/${enc(cbid)}/dsar-settings`),
@@ -1067,6 +1131,7 @@ function createCookieMunch(opts) {
1067
1131
  parseConsentValue,
1068
1132
  readConsent,
1069
1133
  runExecutorConformance,
1134
+ secretPaths,
1070
1135
  verifyDecisionSignature,
1071
1136
  verifyToken,
1072
1137
  verifyWebhookSignature