@gitsang/dsh-gateway 0.0.0-stage → 0.3.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,373 @@
1
- # Temporary Holding Version
1
+ # dsh-gateway
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ An installable gateway for the
4
+ [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) web UI.
5
+
6
+ The dsh web server itself is left untouched. This bundle adds **one** row to
7
+ the `web` profile: a gateway that starts beside `dsh web`, reverse proxies HTTP
8
+ and WebSocket traffic to it, relays dsh's own token authentication, and can
9
+ deliver the page as an installable PWA.
10
+
11
+ Authentication is **dsh's**, not the gateway's. The gateway owns no password,
12
+ no session store and no credential of its own: it forwards the signed cookie
13
+ dsh minted and lets dsh decide.
14
+
15
+ ## Why "gateway"
16
+
17
+ The bundle owns the browser-facing access layer — reverse proxying, loopback
18
+ identity for upstream plugins, credential relay, PWA assets, service-worker
19
+ caching, and Host-mode injection — while dsh keeps owning identity. A gateway
20
+ describes that boundary.
21
+
22
+ The package, bundle row, environment prefix and data directory all use
23
+ `dsh-gateway` / `gateway`.
24
+
25
+ ## How authentication works
26
+
27
+ dsh mints one random launch token per process and prints (and usually opens) its
28
+ application URL with `?token=…`. `GET /?token=…` exchanges that token for a
29
+ signed browser cookie and redirects to the clean `./`. The cookie is:
30
+
31
+ - named `dsh-auth-<base64url sha256 of the request authority>`,
32
+ - bound to the authority (hostname + port, plus scheme on TLS) it was minted for,
33
+ - `HttpOnly; SameSite=Strict; Path=/`, valid 30 days by default,
34
+ - `Secure` only when the listener that received the request serves TLS.
35
+
36
+ The gateway relays that exchange instead of replacing it:
37
+
38
+ 1. A browser opens the **gateway** URL printed at startup
39
+ (`http://127.0.0.1:3081/?token=…`).
40
+ 2. The gateway forwards the request to dsh with `Host` rewritten to the
41
+ loopback upstream, so dsh mints the cookie for `127.0.0.1:<dsh port>` — the
42
+ same authority every later proxied request presents.
43
+ 3. dsh answers `303` with `Set-Cookie: dsh-auth-…`; the gateway relays it,
44
+ adding `Secure` when the browser-facing hop is HTTPS (see below).
45
+ 4. Every subsequent HTTP request and WebSocket upgrade carries that cookie.
46
+ The gateway forwards it **unchanged** to dsh, which accepts or rejects it.
47
+
48
+ There is no gateway login page and no gateway session. If a browser shows up
49
+ without a valid cookie, dsh answers `401`, and the gateway turns that into a
50
+ short explanation page for top-level navigations instead of relaying dsh's
51
+ bare one-line text. API traffic keeps dsh's own `401`/`403`.
52
+
53
+ ![gateway sign-in required page](docs/sign-in-required.png)
54
+
55
+ ### Signing in
56
+
57
+ Opening the plain gateway origin does nothing useful until a browser holds a
58
+ cookie. Sign in with the URL the gateway prints:
59
+
60
+ ```
61
+ dsh-gateway: http://127.0.0.1:3081 -> http://127.0.0.1:3080
62
+ dsh-gateway: sign in with http://127.0.0.1:3081/?token=…
63
+ ```
64
+
65
+ That URL is derived from dsh's own launch token, so it is only as secret as
66
+ dsh's startup line. The token changes every time `dsh web` restarts; after a
67
+ restart an existing browser keeps working while its cookie is valid, but a
68
+ **new** browser needs the new URL.
69
+
70
+ ### Signing out
71
+
72
+ dsh documents no logout operation — "clearing the browser cookie ends one
73
+ browser session". Because dsh's cookie is `HttpOnly`, page JavaScript cannot
74
+ clear it, so the gateway's configuration page offers **退出登录**: it writes an
75
+ expired `Max-Age=0` copy of the exact cookie name for the forwarded authority,
76
+ drops the offline caches, and reloads.
77
+
78
+ Revoking *every* session is dsh's own operation, not the gateway's:
79
+
80
+ ```bash
81
+ # delete the owner-scoped browser-session grant from $DSH_HOME/.credentials.yaml
82
+ # (or replace it), then restart dsh
83
+ ```
84
+
85
+ ## How it works
86
+
87
+ - Once a request reaches the gateway, it is proxied to dsh's loopback port.
88
+ `Host` and `Origin` are rewritten to `http://127.0.0.1:<dsh web port>`, and
89
+ `Referer` is translated to match, so dsh and plugins that require a local
90
+ host treat traffic through this gateway as local without any
91
+ Traefik/nginx header middleware. `Sec-Fetch-Site` is left untouched so
92
+ cross-site requests are still rejected by dsh's CSRF fence.
93
+ - The browser's `Cookie` header is forwarded as-is, minus the obsolete
94
+ `dsh_gateway_session` cookie from the removed password gate. Every
95
+ `dsh-auth-*` cookie the browser holds reaches dsh untouched, including the
96
+ one minted for the loopback authority.
97
+ - Response cookies are relayed as dsh sent them. The only edit is adding
98
+ `Secure` to the `dsh-auth-*` cookie when the browser-facing hop is HTTPS: dsh
99
+ marks that cookie `Secure` only when *its own* listener terminates TLS, and a
100
+ gateway behind a TLS proxy speaks plain HTTP upstream. Without this the
101
+ browser would hold a non-`Secure` bearer cookie on an HTTPS origin.
102
+ - Absolute `Location` headers pointing at the loopback upstream are translated
103
+ back to the public origin.
104
+ - By default the gateway also serves PWA assets (`/manifest.webmanifest`,
105
+ `/sw.js`, `/favicon.svg`) and injects the manifest link plus a caching
106
+ service-worker registration into HTML pages. The service worker keeps a
107
+ network-first app shell and a cache-first store for content-addressed
108
+ `/assets/*` and `/plugins/*?rev=…` bundles, so repeat and weak-network loads
109
+ do not refetch unchanged JavaScript and CSS. API traffic, the gateway's own
110
+ settings API, dsh's token exchange and WebSocket upgrades are never cached,
111
+ and a navigation that comes back `401` drops the cached shell so signing out
112
+ does not leave the authenticated document available offline. The manifest
113
+ references the SVG icon only. This makes the gateway origin installable as a
114
+ standalone web app on `localhost`/HTTPS.
115
+
116
+ On first run the gateway creates a `~/.dsh/profiles/gateway/.gitignore` keeping
117
+ `settings.json` out of git.
118
+
119
+ ## Gateway configuration page
120
+
121
+ The bundle keeps **one row** for the whole gateway. PWA and Host mode are not
122
+ qol-style independently loadable components: they share the proxy lifecycle,
123
+ and forwarding must never become an independently switchable item. Instead,
124
+ the features and their status live on the row's own page under
125
+ **Plugins → @gitsang/dsh-gateway → Gateway → Configure**. Nothing is injected
126
+ into Settings.
127
+
128
+ The page contains:
129
+
130
+ - **浏览器体验** — PWA switch and a button that clears the gateway's offline
131
+ caches.
132
+ - **兼容性** — Host mode switch, with the security warning and restart
133
+ requirement stated inline.
134
+ - **访问控制** — the authentication provider and exact cookie name, the
135
+ sign-in hint, and a **退出登录** button that expires this browser's cookie.
136
+ - **运行状态** — listen address, upstream dsh, uptime, version and data
137
+ directory.
138
+
139
+ ![gateway configuration page](docs/gateway-config.png)
140
+
141
+ Saving the form stages both switches and writes on **Save**. PWA changes take
142
+ effect on the next page request. Host mode requires a restart of `dsh web`,
143
+ because dsh's index injection is composed during boot. Settings saved on this
144
+ page live in `settings.json`; they deliberately do not use dsh's own settings
145
+ store, because turning Host mode off removes the browser's Host-settings
146
+ transport and the gateway page must still be able to turn it back on.
147
+
148
+ Environment variables win over the configuration page. When a toggle is managed
149
+ by an environment variable, the page shows it as read-only.
150
+
151
+ The settings API (`/__dsh_gateway/*`) is authenticated by the very cookie dsh
152
+ minted: the gateway validates the caller with one loopback `HEAD /` carrying the
153
+ browser's cookie, and dsh answers `200` only for an unexpired, correctly signed
154
+ cookie for the forwarded authority. Mutating requests additionally require a
155
+ same-origin `x-dsh-gateway: 1` header.
156
+
157
+ ## Install
158
+
159
+ Add the bundle to the existing `web` profile:
160
+
161
+ ```bash
162
+ dsh plugin --profile web add github:gitsang/dsh-gateway
163
+ ```
164
+
165
+ Then start dsh web as usual:
166
+
167
+ ```bash
168
+ dsh web
169
+ ```
170
+
171
+ The dsh web server still listens on `127.0.0.1:3080`; the gateway listens
172
+ on the dsh web port plus one (`127.0.0.1:3081` for the default dsh web port).
173
+ Open the sign-in URL the gateway prints, not the bare origin.
174
+
175
+ ### Upgrading from 0.2 (password gate)
176
+
177
+ Version 0.3 removes the password gate. There is nothing to migrate:
178
+
179
+ - `auth.json` and `session.json` in the data directory are no longer read. Delete
180
+ them if you like; they contain a now-unused bcrypt hash and dead session
181
+ tokens.
182
+ - Any browser holding the old `dsh_gateway_session` cookie is signed out by
183
+ that change; the gateway also strips that cookie instead of forwarding it.
184
+ - `DSH_GATEWAY_TTL_MS` is gone (the session lifetime is dsh's
185
+ `cookieMaxAgeDays`, 30 days).
186
+ - Open the sign-in URL printed at the next `dsh web` startup to sign in again.
187
+
188
+ ## Configuration
189
+
190
+ All configuration is optional and read from environment variables at boot.
191
+
192
+ | Variable | Default | Meaning |
193
+ | ----------------------- | ------------------------- | ----------------------------------------------------- |
194
+ | `DSH_GATEWAY_HOST` | `127.0.0.1` | Gateway bind host |
195
+ | `DSH_GATEWAY_PORT` | `dsh web port + 1` | Gateway listen port (overrides the default) |
196
+ | `DSH_GATEWAY_DATA_DIR` | `~/.dsh/profiles/gateway` | Directory for gateway settings |
197
+ | `DSH_GATEWAY_PWA` | `1` | Serve PWA assets and inject PWA tags into HTML |
198
+ | `DSH_GATEWAY_HOST_MODE` | `1` | Declare the page a host-owning page (Host settings) |
199
+ | `DSH_HOME` | `~/.dsh` | dsh home (used when `DSH_GATEWAY_DATA_DIR` is unset) |
200
+
201
+ ### PWA (installable web app)
202
+
203
+ PWA injection is enabled by default. The gateway serves the manifest, service
204
+ worker and `/favicon.svg` itself, and injects the required tags into the 401
205
+ page and proxied HTML responses. Toggle it on the gateway's **Configure** page, or set
206
+ `DSH_GATEWAY_PWA=0` to lock it off from the environment.
207
+
208
+ The service worker uses two caches:
209
+
210
+ - `dsh-gateway-shell-v1` holds the last authenticated HTML document plus the
211
+ manifest and icon. A navigation is network-first; when the network is slower
212
+ than three seconds or unavailable, the cached shell is shown. A successful
213
+ navigation replaces it, so a changed deployment is picked up on the next
214
+ load. A `401` navigation clears the cache.
215
+ - `dsh-gateway-assets-v1` is cache-first for immutable resources:
216
+ Vite's hashed `/assets/*` files and dsh's `/plugins/*?rev=…` bundles. A
217
+ new build produces a new URL, so unchanged resources stay cached while
218
+ changed resources are fetched once. The cache is trimmed to the newest 300
219
+ entries.
220
+
221
+ `/api/*`, `/__dsh_gateway/*`, `/sw.js`, range requests and non-GET requests
222
+ bypass the service worker. Responses that redirect (for example dsh's token
223
+ exchange), fail, or carry `Cache-Control: no-store` are not stored.
224
+
225
+ When PWA is disabled, `/sw.js` serves a cleanup worker that deletes the
226
+ gateway caches and unregisters itself, so an already-installed worker does not
227
+ keep serving an old offline shell.
228
+
229
+ The gateway also adds HTTP cache headers for clients that do not run a service
230
+ worker: hashed assets are
231
+ `Cache-Control: public, max-age=31536000, immutable`, HTML is
232
+ `private, no-cache` (plus `Vary: Cookie`), and `/api/*` is `no-store`.
233
+
234
+ Browser install prompts require a secure context. `http://127.0.0.1:3081` and
235
+ `http://localhost:3081` qualify; an exposed `http://192.168.x.x:3081` LAN URL
236
+ does **not**. For LAN installability, terminate HTTPS in front of the gateway
237
+ with a certificate the browser trusts.
238
+
239
+ ### Host mode (settings, Models page)
240
+
241
+ dsh only lets a page read and write Host settings when it believes it is the
242
+ owning host. Its client decides that with
243
+ `ctx.remote.$host.isLoopback`, which is true for a loopback hostname, or when
244
+ the page declares `globalThis.__DSH_TRANSPORT__ = { ownsHost: true }` — the
245
+ same flag the Desktop shell sets.
246
+
247
+ A page served through this gateway is never on a loopback hostname, so without
248
+ that flag dsh falls back to its process-local `memory` settings transport. The
249
+ visible symptom is **Settings → Models: "Loading the provider directory
250
+ failed: settings are unavailable in this browser"**, and no settings change
251
+ survives a reload.
252
+
253
+ Host mode carries the flag two ways:
254
+
255
+ 1. **Primary — native index injection.** The plugin pushes a
256
+ `webserver/index-inject` row, so dsh itself renders
257
+
258
+ ```html
259
+ <script>globalThis["__DSH_TRANSPORT__"] = {"ownsHost":true}</script>
260
+ ```
261
+
262
+ into its own `index.html`. This survives gzip, caching reverse proxies and
263
+ any hop that rewrites or re-serves our responses.
264
+
265
+ 2. **Fallback — head rewrite.** The gateway also patches the tag into HTML it
266
+ relays, which keeps working if some deployment serves the page from
267
+ somewhere else.
268
+
269
+ It is **on by default**, because a remote dsh web UI is otherwise unable to
270
+ edit providers. Toggle it on the gateway's **Configure** page, or set
271
+ `DSH_GATEWAY_HOST_MODE=0`. A change requires restarting `dsh web`.
272
+
273
+ Injection needs the upstream HTML uncompressed, so for HTML navigation
274
+ requests only (`Accept: text/html`) the gateway drops `Accept-Encoding`; scripts,
275
+ styles and API payloads keep their compression. The same rewrite path is what
276
+ makes the PWA tags work in a real browser.
277
+
278
+ ### Loopback identity for upstream plugins
279
+
280
+ The gateway presents every request to dsh as a local request. This is
281
+ deliberate: dsh's `/api` trust fence and many plugins special-case loopback
282
+ hosts, and a remote HTTPS reverse proxy would otherwise make them reject an
283
+ otherwise valid request.
284
+
285
+ The translation applies to ordinary HTTP and WebSocket upgrades:
286
+
287
+ | Browser sends | Upstream receives |
288
+ | -------------------------------------- | ----------------------------------------- |
289
+ | `Host: dsh.example.com` | `Host: 127.0.0.1:3080` |
290
+ | `Origin: https://dsh.example.com` | `Origin: http://127.0.0.1:3080` |
291
+ | `Referer: https://dsh.example.com/...` | `Referer: http://127.0.0.1:3080/...` |
292
+
293
+ The dsh session cookie is bound to that same rewritten authority, so `/api` RPC
294
+ and WebSocket streams work through the proxy without any extra header
295
+ middleware.
296
+
297
+ This assumes the gateway-to-dsh hop is plain HTTP on loopback, which is what
298
+ `dsh web` serves by default. If you run `dsh web --tls-cert … --tls-key …`, dsh
299
+ binds its cookie to the `https:` audience and a plain-HTTP upstream request
300
+ cannot satisfy it; terminate TLS at the gateway/front proxy instead and leave
301
+ dsh's own listener on HTTP.
302
+
303
+ Because of this, reverse-proxy header middleware is no longer needed. For
304
+ example, a Traefik router can point straight at the gateway and omit the old
305
+ `Host`/`Origin` injection:
306
+
307
+ ```yaml
308
+ http:
309
+ routers:
310
+ to-dsh:
311
+ rule: Host(`dsh.example.com`)
312
+ tls:
313
+ certResolver: aliResolver
314
+ service: dsh
315
+
316
+ services:
317
+ dsh:
318
+ loadBalancer:
319
+ servers:
320
+ - url: "http://127.0.0.1:3081"
321
+ ```
322
+
323
+ Do **not** apply this translation to a route that bypasses dsh-gateway (for
324
+ example a raw `:3080` route): that would hand any traffic the same local
325
+ identity and skip the cookie check the gateway relies on dsh to perform. Keep
326
+ dsh web bound to loopback and route external traffic only through the gateway.
327
+
328
+ ### Exposing through the gateway
329
+
330
+ The dsh web server itself is loopback-only (`--host 0.0.0.0` is rejected by
331
+ dsh web). To expose the UI on the LAN through this gateway, run:
332
+
333
+ ```bash
334
+ DSH_GATEWAY_HOST=0.0.0.0 dsh web
335
+ ```
336
+
337
+ No `--trusted-host` flag is needed for proxied requests: the gateway rewrites
338
+ `Host`/`Origin` to the loopback upstream before dsh's Host/Origin fence sees
339
+ them. Keep `--trusted-host` only if something else still reaches dsh directly
340
+ under a non-loopback authority.
341
+
342
+ ## Security notes
343
+
344
+ This is intentionally minimal. Understand what the removed password gate used
345
+ to provide:
346
+
347
+ - **The session cookie is a bearer credential, not a per-user account.** Any
348
+ browser holding `dsh-auth-…` is the owner. There is no password, no rate
349
+ limit, and no per-device revocation; revoking means deleting dsh's
350
+ `client-connection/browser-session` credential record and restarting dsh,
351
+ which signs out everyone.
352
+ - **The launch token appears in a startup line and in the URL.** Treat the
353
+ printed sign-in URL like a password, keep it out of logs and screenshots, and
354
+ remember that query strings can land in reverse-proxy access logs. The token
355
+ rotates on every `dsh web` restart.
356
+ - **Cookies last 30 days.** dsh's `connection.cookieMaxAgeDays` controls that.
357
+ - **Plain HTTP leaks the cookie in transit.** dsh sets `Secure` only on a TLS
358
+ listener; behind a TLS proxy the gateway adds it, but the gateway-to-dsh hop
359
+ is plain HTTP on loopback, so keep it there.
360
+
361
+ **Loopback identity widens the gate.** Every request reaches dsh as if it came
362
+ from the local machine. Only dsh's cookie separates a remote browser from
363
+ host-local plugin surfaces, so keep the gateway itself behind HTTPS and do not
364
+ expose dsh's raw port.
365
+
366
+ **Host mode widens that gate.** `ownsHost: true` gives every signed-in browser
367
+ the same configuration surface the Desktop shell has: editing settings (which
368
+ writes `cordis.patch.yml`), the "open configuration file" action, and
369
+ installing or enabling plugin bundles. Since a plugin bundle is code the Host
370
+ loads, anyone who can sign in can effectively run code on this machine. Keep
371
+ Host mode on only where everyone who can obtain the sign-in URL is trusted to
372
+ do that, and turn it off on the gateway's **Configure** page (or with
373
+ `DSH_GATEWAY_HOST_MODE=0`) otherwise.
@@ -0,0 +1,15 @@
1
+ # yaml-language-server: $schema=../../schemas/cordis-patch.schema.json
2
+
3
+ # dsh-gateway bundle patch.
4
+ #
5
+ # Adds one gateway row beside the dsh web server: a reverse proxy that relays
6
+ # dsh's own token authentication and also delivers the installable PWA shell and
7
+ # Host-mode transport declaration. One row means one lifecycle and one switch on
8
+ # the Plugins page.
9
+ #
10
+ # `connection` is optional: without the browser transport the gateway still
11
+ # proxies, it just cannot print a tokenized sign-in URL.
12
+ - insert:
13
+ - id: gateway
14
+ name: '@gitsang/dsh-gateway'
15
+ inject: [webServer]
package/package.json CHANGED
@@ -1,6 +1,65 @@
1
1
  {
2
2
  "name": "@gitsang/dsh-gateway",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "description": "An installable gateway for the dsh web UI: reverse proxy on dsh token authentication, PWA delivery and Host-mode injection.",
4
+ "version": "0.3.0",
5
+ "type": "module",
6
+ "scripts": {
7
+ "test": "node --test"
8
+ },
9
+ "main": "src/index.js",
10
+ "exports": {
11
+ ".": "./src/index.js",
12
+ "./client": "./src/client.js",
13
+ "./cordis.patch.yml": "./cordis.patch.yml",
14
+ "./package.json": "./package.json"
15
+ },
16
+ "files": [
17
+ "src",
18
+ "cordis.patch.yml",
19
+ "README.md",
20
+ "LICENSE"
21
+ ],
22
+ "license": "MIT",
23
+ "keywords": [
24
+ "dsh",
25
+ "deepseek-harness",
26
+ "dsh-plugin",
27
+ "gateway",
28
+ "reverse-proxy",
29
+ "pwa"
30
+ ],
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/gitsang/dsh-gateway.git"
34
+ },
35
+ "homepage": "https://github.com/gitsang/dsh-gateway#readme",
36
+ "bugs": {
37
+ "url": "https://github.com/gitsang/dsh-gateway/issues"
38
+ },
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "dsh": {
43
+ "bundle": {
44
+ "patch": "./cordis.patch.yml"
45
+ },
46
+ "client": {
47
+ "platform": "web",
48
+ "inject": [
49
+ "@deepseek-ai/dsh-client-locale",
50
+ "@deepseek-ai/dsh-client-ui-primitives",
51
+ "@deepseek-ai/dsh-client-ui-plugin-manager"
52
+ ]
53
+ }
54
+ },
55
+ "dependencies": {},
56
+ "peerDependencies": {
57
+ "@deepseek-ai/cordis": "^4.0.1",
58
+ "@deepseek-ai/schemastery": "^3.18.1",
59
+ "react": "^18.2.0"
60
+ },
61
+ "devDependencies": {
62
+ "@deepseek-ai/cordis": "^4.0.1",
63
+ "@deepseek-ai/schemastery": "^3.18.1"
64
+ }
65
+ }