@intentic/sandbox-contract 1.371.0 → 1.372.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.
@@ -6,14 +6,13 @@ export declare const webextSecretUrl: (sandboxUrl: string) => string;
6
6
  export declare const webextGrantsChangedUrl: (sandboxUrl: string) => string;
7
7
  export declare const WEBEXT_TICKET_TOOL = "type_ticket";
8
8
  export declare const WEBEXT_TICKET_TTL_MS = 60000;
9
- export declare const webextPairingCode: (pairing: {
9
+ export interface WebextPairing {
10
10
  readonly url: string;
11
11
  readonly token: string;
12
- }) => string;
13
- export declare const parseWebextPairingCode: (code: string) => {
14
- readonly url: string;
15
- readonly token: string;
16
- } | undefined;
12
+ readonly name?: string;
13
+ }
14
+ export declare const webextPairingCode: (pairing: WebextPairing) => string;
15
+ export declare const parseWebextPairingCode: (code: string) => WebextPairing | undefined;
17
16
  export declare const WEBEXT_PAIR_MESSAGE = "intentic:webext:pair";
18
17
  export declare const WEBEXT_PAIRED_MESSAGE = "intentic:webext:paired";
19
18
  //# sourceMappingURL=webext-links.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"webext-links.d.ts","sourceRoot":"","sources":["../../../src/protocol/doors/webext-links.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,gBAAgB,eAAgB,MAAM,KAAG,MAAyF,CAAC;AAIhJ,eAAO,MAAM,eAAe,eAAgB,MAAM,KAAG,MAAiE,CAAC;AAIvH,eAAO,MAAM,gBAAgB,eAAgB,MAAM,KAAG,MAAkE,CAAC;AAKzH,eAAO,MAAM,aAAa,eAAgB,MAAM,KAAG,MAA+D,CAAC;AAInH,eAAO,MAAM,eAAe,eAAgB,MAAM,KAAG,MAAiE,CAAC;AAGvH,eAAO,MAAM,sBAAsB,eAAgB,MAAM,KAAG,MAAyE,CAAC;AAKtI,eAAO,MAAM,kBAAkB,gBAAgB,CAAC;AAIhD,eAAO,MAAM,oBAAoB,QAAS,CAAC;AAQ3C,eAAO,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AC1D,eAAO,MAAM,qBAAqB,2BAA2B,CAAC"}
@@ -7,19 +7,29 @@ export const webextGrantsChangedUrl = (sandboxUrl) => `${sandboxUrl.replace(/\/$
7
7
  export const WEBEXT_TICKET_TOOL = "type_ticket";
8
8
  export const WEBEXT_TICKET_TTL_MS = 60_000;
9
9
  const PAIRING_PREFIX = "ixb1_";
10
- const base64url = (value) => btoa(value).replaceAll("+", "-").replaceAll("/", "_").replaceAll("=", "");
11
- export const webextPairingCode = (pairing) => `${PAIRING_PREFIX}${base64url(JSON.stringify({ url: pairing.url, token: pairing.token }))}`;
10
+ const NAME_LIMIT = 80;
11
+ const base64url = (value) => btoa(String.fromCodePoint(...new TextEncoder().encode(value)))
12
+ .replaceAll("+", "-")
13
+ .replaceAll("/", "_")
14
+ .replaceAll("=", "");
15
+ const fromBase64url = (value) => new TextDecoder().decode(Uint8Array.from(atob(value.replaceAll("-", "+").replaceAll("_", "/")), (char) => char.codePointAt(0) ?? 0));
16
+ export const webextPairingCode = (pairing) => {
17
+ const name = pairing.name?.trim().slice(0, NAME_LIMIT) ?? "";
18
+ const carried = name === "" ? { url: pairing.url, token: pairing.token } : { url: pairing.url, token: pairing.token, name };
19
+ return `${PAIRING_PREFIX}${base64url(JSON.stringify(carried))}`;
20
+ };
12
21
  export const parseWebextPairingCode = (code) => {
13
22
  const trimmed = code.trim();
14
23
  if (!trimmed.startsWith(PAIRING_PREFIX)) {
15
24
  return undefined;
16
25
  }
17
26
  try {
18
- const decoded = JSON.parse(atob(trimmed.slice(PAIRING_PREFIX.length).replaceAll("-", "+").replaceAll("_", "/")));
27
+ const decoded = JSON.parse(fromBase64url(trimmed.slice(PAIRING_PREFIX.length)));
19
28
  if (typeof decoded.url !== "string" || typeof decoded.token !== "string" || decoded.token === "" || !/^https?:\/\//.test(decoded.url)) {
20
29
  return undefined;
21
30
  }
22
- return { url: decoded.url, token: decoded.token };
31
+ const name = typeof decoded.name === "string" ? decoded.name.trim().slice(0, NAME_LIMIT) : "";
32
+ return name === "" ? { url: decoded.url, token: decoded.token } : { url: decoded.url, token: decoded.token, name };
23
33
  }
24
34
  catch {
25
35
  return undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"webext-links.js","sourceRoot":"","sources":["../../../src/protocol/doors/webext-links.ts"],"names":[],"mappings":"AAKA,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,UAAkB,EAAU,EAAE,CAAC,GAAG,UAAU,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,wBAAwB,CAAC;AAIhJ,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,UAAkB,EAAU,EAAE,CAAC,GAAG,UAAU,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,uBAAuB,CAAC;AAIvH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,UAAkB,EAAU,EAAE,CAAC,GAAG,UAAU,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,wBAAwB,CAAC;AAKzH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,UAAkB,EAAU,EAAE,CAAC,GAAG,UAAU,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,qBAAqB,CAAC;AAInH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,UAAkB,EAAU,EAAE,CAAC,GAAG,UAAU,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,uBAAuB,CAAC;AAGvH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,UAAkB,EAAU,EAAE,CAAC,GAAG,UAAU,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,+BAA+B,CAAC;AAKtI,MAAM,CAAC,MAAM,kBAAkB,GAAG,aAAa,CAAC;AAIhD,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAM,CAAC;AAI3C,MAAM,cAAc,GAAG,OAAO,CAAC;AAE/B,MAAM,SAAS,GAAG,CAAC,KAAa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@
1
1
  {
2
2
  "name": "@intentic/sandbox-contract",
3
- "version": "1.371.0",
3
+ "version": "1.372.0",
4
4
  "description": "oRPC wire contract for the intentic sandbox daemon, shared by the daemon and its browser client",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -362,10 +362,10 @@
362
362
  }
363
363
  },
364
364
  "dependencies": {
365
- "@intentic/base": "1.371.0",
366
- "@intentic/constants": "1.371.0",
367
- "@intentic/extension-manifest": "1.371.0",
368
- "@intentic/registry": "1.371.0",
365
+ "@intentic/base": "1.372.0",
366
+ "@intentic/constants": "1.372.0",
367
+ "@intentic/extension-manifest": "1.372.0",
368
+ "@intentic/registry": "1.372.0",
369
369
  "@orpc/contract": "1.16.1",
370
370
  "zod": "4.6.7"
371
371
  },
@@ -0,0 +1,28 @@
1
+ import { parseWebextPairingCode, webextPairingCode } from "./webext-links.js";
2
+
3
+ // The browser extension's pairing code. Since 2026-10-28 it may carry the sandbox's name, so an extension paired with
4
+ // several sandboxes lists each by what its owner calls it rather than by `sandbox-<hex>.<domain>`.
5
+
6
+ const SANDBOX_URL = "https://sandbox-82789f4106b4.example.dev";
7
+
8
+ test("a code carries the sandbox's name both ways, in any script", () => {
9
+ expect(parseWebextPairingCode(webextPairingCode({ url: SANDBOX_URL, token: "pair-1", name: "intentic" }))).toEqual({ url: SANDBOX_URL, token: "pair-1", name: "intentic" });
10
+ // btoa alone refuses anything outside Latin-1, which a name may well be.
11
+ expect(parseWebextPairingCode(webextPairingCode({ url: SANDBOX_URL, token: "pair-1", name: "Zażółć — 项目" }))?.name).toBe("Zażółć — 项目");
12
+ });
13
+
14
+ test("a code without a name is still a whole code, and so is one an older sandbox made", () => {
15
+ expect(parseWebextPairingCode(webextPairingCode({ url: SANDBOX_URL, token: "pair-1" }))).toEqual({ url: SANDBOX_URL, token: "pair-1" });
16
+ expect(parseWebextPairingCode(webextPairingCode({ url: SANDBOX_URL, token: "pair-1", name: " " }))).toEqual({ url: SANDBOX_URL, token: "pair-1" });
17
+ // What a sandbox before names wrote: base64url of {url, token} alone.
18
+ const older = `ixb1_${btoa(JSON.stringify({ url: SANDBOX_URL, token: "pair-1" })).replaceAll("+", "-").replaceAll("/", "_").replaceAll("=", "")}`;
19
+ expect(parseWebextPairingCode(older)).toEqual({ url: SANDBOX_URL, token: "pair-1" });
20
+ });
21
+
22
+ test("a name that is not a string is dropped and a long one is cut, while the code still pairs", () => {
23
+ const odd = (payload: object): string => `ixb1_${btoa(JSON.stringify(payload)).replaceAll("+", "-").replaceAll("/", "_").replaceAll("=", "")}`;
24
+ expect(parseWebextPairingCode(odd({ url: SANDBOX_URL, token: "pair-1", name: 7 }))).toEqual({ url: SANDBOX_URL, token: "pair-1" });
25
+ expect(parseWebextPairingCode(webextPairingCode({ url: SANDBOX_URL, token: "pair-1", name: "x".repeat(200) }))?.name).toHaveLength(80);
26
+ expect(parseWebextPairingCode(odd({ url: "ftp://nope", token: "pair-1" }))).toBeUndefined();
27
+ expect(parseWebextPairingCode("not a code")).toBeUndefined();
28
+ });
@@ -34,31 +34,55 @@ export const WEBEXT_TICKET_TOOL = "type_ticket";
34
34
  // has to be answered inside it, or the ticket it then presents has already lapsed.
35
35
  export const WEBEXT_TICKET_TTL_MS = 60_000;
36
36
 
37
- // One pasteable code, not two fields: base64url of {url, token}, since a popup gets one paste, not two boxes. `token`
38
- // is single-use and expires in ten minutes; the prefix versions the format.
37
+ // One pasteable code, not two fields: base64url of {url, token, name}, since a popup gets one paste, not two boxes.
38
+ // `token` is single-use and expires in ten minutes; the prefix versions the format. `name` (2026-10-28) is what the
39
+ // sandbox is called, so an extension paired with several can tell them apart by more than a hostname; optional both
40
+ // ways, since a code without it is still a whole code and an extension that predates it ignores it.
39
41
  const PAIRING_PREFIX = "ixb1_";
40
42
 
41
- const base64url = (value: string): string => btoa(value).replaceAll("+", "-").replaceAll("/", "_").replaceAll("=", "");
43
+ // Long enough for any sandbox's name; a longer one is cut rather than refused.
44
+ const NAME_LIMIT = 80;
42
45
 
43
- export const webextPairingCode = (pairing: { readonly url: string; readonly token: string }): string =>
44
- `${PAIRING_PREFIX}${base64url(JSON.stringify({ url: pairing.url, token: pairing.token }))}`;
46
+ export interface WebextPairing {
47
+ readonly url: string;
48
+ readonly token: string;
49
+ readonly name?: string;
50
+ }
51
+
52
+ // btoa takes Latin-1 only, so a name in any other script goes through UTF-8 first.
53
+ const base64url = (value: string): string =>
54
+ btoa(String.fromCodePoint(...new TextEncoder().encode(value)))
55
+ .replaceAll("+", "-")
56
+ .replaceAll("/", "_")
57
+ .replaceAll("=", "");
58
+
59
+ const fromBase64url = (value: string): string =>
60
+ new TextDecoder().decode(Uint8Array.from(atob(value.replaceAll("-", "+").replaceAll("_", "/")), (char) => char.codePointAt(0) ?? 0));
61
+
62
+ export const webextPairingCode = (pairing: WebextPairing): string => {
63
+ const name = pairing.name?.trim().slice(0, NAME_LIMIT) ?? "";
64
+ const carried = name === "" ? { url: pairing.url, token: pairing.token } : { url: pairing.url, token: pairing.token, name };
65
+ return `${PAIRING_PREFIX}${base64url(JSON.stringify(carried))}`;
66
+ };
45
67
 
46
68
  // The extension's side; undefined for anything not one of ours, so a bad paste is reported rather than attempted.
47
- export const parseWebextPairingCode = (code: string): { readonly url: string; readonly token: string } | undefined => {
69
+ export const parseWebextPairingCode = (code: string): WebextPairing | undefined => {
48
70
  const trimmed = code.trim();
49
71
  if (!trimmed.startsWith(PAIRING_PREFIX)) {
50
72
  return undefined;
51
73
  }
52
74
  try {
53
- const decoded = JSON.parse(atob(trimmed.slice(PAIRING_PREFIX.length).replaceAll("-", "+").replaceAll("_", "/"))) as {
75
+ const decoded = JSON.parse(fromBase64url(trimmed.slice(PAIRING_PREFIX.length))) as {
54
76
  url?: unknown;
55
77
  token?: unknown;
78
+ name?: unknown;
56
79
  };
57
80
  // Checked here (http(s) URL, non-empty token) rather than left to the WebSocket constructor to throw.
58
81
  if (typeof decoded.url !== "string" || typeof decoded.token !== "string" || decoded.token === "" || !/^https?:\/\//.test(decoded.url)) {
59
82
  return undefined;
60
83
  }
61
- return { url: decoded.url, token: decoded.token };
84
+ const name = typeof decoded.name === "string" ? decoded.name.trim().slice(0, NAME_LIMIT) : "";
85
+ return name === "" ? { url: decoded.url, token: decoded.token } : { url: decoded.url, token: decoded.token, name };
62
86
  } catch {
63
87
  return undefined;
64
88
  }