@linxin666/dsh-client-ui-browser-view 0.4.19 → 0.4.21

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/lib/index.js CHANGED
@@ -1723,11 +1723,18 @@ function isLoopbackRequest(request) {
1723
1723
  * @module @linxin666/dsh-client-ui-browser-view/host/auth
1724
1724
  */
1725
1725
  /**
1726
- * Body text of the page DSH serves when a request carries neither the launch
1727
- * token nor a valid cookie. This is the one page the top-up reacts to.
1726
+ * Body text of the page DSH serves from its own token exchange when a request
1727
+ * carries neither the launch token nor a valid cookie.
1728
1728
  */
1729
1729
  const AUTH_CHALLENGE_TEXT = /dsh web authentication required/i;
1730
1730
  /**
1731
+ * Body of the bare refusal a post-#8266 Host answers when route admission
1732
+ * rejects a request before its resource runs. That cohort admits every route
1733
+ * as authenticated unless it declares otherwise, so a navigation to the site
1734
+ * root is refused here instead of reaching the friendly exchange page above.
1735
+ */
1736
+ const ADMISSION_REJECTION_TEXT = /^\s*unauthorized\s*$/i;
1737
+ /**
1731
1738
  * Script that reads the document's visible text. `document.body` is absent on
1732
1739
  * a document that has no body yet, and `innerText` needs one.
1733
1740
  */
@@ -1741,6 +1748,53 @@ function isAuthChallengeText(text) {
1741
1748
  return AUTH_CHALLENGE_TEXT.test(text);
1742
1749
  }
1743
1750
  /**
1751
+ * Whether a page's visible text is the bare refusal of route admission. The
1752
+ * body is generic — any local service can answer it — so a caller must also
1753
+ * know the page came from this host's own listener before trusting it.
1754
+ * @param text - `document.body.innerText` of the page that just loaded.
1755
+ * @returns true when the document is only the word `unauthorized`.
1756
+ */
1757
+ function isAdmissionRejectionText(text) {
1758
+ return ADMISSION_REJECTION_TEXT.test(text);
1759
+ }
1760
+ /**
1761
+ * Whether a URL names the same loopback listener as an origin: both
1762
+ * authorities loopback, the same protocol, and the same port. A different
1763
+ * port is a different local service, and a non-loopback host is never ours.
1764
+ * @param url - the URL a navigation was asked to open.
1765
+ * @param origin - the host's own listener origin.
1766
+ * @returns true when the two name one loopback listener.
1767
+ */
1768
+ function isSameLoopbackOrigin(url, origin) {
1769
+ let target;
1770
+ let own;
1771
+ try {
1772
+ target = new URL(url);
1773
+ own = new URL(origin);
1774
+ } catch {
1775
+ return false;
1776
+ }
1777
+ if (target.protocol !== "http:" && target.protocol !== "https:") return false;
1778
+ if (target.protocol !== own.protocol) return false;
1779
+ if (!isLoopbackHostname(target.hostname) || !isLoopbackHostname(own.hostname)) return false;
1780
+ if (target.port !== own.port) return false;
1781
+ return target.hostname === own.hostname || target.hostname === "localhost";
1782
+ }
1783
+ /**
1784
+ * Whether a URL names the site root. DSH's token exchange answers only
1785
+ * `GET /` with the cookie-setting redirect, so a bare refusal anywhere else
1786
+ * cannot be cleared by a ticketed retry.
1787
+ * @param url - the URL a navigation was asked to open.
1788
+ * @returns true when the URL has no path beyond the root.
1789
+ */
1790
+ function isHostRootUrl(url) {
1791
+ try {
1792
+ return new URL(url).pathname === "/";
1793
+ } catch {
1794
+ return false;
1795
+ }
1796
+ }
1797
+ /**
1744
1798
  * Whether a navigation target is one this DSH process serves on loopback: a
1745
1799
  * `localhost` name, 127/8, or `[::1]`. A remote site can render any text it
1746
1800
  * likes, so only a loopback authority is worth a token retry.
@@ -2261,6 +2315,7 @@ var BrowserViewService = class {
2261
2315
  openSession;
2262
2316
  resolveConfig;
2263
2317
  authenticatedUrl;
2318
+ hostOrigin;
2264
2319
  idleCloseDays;
2265
2320
  sweepIntervalMs;
2266
2321
  idleTimer;
@@ -2304,6 +2359,7 @@ var BrowserViewService = class {
2304
2359
  this.openSession = options.openSession ?? openConfiguredSession;
2305
2360
  this.resolveConfig = options.resolveConfig ?? (() => ({ mode: "isolated" }));
2306
2361
  this.authenticatedUrl = options.authenticatedUrl;
2362
+ this.hostOrigin = options.hostOrigin;
2307
2363
  this.idleCloseDays = options.idleCloseDays ?? (() => 0);
2308
2364
  this.sweepIntervalMs = options.sweepIntervalMs ?? 6e5;
2309
2365
  if (this.sweepIntervalMs > 0) {
@@ -2672,7 +2728,7 @@ var BrowserViewService = class {
2672
2728
  const mint = this.authenticatedUrl;
2673
2729
  if (mint === void 0) return void 0;
2674
2730
  if (!isLoopbackTarget(url)) return void 0;
2675
- if (!await this.documentShowsAuthChallenge(tabId)) return void 0;
2731
+ if (!await this.documentShowsAuthChallenge(tabId, url)) return void 0;
2676
2732
  try {
2677
2733
  const ticket = mint(url);
2678
2734
  return typeof ticket === "string" && ticket !== "" ? ticket : void 0;
@@ -2681,20 +2737,28 @@ var BrowserViewService = class {
2681
2737
  }
2682
2738
  }
2683
2739
  /**
2684
- * Whether the tab's current document is DSH's authentication page. Reading
2685
- * the document is best-effort: a tab that refuses the script, or reports
2686
- * something other than text, simply does not match.
2740
+ * Whether the tab's current document is DSH's authentication page. Two
2741
+ * pages count: the token exchange's own sentence, and the bare
2742
+ * `unauthorized` a post-#8266 Host answers when route admission refuses the
2743
+ * site root before that exchange runs. The bare refusal is generic to every
2744
+ * local service, so it only counts when the requested URL names this host's
2745
+ * own listener.
2687
2746
  * @param tabId - tab to read.
2747
+ * @param url - the URL the caller asked to open.
2688
2748
  * @returns true when the page body is the challenge.
2689
2749
  */
2690
- async documentShowsAuthChallenge(tabId) {
2750
+ async documentShowsAuthChallenge(tabId, url) {
2691
2751
  try {
2692
2752
  const scope = this.scope();
2693
2753
  const text = (await scope.transport.send("Runtime.evaluate", {
2694
2754
  expression: DOCUMENT_TEXT_EXPRESSION,
2695
2755
  returnByValue: true
2696
2756
  }, scope.tabs.sessionOf(tabId))).result?.value;
2697
- return typeof text === "string" && isAuthChallengeText(text);
2757
+ if (typeof text !== "string") return false;
2758
+ if (isAuthChallengeText(text)) return true;
2759
+ if (!isAdmissionRejectionText(text)) return false;
2760
+ const origin = this.hostOrigin?.();
2761
+ return origin !== void 0 && isSameLoopbackOrigin(url, origin) && isHostRootUrl(url);
2698
2762
  } catch {
2699
2763
  return false;
2700
2764
  }
@@ -3293,6 +3357,7 @@ function makeRoutes(deps) {
3293
3357
  routes: [{
3294
3358
  kind: "exact",
3295
3359
  path: BROWSER_VIEW_STATE_PATH,
3360
+ auth: "anonymous",
3296
3361
  handler: (req, res) => {
3297
3362
  if (!isLoopbackRequest(req)) {
3298
3363
  writeJson(res, 403, { error: "forbidden: loopback-only" });
@@ -3307,6 +3372,7 @@ function makeRoutes(deps) {
3307
3372
  }],
3308
3373
  upgrade: {
3309
3374
  path: BROWSER_VIEW_SOCKET_PATH,
3375
+ auth: "anonymous",
3310
3376
  handler: (req, socket, head) => {
3311
3377
  if (!isLoopbackRequest(req)) {
3312
3378
  socket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\n\r\n");
@@ -8745,7 +8811,14 @@ function apply(ctx, config) {
8745
8811
  } : { mode: "isolated" };
8746
8812
  },
8747
8813
  idleCloseDays: () => effectiveConfig(config).idleCloseDays,
8748
- authenticatedUrl: (url) => resolveAuthenticatedUrl(ctx, url)
8814
+ authenticatedUrl: (url) => resolveAuthenticatedUrl(ctx, url),
8815
+ hostOrigin: () => {
8816
+ try {
8817
+ return ctx.webServer.getOrigin();
8818
+ } catch {
8819
+ return;
8820
+ }
8821
+ }
8749
8822
  });
8750
8823
  const saveImage = async (input) => {
8751
8824
  const attachments = ctx.get("attachments");
@@ -111,6 +111,8 @@ export interface NumberConstraints {
111
111
  integer?: boolean;
112
112
  /** The accepted value must be at least this. */
113
113
  min?: number;
114
+ /** The accepted value must be at most this. */
115
+ max?: number;
114
116
  }
115
117
  /** A whole- or decimal-number field. An empty draft clears the field; any other draft that is not a finite number within the constraints stays unwritten. */
116
118
  export declare function numberField(field: string, constraints?: NumberConstraints): FieldSpec;
@@ -42,6 +42,31 @@ export interface AuthenticatedUrlSource {
42
42
  * @returns true when the document is the challenge rather than a real page.
43
43
  */
44
44
  export declare function isAuthChallengeText(text: string): boolean;
45
+ /**
46
+ * Whether a page's visible text is the bare refusal of route admission. The
47
+ * body is generic — any local service can answer it — so a caller must also
48
+ * know the page came from this host's own listener before trusting it.
49
+ * @param text - `document.body.innerText` of the page that just loaded.
50
+ * @returns true when the document is only the word `unauthorized`.
51
+ */
52
+ export declare function isAdmissionRejectionText(text: string): boolean;
53
+ /**
54
+ * Whether a URL names the same loopback listener as an origin: both
55
+ * authorities loopback, the same protocol, and the same port. A different
56
+ * port is a different local service, and a non-loopback host is never ours.
57
+ * @param url - the URL a navigation was asked to open.
58
+ * @param origin - the host's own listener origin.
59
+ * @returns true when the two name one loopback listener.
60
+ */
61
+ export declare function isSameLoopbackOrigin(url: string, origin: string): boolean;
62
+ /**
63
+ * Whether a URL names the site root. DSH's token exchange answers only
64
+ * `GET /` with the cookie-setting redirect, so a bare refusal anywhere else
65
+ * cannot be cleared by a ticketed retry.
66
+ * @param url - the URL a navigation was asked to open.
67
+ * @returns true when the URL has no path beyond the root.
68
+ */
69
+ export declare function isHostRootUrl(url: string): boolean;
45
70
  /**
46
71
  * Whether a navigation target is one this DSH process serves on loopback: a
47
72
  * `localhost` name, 127/8, or `[::1]`. A remote site can render any text it
@@ -95,6 +95,12 @@ export interface BrowserViewServiceOptions {
95
95
  * the loader from `ctx.get('connection')`; absent here means no auth top-up.
96
96
  */
97
97
  authenticatedUrl?: (url: string) => string | undefined;
98
+ /**
99
+ * The running host's own listener origin, or undefined while it has not
100
+ * bound. It decides whether a bare `unauthorized` page came from this host
101
+ * rather than from another local service on its own port.
102
+ */
103
+ hostOrigin?: () => string | undefined;
98
104
  /** Live browser source; called for state and every new connection. */
99
105
  resolveConfig?: () => BrowserConnectionConfig;
100
106
  /**
@@ -143,6 +149,7 @@ export declare class BrowserViewService implements BrowserViewBackend {
143
149
  private readonly openSession;
144
150
  private readonly resolveConfig;
145
151
  private readonly authenticatedUrl;
152
+ private readonly hostOrigin;
146
153
  private readonly idleCloseDays;
147
154
  private readonly sweepIntervalMs;
148
155
  private idleTimer;
@@ -321,10 +328,14 @@ export declare class BrowserViewService implements BrowserViewBackend {
321
328
  */
322
329
  private authTicketUrl;
323
330
  /**
324
- * Whether the tab's current document is DSH's authentication page. Reading
325
- * the document is best-effort: a tab that refuses the script, or reports
326
- * something other than text, simply does not match.
331
+ * Whether the tab's current document is DSH's authentication page. Two
332
+ * pages count: the token exchange's own sentence, and the bare
333
+ * `unauthorized` a post-#8266 Host answers when route admission refuses the
334
+ * site root before that exchange runs. The bare refusal is generic to every
335
+ * local service, so it only counts when the requested URL names this host's
336
+ * own listener.
327
337
  * @param tabId - tab to read.
338
+ * @param url - the URL the caller asked to open.
328
339
  * @returns true when the page body is the challenge.
329
340
  */
330
341
  private documentShowsAuthChallenge;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@linxin666/dsh-client-ui-browser-view",
3
3
  "description": "Right-docked live browser view for the DSH Web GUI: a CDP-driven Chromium the agent operates, a screencast panel the user watches, and screenshots attached to every tool result.",
4
- "version": "0.4.19",
4
+ "version": "0.4.21",
5
5
  "icon": "icon.svg",
6
6
  "type": "module",
7
7
  "main": "lib/index.js",