@kabmo-labs/kabmo 0.33.0 → 0.34.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/client/index.html +1 -1
- package/dist/client/index.js +785 -624
- package/dist/client/static/js/{CsvGrid-fncjbg6j.js → CsvGrid-vze4m5ag.js} +1 -1
- package/dist/client/static/js/{DocxView-c9v2vr3r.js → DocxView-jbf21gmh.js} +5 -5
- package/dist/client/static/js/{DrawioView-vxjksm12.js → DrawioView-b52qybx5.js} +2 -2
- package/dist/client/static/js/{ImageView-a3g5k226.js → ImageView-mdgzj8px.js} +1 -1
- package/dist/client/static/js/{LayoutWrapper-hnjfwsjh.js → LayoutWrapper-3c49dvtt.js} +2 -2
- package/dist/client/static/js/Modal-sqp864vd.js +38 -0
- package/dist/client/static/js/{PdfView-ksaes53y.js → PdfView-g06rxqb1.js} +1 -1
- package/dist/client/static/js/{SessionsView-30vcr95v.js → SessionsView-521adxyr.js} +1 -1
- package/dist/client/static/js/{SheetView-vc484scg.js → SheetView-h8xmsz7q.js} +22 -22
- package/dist/client/static/js/{SvgPreview-tph89jet.js → SvgPreview-1j4jvcvz.js} +1 -1
- package/dist/client/static/js/{abnfDiagram-O67JEVCF-xbar278c.js → abnfDiagram-O67JEVCF-pasxh4b6.js} +1 -1
- package/dist/client/static/js/architecture-7GRP2DOG-fc9kmq1w.js +1 -0
- package/dist/client/static/js/{architectureDiagram-NJMV4G6O-s171n2z6.js → architectureDiagram-NJMV4G6O-ms14g0bm.js} +4 -4
- package/dist/client/static/js/{blockDiagram-BEXU5L5S-gzx6rv9n.js → blockDiagram-BEXU5L5S-ycn78fcq.js} +4 -4
- package/dist/client/static/js/{c4Diagram-YGBWAQC7-7f00skch.js → c4Diagram-YGBWAQC7-vn2gpjfm.js} +5 -5
- package/dist/client/static/js/{classDiagram-v2-NBCMYWYE-j79v95nc.js → classDiagram-v2-NBCMYWYE-fyrm1pr5.js} +4 -4
- package/dist/client/static/js/{cose-bilkent-JH36ORCC-5fkn7ee5.js → cose-bilkent-JH36ORCC-ea5nwvgq.js} +1 -1
- package/dist/client/static/js/cynefin-OW5HDTMX-a4t8992q.js +1 -0
- package/dist/client/static/js/{cynefinDiagram-VND7K2PF-cfs1yvrd.js → cynefinDiagram-VND7K2PF-j6bsjpre.js} +1 -1
- package/dist/client/static/js/{dagre-6A5THRUB-44bgc48p.js → dagre-6A5THRUB-4smdkwv8.js} +2 -2
- package/dist/client/static/js/{diagram-22UHCM2B-a4g4x83k.js → diagram-22UHCM2B-9sm4kqwc.js} +11 -11
- package/dist/client/static/js/{diagram-3UASUU5V-gm4rg5na.js → diagram-3UASUU5V-2h3qxaed.js} +1 -1
- package/dist/client/static/js/{diagram-ATOU4E4O-a95y41vk.js → diagram-ATOU4E4O-1981bc94.js} +3 -3
- package/dist/client/static/js/{diagram-CDSNMT55-t3j93mmc.js → diagram-CDSNMT55-3vgsxavc.js} +4 -4
- package/dist/client/static/js/{diagram-MLGK6HIB-c2wdez02.js → diagram-MLGK6HIB-c9gf2r3f.js} +2 -2
- package/dist/client/static/js/{diagram-MPIPVDR6-gya9jhs5.js → diagram-MPIPVDR6-gjqeq92d.js} +2 -2
- package/dist/client/static/js/{dockerfile-a7q6174s.js → dockerfile-8v935dkn.js} +1 -1
- package/dist/client/static/js/{ebnfDiagram-ZINNZB2B-jjhkza6t.js → ebnfDiagram-ZINNZB2B-4vbsgh99.js} +1 -1
- package/dist/client/static/js/{elixir-rpa1g4gy.js → elixir-vcbg60wv.js} +1 -1
- package/dist/client/static/js/{elk-276RUBZZ-f7re45mw.js → elk-276RUBZZ-nca3t891.js} +1 -1
- package/dist/client/static/js/{erDiagram-OPXOYQCR-tpm9g5te.js → erDiagram-OPXOYQCR-5kfkev03.js} +2 -2
- package/dist/client/static/js/eventmodeling-NTZA5JFV-18bdrkv5.js +1 -0
- package/dist/client/static/js/flowDiagram-KWPJA3E3-fg0yqx0h.js +1 -0
- package/dist/client/static/js/{ganttDiagram-FUAMR5RP-fz43n12d.js → ganttDiagram-FUAMR5RP-mxc73wmq.js} +3 -3
- package/dist/client/static/js/gitGraph-4MIJSDKK-8fa6y40p.js +1 -0
- package/dist/client/static/js/{gitGraphDiagram-X574FWY7-f61hgbn6.js → gitGraphDiagram-X574FWY7-7en3ag0e.js} +12 -12
- package/dist/client/static/js/html-dbrwss36.js +1 -0
- package/dist/client/static/js/{index-m73za75e.js → index-0vm41je9.js} +3 -3
- package/dist/client/static/js/{index-eyzeya95.js → index-1b0t1428.js} +5 -5
- package/dist/client/static/js/{index-2x5f66t6.js → index-1ksg88qf.js} +1 -1
- package/dist/client/static/js/{index-sffjtm5w.js → index-231241f4.js} +9 -9
- package/dist/client/static/js/index-2mx9ej4k.css +1 -0
- package/dist/client/static/js/{index-8239j2v1.js → index-4g46wbdw.js} +1 -1
- package/dist/client/static/js/{index-1xqs14fr.js → index-4qwzxj81.js} +1 -1
- package/dist/client/static/js/{index-gcs49f3p.js → index-52rqxgya.js} +1 -1
- package/dist/client/static/js/{index-at6bhjyf.js → index-6sf2vsp2.js} +1 -1
- package/dist/client/static/js/{index-q2w6s8n7.js → index-7a64yjrm.js} +4 -4
- package/dist/client/static/js/{index-rg8jpbsw.js → index-8acv5b77.js} +2 -2
- package/dist/client/static/js/{index-js7t1vb1.js → index-a0fmsv1d.js} +5 -5
- package/dist/client/static/js/{index-w0fwb80e.js → index-ag2rmtas.js} +1 -1
- package/dist/client/static/js/{index-djmg1d7h.js → index-b29afsjh.js} +1 -1
- package/dist/client/static/js/{index-e4p4cd3r.js → index-c3ek61zv.js} +1 -1
- package/dist/client/static/js/{index-0e40xskg.js → index-cd65zqba.js} +1 -1
- package/dist/client/static/js/{index-zt75xpq0.js → index-cgzrpvzj.js} +204 -204
- package/dist/client/static/js/{index-0n5wwe60.js → index-ev3yt353.js} +2 -2
- package/dist/client/static/js/{index-9y3gjdj7.js → index-eyfcm0cy.js} +1 -1
- package/dist/client/static/js/{index-qsm2z36d.js → index-f7jhhqtk.js} +1 -1
- package/dist/client/static/js/{index-vw5k8csz.js → index-fta1y9pc.js} +50 -50
- package/dist/client/static/js/{index-ajs3jj6j.js → index-fvmfge3p.js} +1 -1
- package/dist/client/static/js/{index-pxv2nw9w.js → index-g2dyfze9.js} +1 -1
- package/dist/client/static/js/{index-bkye8mz4.js → index-h5bn97p6.js} +1 -1
- package/dist/client/static/js/{index-trm9x02n.js → index-j51d5a5m.js} +6 -6
- package/dist/client/static/js/{index-pbe4yed4.js → index-jsyekbx9.js} +2 -2
- package/dist/client/static/js/{index-gs6f0qwa.js → index-nccwxv0s.js} +2 -2
- package/dist/client/static/js/{index-d495kfm0.js → index-p0nz38bm.js} +1 -1
- package/dist/client/static/js/{index-sdtye3jy.js → index-pm6xxwz1.js} +2 -2
- package/dist/client/static/js/{index-3rcjx70v.js → index-ptcrwswj.js} +1 -1
- package/dist/client/static/js/{index-14vndmcr.js → index-ptcwae2n.js} +1 -1
- package/dist/client/static/js/{index-5tgq7aqx.js → index-r014brzs.js} +2 -2
- package/dist/client/static/js/{index-xj9649ac.js → index-ray2czf6.js} +7 -7
- package/dist/client/static/js/{index-apvkd37e.js → index-rs80cwsv.js} +3 -3
- package/dist/client/static/js/{index-vw4brsdx.js → index-rzxcj7cg.js} +1 -1
- package/dist/client/static/js/{index-a293pdg7.js → index-s7gkkkyz.js} +2 -2
- package/dist/client/static/js/{index-1a0zkj8h.js → index-sd66wv26.js} +2 -2
- package/dist/client/static/js/{index-ypx3qmhq.js → index-vhqe0y82.js} +1 -1
- package/dist/client/static/js/{index-9fjt5anh.js → index-vy6termr.js} +1 -1
- package/dist/client/static/js/{index-0tf0arsp.js → index-xx6mqpxm.js} +3 -3
- package/dist/client/static/js/{index-03h3bkcf.js → index-ybm2x4vh.js} +1 -1
- package/dist/client/static/js/info-A6RAGUB7-3hk844f7.js +1 -0
- package/dist/client/static/js/{infoDiagram-VRGFBTTK-1qj804sn.js → infoDiagram-VRGFBTTK-sef80132.js} +2 -2
- package/dist/client/static/js/{ishikawaDiagram-OU5B5YK6-4j4qsnzw.js → ishikawaDiagram-OU5B5YK6-yhvepp1q.js} +6 -6
- package/dist/client/static/js/{journeyDiagram-ZHPQQLJL-j0brnfft.js → journeyDiagram-ZHPQQLJL-5w77y95w.js} +5 -5
- package/dist/client/static/js/{julia-4ghmfy3e.js → julia-d4j6wgvf.js} +1 -1
- package/dist/client/static/js/{kanban-definition-PNTS6WVX-qqc22br0.js → kanban-definition-PNTS6WVX-02znqtna.js} +6 -6
- package/dist/client/static/js/{mermaid.core-vv3wbe65.js → mermaid.core-a6e6vck4.js} +5 -5
- package/dist/client/static/js/{mindmap-definition-NLK3R4M7-y5b2z8zp.js → mindmap-definition-NLK3R4M7-y4n74q9x.js} +8 -8
- package/dist/client/static/js/one-dark-pro-vwhfpzr8.js +1 -0
- package/dist/client/static/js/one-light-y5t9x6zh.js +1 -0
- package/dist/client/static/js/packet-AYTQ26CC-4nj2yy10.js +1 -0
- package/dist/client/static/js/{pegDiagram-GJSIUBJH-0be07mqs.js → pegDiagram-GJSIUBJH-pr7ehgzb.js} +1 -1
- package/dist/client/static/js/{perl-sd00t4b4.js → perl-srptw5vm.js} +1 -1
- package/dist/client/static/js/{php-2zfyqa61.js → php-n89g3fk7.js} +1 -1
- package/dist/client/static/js/pie-WAS4IAKB-k7rde3f9.js +1 -0
- package/dist/client/static/js/{pieDiagram-5QR66LMP-544w86jk.js → pieDiagram-5QR66LMP-39wpsk08.js} +2 -2
- package/dist/client/static/js/{quadrantDiagram-O4NWA36T-p09e7z5s.js → quadrantDiagram-O4NWA36T-esm913km.js} +5 -5
- package/dist/client/static/js/radar-RG4KPBEZ-513dpw4f.js +1 -0
- package/dist/client/static/js/railroad-74A4TZTK-9bqnst9c.js +1 -0
- package/dist/client/static/js/railroad-abnf-HS5TGJTU-7367d5bv.js +1 -0
- package/dist/client/static/js/railroad-ebnf-LZEXJU2U-b806b1mj.js +1 -0
- package/dist/client/static/js/railroad-peg-WCYAUIDC-5a67rwf8.js +1 -0
- package/dist/client/static/js/{railroadDiagram-XR7U4H2S-de830ed2.js → railroadDiagram-XR7U4H2S-wy2mgya6.js} +1 -1
- package/dist/client/static/js/{requirementDiagram-PLB6GJNP-b47d74p1.js → requirementDiagram-PLB6GJNP-6z9xdgbq.js} +2 -2
- package/dist/client/static/js/{ruby-v9qaqhby.js → ruby-r81ytgcm.js} +1 -1
- package/dist/client/static/js/{rust-fkt0qyrs.js → rust-frn3szvc.js} +1 -1
- package/dist/client/static/js/{sankeyDiagram-IPEJSGJF-6ezc2fpj.js → sankeyDiagram-IPEJSGJF-rtmxk3ys.js} +4 -4
- package/dist/client/static/js/{sequenceDiagram-PO4LG4MO-by5zqj6w.js → sequenceDiagram-PO4LG4MO-awzcrafg.js} +18 -18
- package/dist/client/static/js/{sizeCapture-INFHLROL-q85r06j6.js → sizeCapture-INFHLROL-03gy196j.js} +1 -1
- package/dist/client/static/js/sql-0e9mnrda.js +1 -0
- package/dist/client/static/js/{stateDiagram-v2-GCMORJYK-ep5pyexs.js → stateDiagram-v2-GCMORJYK-an8v3q5n.js} +3 -3
- package/dist/client/static/js/{swimlanes-2SLR337P-95fcqkh8.js → swimlanes-2SLR337P-n9yb4wcs.js} +1 -1
- package/dist/client/static/js/{swimlanesDiagram-TC7HE7FX-xs2en5dv.js → swimlanesDiagram-TC7HE7FX-nx5nfnf0.js} +1 -1
- package/dist/client/static/js/{timeline-definition-EJHVYXUP-461e615g.js → timeline-definition-EJHVYXUP-jceb575g.js} +4 -4
- package/dist/client/static/js/treeView-Q6P3EWNA-kcy40evb.js +1 -0
- package/dist/client/static/js/treemap-WGGIJYW6-fsrvqxf4.js +1 -0
- package/dist/client/static/js/{usecaseDiagram-POWQR4AR-ftqfnvhg.js → usecaseDiagram-POWQR4AR-888h3100.js} +13 -13
- package/dist/client/static/js/{vennDiagram-UO4OBE2U-7t96h6ez.js → vennDiagram-UO4OBE2U-wbfz0rzv.js} +7 -7
- package/dist/client/static/js/{vue-j8vb9d7r.js → vue-c0rtg1wf.js} +1 -1
- package/dist/client/static/js/wardley-WFR3VGLG-36dmxww4.js +1 -0
- package/dist/client/static/js/{wardleyDiagram-VNRHLVJA-0de8w8c6.js → wardleyDiagram-VNRHLVJA-rn8c5qf9.js} +4 -4
- package/dist/client/static/js/{xychartDiagram-PMCCYNJV-dez9phkf.js → xychartDiagram-PMCCYNJV-8sn1apt6.js} +5 -5
- package/package.json +4 -1
- package/src/client/components/auth/login-screen/index.tsx +1 -0
- package/src/client/components/common/MarkdownRenderer.tsx +9 -12
- package/src/client/components/settings/Modal.tsx +14 -84
- package/src/client/components/settings/Sidebar.tsx +3 -1
- package/src/client/components/settings/categories/ConnectSettings.tsx +105 -0
- package/src/client/components/settings/categories/connect-settings/DeviceTable.tsx +145 -0
- package/src/client/components/settings/categories/connect-settings/PairDialog.tsx +160 -0
- package/src/client/components/settings/categories/connect-settings/QrCode.tsx +73 -0
- package/src/client/components/settings/categories/connect-settings/ServerCard.tsx +94 -0
- package/src/client/components/settings/categories/connect-settings/TunnelRow.tsx +127 -0
- package/src/client/components/settings/category-content.tsx +116 -0
- package/src/client/components/workspace/chat-timeline/ChangedFilesSummary.test.tsx +120 -0
- package/src/client/components/workspace/chat-timeline/ChangedFilesSummary.tsx +56 -20
- package/src/client/components/workspace/chat-timeline/RunFooter.tsx +1 -0
- package/src/client/hooks/chat/timeline/change-stats.ts +57 -0
- package/src/client/hooks/connect/use-connect-status.ts +116 -0
- package/src/client/hooks/connect/use-pairing.ts +100 -0
- package/src/client/hooks/ui/clipboard.ts +26 -0
- package/src/client/tailwind.css +5 -0
- package/src/server/index.ts +2 -0
- package/src/server/lib/assets/font-css.server.test.ts +19 -0
- package/src/server/lib/auth/client-address.test.ts +58 -0
- package/src/server/lib/auth/client-address.ts +37 -3
- package/src/server/lib/auth/guard.ts +31 -4
- package/src/server/lib/auth/rate-limit.test.ts +25 -8
- package/src/server/lib/auth/rate-limit.ts +8 -5
- package/src/server/lib/connect/cloudflared.lifecycle.test.ts +88 -0
- package/src/server/lib/connect/cloudflared.server.test.ts +65 -0
- package/src/server/lib/connect/cloudflared.server.ts +206 -0
- package/src/server/lib/connect/devices.server.ts +192 -0
- package/src/server/lib/connect/identity.server.ts +81 -0
- package/src/server/lib/connect/pairing.server.test.ts +56 -0
- package/src/server/lib/connect/pairing.server.ts +113 -0
- package/src/server/lib/connect/qr.roundtrip.test.ts +106 -0
- package/src/server/lib/connect/qr.server.ts +36 -0
- package/src/server/lib/connect/run.server.ts +101 -0
- package/src/server/lib/connect/status.server.ts +177 -0
- package/src/server/lib/connect/tailscale.server.test.ts +117 -0
- package/src/server/lib/connect/tailscale.server.ts +172 -0
- package/src/server/lib/db/schema.ts +4 -0
- package/src/server/lib/omp/session/changes.test.ts +64 -1
- package/src/server/lib/omp/session/changes.ts +94 -1
- package/src/server/routes/connect/devices.ts +56 -0
- package/src/server/routes/connect/index.ts +15 -0
- package/src/server/routes/connect/pair-page.ts +177 -0
- package/src/server/routes/connect/pair.ts +158 -0
- package/src/server/routes/connect/status.ts +19 -0
- package/src/server/routes/connect/tunnels.ts +76 -0
- package/src/server/routes/health/probe.ts +5 -0
- package/src/server/routes/index.ts +2 -0
- package/src/server/routes/sessions/changes.test.ts +12 -3
- package/src/server/routes/sessions/changes.ts +27 -3
- package/src/server/routes/sessions/index.ts +2 -1
- package/src/shared/lib/connect/payload.test.ts +144 -0
- package/src/shared/lib/connect/payload.ts +229 -0
- package/src/shared/lib/connect/tier.test.ts +73 -0
- package/src/shared/lib/connect/tier.ts +105 -0
- package/src/shared/lib/connect/types.ts +99 -0
- package/src/shared/lib/markdown/fira-code-fonts.css +65 -6
- package/src/shared/lib/markdown/marked.ts +7 -4
- package/src/shared/lib/markdown/mermaid-actions.test.ts +78 -0
- package/src/shared/lib/markdown/mermaid-actions.ts +45 -0
- package/src/shared/lib/markdown/mermaid.ts +41 -7
- package/src/shared/lib/settings/sections.ts +6 -1
- package/src/shared/types/session-changes.ts +6 -0
- package/src/shared/types/settings/state.ts +2 -0
- package/dist/client/static/js/Modal-qtftcp2t.js +0 -38
- package/dist/client/static/js/architecture-7GRP2DOG-tavtgb8a.js +0 -1
- package/dist/client/static/js/cynefin-OW5HDTMX-cdh3n2z2.js +0 -1
- package/dist/client/static/js/eventmodeling-NTZA5JFV-as97tpyp.js +0 -1
- package/dist/client/static/js/flowDiagram-KWPJA3E3-ggm06d12.js +0 -1
- package/dist/client/static/js/gitGraph-4MIJSDKK-eqz7p9ng.js +0 -1
- package/dist/client/static/js/html-pv88ny12.js +0 -1
- package/dist/client/static/js/index-e94a1en0.css +0 -1
- package/dist/client/static/js/info-A6RAGUB7-4pkzh1qx.js +0 -1
- package/dist/client/static/js/one-dark-pro-hm3f56n1.js +0 -1
- package/dist/client/static/js/one-light-xrz49rct.js +0 -1
- package/dist/client/static/js/packet-AYTQ26CC-yg71sp88.js +0 -1
- package/dist/client/static/js/pie-WAS4IAKB-89wd6np0.js +0 -1
- package/dist/client/static/js/radar-RG4KPBEZ-avn0b8r9.js +0 -1
- package/dist/client/static/js/railroad-74A4TZTK-de27xv5w.js +0 -1
- package/dist/client/static/js/railroad-abnf-HS5TGJTU-rg4zzj7x.js +0 -1
- package/dist/client/static/js/railroad-ebnf-LZEXJU2U-d84fhj8v.js +0 -1
- package/dist/client/static/js/railroad-peg-WCYAUIDC-dvf8xrmc.js +0 -1
- package/dist/client/static/js/sql-g8wcshpc.js +0 -1
- package/dist/client/static/js/treeView-Q6P3EWNA-mham0bh6.js +0 -1
- package/dist/client/static/js/treemap-WGGIJYW6-mxrxax86.js +0 -1
- package/dist/client/static/js/wardley-WFR3VGLG-8vrvh9p3.js +0 -1
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Cloudflare Tunnel, named only.
|
|
8
|
+
*
|
|
9
|
+
* Quick tunnels are deliberately NOT supported. Two measured facts make them
|
|
10
|
+
* unusable for this product: they buffer Server-Sent Events (cloudflared #1449 —
|
|
11
|
+
* the maintainer confirms named tunnels stream correctly and quick tunnels do
|
|
12
|
+
* not), and Kabmo has six SSE endpoints including the chat stream, so a quick
|
|
13
|
+
* tunnel would make every answer arrive in one burst at the end. They also cap
|
|
14
|
+
* at 200 in-flight requests and change hostname on every start.
|
|
15
|
+
*
|
|
16
|
+
* A named tunnel needs an account and a domain, which is why it is the second
|
|
17
|
+
* route rather than the first. It is also the one route where a third party
|
|
18
|
+
* reads the traffic: TLS terminates at Cloudflare's edge, so the console's
|
|
19
|
+
* password and every prompt cross their machines in plaintext. The Connect page
|
|
20
|
+
* says so on the card rather than in a wiki.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import fs from 'fs';
|
|
24
|
+
import os from 'os';
|
|
25
|
+
import path from 'path';
|
|
26
|
+
import { which } from '@/server/lib/connect/run.server';
|
|
27
|
+
|
|
28
|
+
export interface CloudflaredPresence {
|
|
29
|
+
binary: string | null;
|
|
30
|
+
configPath: string | null;
|
|
31
|
+
/** Why it cannot be used, in one sentence, when it cannot. */
|
|
32
|
+
reason: string | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface CloudflaredConfig {
|
|
36
|
+
/** The tunnel name or UUID from `tunnel:`. */
|
|
37
|
+
tunnel: string | null;
|
|
38
|
+
/** The first public hostname in the ingress list. */
|
|
39
|
+
hostname: string | null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Where cloudflared looks for its config, honouring the documented override. */
|
|
43
|
+
export function cloudflaredConfigPath(): string {
|
|
44
|
+
const override = (Bun.env.CLOUDFLARED_CONFIG ?? '').trim();
|
|
45
|
+
if (override) return override;
|
|
46
|
+
return path.join(os.homedir(), '.cloudflared', 'config.yml');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Read the two facts this page needs out of the YAML.
|
|
51
|
+
*
|
|
52
|
+
* A line scan rather than a YAML parser: the file is written by
|
|
53
|
+
* `cloudflared tunnel create` and by hand, and the only fields read here are a
|
|
54
|
+
* top-level `tunnel:` and the first `hostname:` under `ingress:`. Pulling in a
|
|
55
|
+
* parser for two scalars would be a dependency the rest of the server does not
|
|
56
|
+
* have, and a malformed file must degrade to "not configured" rather than throw.
|
|
57
|
+
*/
|
|
58
|
+
export function readCloudflaredConfig(file: string): CloudflaredConfig | null {
|
|
59
|
+
let text: string;
|
|
60
|
+
try {
|
|
61
|
+
text = fs.readFileSync(file, 'utf8');
|
|
62
|
+
} catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let tunnel: string | null = null;
|
|
67
|
+
let hostname: string | null = null;
|
|
68
|
+
let inIngress = false;
|
|
69
|
+
for (const rawLine of text.split('\n')) {
|
|
70
|
+
const line = rawLine.replace(/\s+#.*$/, '');
|
|
71
|
+
if (/^tunnel:\s*/.test(line)) {
|
|
72
|
+
tunnel = line.replace(/^tunnel:\s*/, '').trim().replace(/^["']|["']$/g, '') || null;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (/^ingress:\s*$/.test(line)) {
|
|
76
|
+
inIngress = true;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (inIngress && /^\S/.test(line)) {
|
|
80
|
+
// A new top-level key ends the ingress block.
|
|
81
|
+
inIngress = false;
|
|
82
|
+
}
|
|
83
|
+
if (inIngress && hostname === null) {
|
|
84
|
+
const match = /^\s*-\s*hostname:\s*(\S+)/.exec(line) ?? /^\s*hostname:\s*(\S+)/.exec(line);
|
|
85
|
+
if (match?.[1]) hostname = match[1].replace(/^["']|["']$/g, '');
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!tunnel && !hostname) return null;
|
|
90
|
+
return { tunnel, hostname };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Locate the binary and a usable config. */
|
|
94
|
+
export function findCloudflared(): CloudflaredPresence {
|
|
95
|
+
const binary = which('cloudflared');
|
|
96
|
+
const configPath = cloudflaredConfigPath();
|
|
97
|
+
if (!binary) {
|
|
98
|
+
return {
|
|
99
|
+
binary: null,
|
|
100
|
+
configPath: null,
|
|
101
|
+
reason: 'The cloudflared command was not found. Install it with `brew install cloudflared`.',
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const config = readCloudflaredConfig(configPath);
|
|
105
|
+
if (!config) {
|
|
106
|
+
return {
|
|
107
|
+
binary,
|
|
108
|
+
configPath: null,
|
|
109
|
+
reason: `No cloudflared config at ${configPath}. A named tunnel needs an account and a domain.`,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
if (!config.hostname) {
|
|
113
|
+
return {
|
|
114
|
+
binary,
|
|
115
|
+
configPath,
|
|
116
|
+
reason: `The config at ${configPath} has no ingress hostname, so there is no public URL to show.`,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
return { binary, configPath, reason: null };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
interface RunningTunnel {
|
|
123
|
+
proc: Bun.Subprocess<'ignore', 'ignore', 'pipe'>;
|
|
124
|
+
url: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
declare global {
|
|
128
|
+
// eslint-disable-next-line no-var
|
|
129
|
+
var __kabmoCloudflared: Map<number, RunningTunnel> | undefined;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function running(): Map<number, RunningTunnel> {
|
|
133
|
+
globalThis.__kabmoCloudflared ??= new Map();
|
|
134
|
+
return globalThis.__kabmoCloudflared;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The public origin of a running tunnel for `port`, or null. */
|
|
138
|
+
export function runningTunnelUrl(port: number): string | null {
|
|
139
|
+
return running().get(port)?.url ?? null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Start the named tunnel for `port`.
|
|
144
|
+
*
|
|
145
|
+
* `--url` overrides the config's ingress service so the tunnel points at THIS
|
|
146
|
+
* instance's port rather than whatever the config was written for; without it a
|
|
147
|
+
* second Kabmo on another port would silently share the first one.
|
|
148
|
+
*/
|
|
149
|
+
export async function startCloudflared(
|
|
150
|
+
binary: string,
|
|
151
|
+
configPath: string,
|
|
152
|
+
hostname: string,
|
|
153
|
+
port: number,
|
|
154
|
+
): Promise<{ ok: boolean; url: string | null; reason: string | null }> {
|
|
155
|
+
if (running().has(port)) {
|
|
156
|
+
return { ok: true, url: runningTunnelUrl(port), reason: null };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let proc: Bun.Subprocess<'ignore', 'ignore', 'pipe'>;
|
|
160
|
+
try {
|
|
161
|
+
proc = Bun.spawn({
|
|
162
|
+
cmd: [binary, 'tunnel', '--config', configPath, 'run', '--url', `http://127.0.0.1:${port}`],
|
|
163
|
+
stdin: 'ignore',
|
|
164
|
+
stdout: 'ignore',
|
|
165
|
+
stderr: 'pipe',
|
|
166
|
+
env: { ...Bun.env },
|
|
167
|
+
});
|
|
168
|
+
} catch (error) {
|
|
169
|
+
return {
|
|
170
|
+
ok: false,
|
|
171
|
+
url: null,
|
|
172
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const url = `https://${hostname}`;
|
|
177
|
+
running().set(port, { proc, url });
|
|
178
|
+
|
|
179
|
+
// The process is long-lived; a failure after this point is reported by the
|
|
180
|
+
// status read (the process is gone), not by this call.
|
|
181
|
+
proc.exited.then(() => {
|
|
182
|
+
running().delete(port);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
return { ok: true, url, reason: null };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Stop the tunnel for `port`, if this process started one. */
|
|
189
|
+
export function stopCloudflared(port: number): { ok: boolean; reason: string | null } {
|
|
190
|
+
const entry = running().get(port);
|
|
191
|
+
if (!entry) return { ok: true, reason: null };
|
|
192
|
+
try {
|
|
193
|
+
entry.proc.kill('SIGTERM');
|
|
194
|
+
} catch {
|
|
195
|
+
// Already gone.
|
|
196
|
+
}
|
|
197
|
+
running().delete(port);
|
|
198
|
+
return { ok: true, reason: null };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Whether a tunnel this process started is still alive. */
|
|
202
|
+
export function cloudflaredAlive(port: number): boolean {
|
|
203
|
+
const entry = running().get(port);
|
|
204
|
+
if (!entry) return false;
|
|
205
|
+
return entry.proc.exitCode === null;
|
|
206
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Devices that have redeemed a pairing code, and the tokens they hold.
|
|
8
|
+
*
|
|
9
|
+
* A device token is NOT the UI password and never becomes one. The password is
|
|
10
|
+
* a single shared secret typed by a human; a device token is per-device, so
|
|
11
|
+
* revoking one phone does not sign out the laptop, and a token that leaks
|
|
12
|
+
* identifies exactly which device leaked it.
|
|
13
|
+
*
|
|
14
|
+
* Only the SHA-256 of a token is stored, for the same reason the agent tokens
|
|
15
|
+
* are hashed: a copy of the database must not be replayable as a credential.
|
|
16
|
+
* The gate runs on every request and is synchronous, so verified hashes are
|
|
17
|
+
* mirrored in memory and a restart re-reads the table.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type { DbClient } from '@/server/lib/db/client';
|
|
21
|
+
import { getDb } from '@/server/db.server';
|
|
22
|
+
import type { ConnectRouteKind, PairedDevice } from '@/shared/lib/connect/types';
|
|
23
|
+
|
|
24
|
+
interface DeviceRow {
|
|
25
|
+
id: string;
|
|
26
|
+
name: string;
|
|
27
|
+
platform: string;
|
|
28
|
+
token_hash: string;
|
|
29
|
+
route: string | null;
|
|
30
|
+
rtt_ms: number | null;
|
|
31
|
+
created_at: number;
|
|
32
|
+
last_seen_at: number | null;
|
|
33
|
+
revoked_at: number | null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
declare global {
|
|
37
|
+
// eslint-disable-next-line no-var
|
|
38
|
+
var __kabmoDeviceTokens: Map<string, string> | undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function tokenCache(): Map<string, string> {
|
|
42
|
+
globalThis.__kabmoDeviceTokens ??= new Map();
|
|
43
|
+
return globalThis.__kabmoDeviceTokens;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function ensureConnectSchema(db: DbClient): Promise<void> {
|
|
47
|
+
await db.exec(`
|
|
48
|
+
CREATE TABLE IF NOT EXISTS paired_devices (
|
|
49
|
+
id TEXT PRIMARY KEY,
|
|
50
|
+
name TEXT NOT NULL,
|
|
51
|
+
platform TEXT NOT NULL,
|
|
52
|
+
token_hash TEXT NOT NULL UNIQUE,
|
|
53
|
+
route TEXT,
|
|
54
|
+
rtt_ms INTEGER,
|
|
55
|
+
created_at INTEGER NOT NULL,
|
|
56
|
+
last_seen_at INTEGER,
|
|
57
|
+
revoked_at INTEGER
|
|
58
|
+
);
|
|
59
|
+
CREATE INDEX IF NOT EXISTS idx_paired_devices_token
|
|
60
|
+
ON paired_devices (token_hash);
|
|
61
|
+
`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function hashToken(token: string): string {
|
|
65
|
+
return new Bun.CryptoHasher('sha256').update(token).digest('hex');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Load every live token hash into the gate's cache. Call once at boot. */
|
|
69
|
+
export async function initDeviceTokens(): Promise<void> {
|
|
70
|
+
try {
|
|
71
|
+
const db = await getDb();
|
|
72
|
+
const rows = (await db.all(
|
|
73
|
+
'SELECT id, token_hash FROM paired_devices WHERE revoked_at IS NULL',
|
|
74
|
+
)) as { id: string; token_hash: string }[];
|
|
75
|
+
const next = new Map<string, string>();
|
|
76
|
+
for (const row of rows) next.set(row.token_hash, row.id);
|
|
77
|
+
globalThis.__kabmoDeviceTokens = next;
|
|
78
|
+
} catch {
|
|
79
|
+
// A database opened before this table existed has no device credentials;
|
|
80
|
+
// the browser path is unaffected.
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The device id a bearer token belongs to, or null. Synchronous: the gate. */
|
|
85
|
+
export function verifyDeviceToken(token: string | null): string | null {
|
|
86
|
+
if (!token) return null;
|
|
87
|
+
return tokenCache().get(hashToken(token)) ?? null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The cookie a shell sets after pairing.
|
|
92
|
+
*
|
|
93
|
+
* A webview cannot attach an `Authorization` header to the requests it makes —
|
|
94
|
+
* the page's own `fetch`, the WebSocket upgrades, the image loads. A cookie is
|
|
95
|
+
* the one credential the platform sends on all of them, so the shell writes this
|
|
96
|
+
* one for the server's origin and the gate reads it here.
|
|
97
|
+
*
|
|
98
|
+
* `HttpOnly` is set by the shell, not by this server: the value is written
|
|
99
|
+
* through the platform's cookie store, which is the only place a webview cookie
|
|
100
|
+
* can be created from native code.
|
|
101
|
+
*/
|
|
102
|
+
export const DEVICE_COOKIE = 'kabmo_device';
|
|
103
|
+
|
|
104
|
+
/** The device token carried as a cookie, or null. */
|
|
105
|
+
export function readDeviceCookie(request: Request): string | null {
|
|
106
|
+
const header = request.headers.get('cookie');
|
|
107
|
+
if (!header) return null;
|
|
108
|
+
for (const segment of header.split(';')) {
|
|
109
|
+
const index = segment.indexOf('=');
|
|
110
|
+
if (index === -1) continue;
|
|
111
|
+
if (segment.slice(0, index).trim() !== DEVICE_COOKIE) continue;
|
|
112
|
+
const value = segment.slice(index + 1).trim();
|
|
113
|
+
if (!value) return null;
|
|
114
|
+
try {
|
|
115
|
+
return decodeURIComponent(value);
|
|
116
|
+
} catch {
|
|
117
|
+
return value;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Mint a token for a device that just redeemed a pairing code. */
|
|
124
|
+
export async function mintDeviceToken(input: {
|
|
125
|
+
name: string;
|
|
126
|
+
platform: string;
|
|
127
|
+
}): Promise<{ id: string; token: string }> {
|
|
128
|
+
const id = crypto.randomUUID();
|
|
129
|
+
const token = `kabmo_device_${crypto.randomUUID().replace(/-/g, '')}`;
|
|
130
|
+
const hash = hashToken(token);
|
|
131
|
+
const db = await getDb();
|
|
132
|
+
await db.run(
|
|
133
|
+
`INSERT INTO paired_devices (id, name, platform, token_hash, created_at)
|
|
134
|
+
VALUES (?, ?, ?, ?, ?)`,
|
|
135
|
+
[id, input.name.trim() || 'Device', input.platform.trim() || 'unknown', hash, Date.now()],
|
|
136
|
+
);
|
|
137
|
+
tokenCache().set(hash, id);
|
|
138
|
+
return { id, token };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Every device, newest first, revoked ones included so the page can say so. */
|
|
142
|
+
export async function listPairedDevices(): Promise<PairedDevice[]> {
|
|
143
|
+
const db = await getDb();
|
|
144
|
+
const rows = (await db.all(
|
|
145
|
+
`SELECT id, name, platform, route, rtt_ms, created_at, last_seen_at, revoked_at
|
|
146
|
+
FROM paired_devices
|
|
147
|
+
WHERE revoked_at IS NULL
|
|
148
|
+
ORDER BY created_at DESC`,
|
|
149
|
+
)) as DeviceRow[];
|
|
150
|
+
return rows.map((row) => ({
|
|
151
|
+
id: row.id,
|
|
152
|
+
name: row.name,
|
|
153
|
+
platform: row.platform,
|
|
154
|
+
route: (row.route as ConnectRouteKind | null) ?? null,
|
|
155
|
+
rttMs: row.rtt_ms,
|
|
156
|
+
lastSeenAt: row.last_seen_at ? new Date(row.last_seen_at).toISOString() : null,
|
|
157
|
+
createdAt: new Date(row.created_at).toISOString(),
|
|
158
|
+
}));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Cut a device off. The token stops verifying on the next request. */
|
|
162
|
+
export async function revokeDevice(id: string): Promise<boolean> {
|
|
163
|
+
const db = await getDb();
|
|
164
|
+
const row = (await db.get('SELECT token_hash FROM paired_devices WHERE id = ?', [id])) as
|
|
165
|
+
| { token_hash: string }
|
|
166
|
+
| undefined;
|
|
167
|
+
if (!row) return false;
|
|
168
|
+
await db.run('UPDATE paired_devices SET revoked_at = ? WHERE id = ?', [Date.now(), id]);
|
|
169
|
+
tokenCache().delete(row.token_hash);
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Record the route a device reported, so the page can show which one it uses.
|
|
175
|
+
*
|
|
176
|
+
* Best-effort: a failed write must not fail the request that carried it, and the
|
|
177
|
+
* column is a display fact rather than a security one.
|
|
178
|
+
*/
|
|
179
|
+
export async function touchDevice(
|
|
180
|
+
id: string,
|
|
181
|
+
report: { route: ConnectRouteKind; rttMs: number } | null,
|
|
182
|
+
): Promise<void> {
|
|
183
|
+
try {
|
|
184
|
+
const db = await getDb();
|
|
185
|
+
await db.run(
|
|
186
|
+
'UPDATE paired_devices SET last_seen_at = ?, route = COALESCE(?, route), rtt_ms = COALESCE(?, rtt_ms) WHERE id = ?',
|
|
187
|
+
[Date.now(), report?.route ?? null, report?.rttMs ?? null, id],
|
|
188
|
+
);
|
|
189
|
+
} catch {
|
|
190
|
+
// See above: a display fact, not a credential.
|
|
191
|
+
}
|
|
192
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* This server's stable identity, and why it is a key rather than a hostname.
|
|
8
|
+
*
|
|
9
|
+
* A paired device pins `serverId` and refuses a candidate that answers with a
|
|
10
|
+
* different one. That check is what stops a LAN address reassigned by DHCP from
|
|
11
|
+
* receiving a device's token: the address is not the identity, and a hostname is
|
|
12
|
+
* not either (two machines can be called `macbook`).
|
|
13
|
+
*
|
|
14
|
+
* The id is derived from a random key persisted in the data directory, so it
|
|
15
|
+
* survives a restart, a port change and a LAN address change — the three things
|
|
16
|
+
* that must NOT invalidate a pairing. It is deliberately not derived from the
|
|
17
|
+
* machine's hardware id: that would make two installs on one machine share an
|
|
18
|
+
* identity, and a copied data directory is already the case the relay-style
|
|
19
|
+
* designs have to handle explicitly.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import fs from 'fs';
|
|
23
|
+
import path from 'path';
|
|
24
|
+
import { getDataDir } from '@/server/lib/lifecycle/paths';
|
|
25
|
+
|
|
26
|
+
interface IdentityFile {
|
|
27
|
+
v: 1;
|
|
28
|
+
/** base64url of 32 random bytes. Never leaves the machine. */
|
|
29
|
+
key: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface Identity {
|
|
33
|
+
serverId: string;
|
|
34
|
+
key: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
declare global {
|
|
38
|
+
// eslint-disable-next-line no-var
|
|
39
|
+
var __kabmoConnectIdentity: Identity | undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function deriveServerId(key: string): string {
|
|
43
|
+
return new Bun.CryptoHasher('sha256').update(key).digest('base64url');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The identity, minted on first read.
|
|
48
|
+
*
|
|
49
|
+
* A corrupt or unreadable file is REPLACED rather than treated as first run
|
|
50
|
+
* silently: the caller gets a new id, and every paired device will refuse it
|
|
51
|
+
* until it is paired again. That is the honest outcome — the alternative is a
|
|
52
|
+
* server that cannot answer its own identity at all.
|
|
53
|
+
*/
|
|
54
|
+
export function connectIdentity(): Identity {
|
|
55
|
+
const cached = globalThis.__kabmoConnectIdentity;
|
|
56
|
+
if (cached) return cached;
|
|
57
|
+
|
|
58
|
+
const file = path.join(getDataDir(), 'connect-identity.json');
|
|
59
|
+
try {
|
|
60
|
+
const parsed = JSON.parse(fs.readFileSync(file, 'utf8')) as Partial<IdentityFile>;
|
|
61
|
+
if (typeof parsed.key === 'string' && parsed.key.length >= 32) {
|
|
62
|
+
const identity = { key: parsed.key, serverId: deriveServerId(parsed.key) };
|
|
63
|
+
globalThis.__kabmoConnectIdentity = identity;
|
|
64
|
+
return identity;
|
|
65
|
+
}
|
|
66
|
+
} catch {
|
|
67
|
+
// Absent or unreadable: minted below.
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const key = Buffer.from(crypto.getRandomValues(new Uint8Array(32))).toString('base64url');
|
|
71
|
+
const identity = { key, serverId: deriveServerId(key) };
|
|
72
|
+
try {
|
|
73
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
74
|
+
fs.writeFileSync(file, JSON.stringify({ v: 1, key } satisfies IdentityFile), { mode: 0o600 });
|
|
75
|
+
} catch {
|
|
76
|
+
// A read-only data directory still gets a working identity for this process;
|
|
77
|
+
// it just will not survive a restart, and the next boot mints another.
|
|
78
|
+
}
|
|
79
|
+
globalThis.__kabmoConnectIdentity = identity;
|
|
80
|
+
return identity;
|
|
81
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One-time pairing secrets.
|
|
8
|
+
*
|
|
9
|
+
* The property that matters is single use: a code that can be redeemed twice
|
|
10
|
+
* enrols a second device from a QR someone photographed. The second property is
|
|
11
|
+
* that a wrong secret does NOT consume the code — otherwise a mistyped paste
|
|
12
|
+
* would invalidate the code on screen.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { afterEach, describe, expect, test } from 'bun:test';
|
|
16
|
+
import { consumePairing, createPairing, resetPairings } from '@/server/lib/connect/pairing.server';
|
|
17
|
+
|
|
18
|
+
const CANDIDATES = [{ kind: 'lan' as const, url: 'http://192.168.18.76:3000' }];
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
resetPairings();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe('pairing secrets', () => {
|
|
25
|
+
test('redeems once and refuses the second attempt', () => {
|
|
26
|
+
const { pairingId, secret } = createPairing(CANDIDATES);
|
|
27
|
+
expect(consumePairing(pairingId, secret).ok).toBe(true);
|
|
28
|
+
|
|
29
|
+
const second = consumePairing(pairingId, secret);
|
|
30
|
+
expect(second.ok).toBe(false);
|
|
31
|
+
if (!second.ok) expect(second.reason).toContain('already used');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('a wrong secret does not consume the code', () => {
|
|
35
|
+
const { pairingId, secret } = createPairing(CANDIDATES);
|
|
36
|
+
expect(consumePairing(pairingId, 'not-the-secret').ok).toBe(false);
|
|
37
|
+
expect(consumePairing(pairingId, secret).ok).toBe(true);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('an unknown pairing id is refused', () => {
|
|
41
|
+
expect(consumePairing('nope', 'anything').ok).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('carries the candidates the code was minted with', () => {
|
|
45
|
+
const { pairingId, secret } = createPairing(CANDIDATES);
|
|
46
|
+
const consumed = consumePairing(pairingId, secret);
|
|
47
|
+
expect(consumed.ok && consumed.candidates).toEqual(CANDIDATES);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('mints a distinct secret per code', () => {
|
|
51
|
+
const first = createPairing(CANDIDATES);
|
|
52
|
+
const second = createPairing(CANDIDATES);
|
|
53
|
+
expect(first.secret).not.toBe(second.secret);
|
|
54
|
+
expect(first.pairingId).not.toBe(second.pairingId);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One-time pairing secrets.
|
|
8
|
+
*
|
|
9
|
+
* A secret is minted when the QR is drawn, lives for two minutes, and is
|
|
10
|
+
* consumed by the first successful claim. It is held in memory rather than in
|
|
11
|
+
* the database on purpose: a pairing that outlives the process that offered it
|
|
12
|
+
* is a pairing nobody is looking at, and a restart should invalidate the code on
|
|
13
|
+
* screen rather than leave a window open.
|
|
14
|
+
*
|
|
15
|
+
* The secret is compared by hash, so a heap dump or a log line that captured the
|
|
16
|
+
* stored value cannot be replayed as a credential.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { ConnectCandidate } from '@/shared/lib/connect/types';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* How long a code stays valid.
|
|
23
|
+
*
|
|
24
|
+
* Five minutes, not two. A camera scan takes seconds, but the image path does
|
|
25
|
+
* not: screenshot the dialog, send it to the other device, open the login screen,
|
|
26
|
+
* pick the file. Measured on this machine, that round trip took 6–16 seconds when
|
|
27
|
+
* scripted and minutes when a human does it — and a code that expires mid-transfer
|
|
28
|
+
* reads as "this QR is not a Kabmo code", which is the wrong diagnosis.
|
|
29
|
+
*
|
|
30
|
+
* The window is still short enough that a photographed code is worthless by the
|
|
31
|
+
* time anyone could act on it, and it is single-use regardless.
|
|
32
|
+
*/
|
|
33
|
+
export const PAIRING_TTL_MS = 5 * 60 * 1000;
|
|
34
|
+
|
|
35
|
+
export interface PendingPairing {
|
|
36
|
+
pairingId: string;
|
|
37
|
+
secretHash: string;
|
|
38
|
+
candidates: ConnectCandidate[];
|
|
39
|
+
expiresAt: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
declare global {
|
|
43
|
+
// eslint-disable-next-line no-var
|
|
44
|
+
var __kabmoPairings: Map<string, PendingPairing> | undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function store(): Map<string, PendingPairing> {
|
|
48
|
+
globalThis.__kabmoPairings ??= new Map();
|
|
49
|
+
return globalThis.__kabmoPairings;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function hashSecret(secret: string): string {
|
|
53
|
+
return new Bun.CryptoHasher('sha256').update(secret).digest('hex');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Drop expired entries. Called on every mint and claim, so the map stays small. */
|
|
57
|
+
function sweep(now: number): void {
|
|
58
|
+
for (const [id, entry] of store()) {
|
|
59
|
+
if (entry.expiresAt <= now) store().delete(id);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Mint a pairing and return the plaintext secret exactly once. */
|
|
64
|
+
export function createPairing(candidates: ConnectCandidate[]): {
|
|
65
|
+
pairingId: string;
|
|
66
|
+
secret: string;
|
|
67
|
+
expiresAt: string;
|
|
68
|
+
} {
|
|
69
|
+
const now = Date.now();
|
|
70
|
+
sweep(now);
|
|
71
|
+
const pairingId = crypto.randomUUID();
|
|
72
|
+
const secret = Buffer.from(crypto.getRandomValues(new Uint8Array(32))).toString('base64url');
|
|
73
|
+
const expiresAt = now + PAIRING_TTL_MS;
|
|
74
|
+
store().set(pairingId, {
|
|
75
|
+
pairingId,
|
|
76
|
+
secretHash: hashSecret(secret),
|
|
77
|
+
candidates,
|
|
78
|
+
expiresAt,
|
|
79
|
+
});
|
|
80
|
+
return { pairingId, secret, expiresAt: new Date(expiresAt).toISOString() };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Consume a pairing, or answer why it cannot be consumed.
|
|
85
|
+
*
|
|
86
|
+
* Single use is enforced by DELETING the entry before the caller does anything
|
|
87
|
+
* with it: a claim that fails afterwards (a database error) costs the user a new
|
|
88
|
+
* QR, which is the safe direction. The alternative — delete after success —
|
|
89
|
+
* leaves a window where two devices redeem the same code.
|
|
90
|
+
*/
|
|
91
|
+
export function consumePairing(
|
|
92
|
+
pairingId: string,
|
|
93
|
+
secret: string,
|
|
94
|
+
): { ok: true; candidates: ConnectCandidate[] } | { ok: false; reason: string } {
|
|
95
|
+
const now = Date.now();
|
|
96
|
+
sweep(now);
|
|
97
|
+
const entry = store().get(pairingId);
|
|
98
|
+
if (!entry) return { ok: false, reason: 'This pairing code has expired or was already used.' };
|
|
99
|
+
if (entry.expiresAt <= now) {
|
|
100
|
+
store().delete(pairingId);
|
|
101
|
+
return { ok: false, reason: 'This pairing code has expired.' };
|
|
102
|
+
}
|
|
103
|
+
if (entry.secretHash !== hashSecret(secret)) {
|
|
104
|
+
return { ok: false, reason: 'This pairing code does not match the one this server issued.' };
|
|
105
|
+
}
|
|
106
|
+
store().delete(pairingId);
|
|
107
|
+
return { ok: true, candidates: entry.candidates };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Drop every pending pairing. The tests use it; a running server has no reason to. */
|
|
111
|
+
export function resetPairings(): void {
|
|
112
|
+
globalThis.__kabmoPairings = undefined;
|
|
113
|
+
}
|