Skip to content

An open-source, self-hosted system for apps built with web technologies.

Enforce scoped Client network permissions

zohayrslileh245983fd51fe
diff --git a/bun.lock b/bun.lock
index 95775fa..8189dee 100644
--- a/bun.lock
+++ b/bun.lock
@@ -7,6 +7,7 @@
"dependencies": {
"@fontsource/roboto": "^5.3.0",
"@hono/node-server": "^2.1.1",
+ "@phreshos/react-ui": "^0.1.15",
"@the-link/core": "^0.3.0",
"@the-link/http": "^0.1.1",
"@the-link/ipc": "^0.2.1",
@@ -28,7 +29,6 @@
"@babel/core": "^7.29.7",
"@babel/plugin-proposal-decorators": "^7.29.7",
"@phreshos/core": "^0.1.38",
- "@phreshos/react-ui": "^0.1.15",
"@rolldown/plugin-babel": "^0.2.3",
"@tailwindcss/vite": "^4.3.3",
"@types/adm-zip": "^0.5.8",
diff --git a/package.json b/package.json
index 9ff37b8..bb77109 100644
--- a/package.json
+++ b/package.json
@@ -37,6 +37,7 @@
"dependencies": {
"@fontsource/roboto": "^5.3.0",
"@hono/node-server": "^2.1.1",
+ "@phreshos/react-ui": "^0.1.15",
"@the-link/core": "^0.3.0",
"@the-link/http": "^0.1.1",
"@the-link/ipc": "^0.2.1",
@@ -58,7 +59,6 @@
"@babel/core": "^7.29.7",
"@babel/plugin-proposal-decorators": "^7.29.7",
"@phreshos/core": "^0.1.38",
- "@phreshos/react-ui": "^0.1.15",
"@rolldown/plugin-babel": "^0.2.3",
"@tailwindcss/vite": "^4.3.3",
"@types/adm-zip": "^0.5.8",
diff --git a/scripts/contracts/client-system-access.ts b/scripts/contracts/client-system-access.ts
index 28d0188..3edd4b3 100644
--- a/scripts/contracts/client-system-access.ts
+++ b/scripts/contracts/client-system-access.ts
@@ -44,6 +44,7 @@ await assert.rejects(access.program(outsideProgram as never), /Execution is not
await assert.rejects(access.process(outside as never), /Execution is not permitted/)
await assert.rejects(access.service(outsideService), /Execution is not permitted/)
await assert.rejects(access.requirePrograms(), /Execution is not permitted/)
+await assert.rejects(access.requireNetwork("https://api.example.com/v1/users"), /Execution is not permitted/)
await assert.rejects(access.require("appearance", []), /Execution is not permitted/)
await assert.rejects(access.require("desktopPreferences", []), /Execution is not permitted/)
@@ -79,6 +80,12 @@ await access.require("appearance", [])
await access.require("desktopPreferences", [])
await assert.rejects(access.program(outsideProgram as never), /Execution is not permitted/)
+permissions = { network: ["https://*.example.com/v1/**"] }
+
+await access.requireNetwork("https://api.example.com/v1/users")
+await assert.rejects(access.requireNetwork("https://example.com/v1/users"), /Execution is not permitted/)
+await assert.rejects(access.requireNetwork("https://api.example.com/v2/users"), /Execution is not permitted/)
+
permissions = { all: [] }
assert.equal((await access.program(outsideProgram as never)).identity, "outside")
@@ -87,4 +94,5 @@ assert.equal(await access.service(outsideService), outsideService)
await access.requirePrograms()
await access.require("appearance", [])
await access.require("desktopPreferences", [])
+await access.requireNetwork("wss://events.example.com/socket")
await access.requireAll()
diff --git a/scripts/contracts/permissions.ts b/scripts/contracts/permissions.ts
index b2e7f05..eb557fe 100644
--- a/scripts/contracts/permissions.ts
+++ b/scripts/contracts/permissions.ts
@@ -19,6 +19,11 @@ const catalog = new PermissionCatalog({
title: "Programs",
description: "Access every Program or selected Programs and their Services."
},
+ network: {
+ default: [],
+ title: "Network",
+ description: "Use System networking with every request target or selected target scopes."
+ },
appearance: {
default: [],
title: "Appearance",
@@ -43,10 +48,17 @@ assert.deepEqual(catalog.definition("programs"), {
title: "Programs",
description: "Access every Program or selected Programs and their Services."
})
+assert.deepEqual(catalog.definition("network"), {
+ valueDomain: "network",
+ default: [],
+ title: "Network",
+ description: "Use System networking with every request target or selected target scopes."
+})
assert.deepEqual(catalog.resolve("all", true), [])
assert.deepEqual(catalog.resolve("all", []), [])
assert.deepEqual(catalog.resolve("services", true), [])
assert.deepEqual(catalog.resolve("services", ["flambo", "terminal", "flambo"]), ["flambo", "terminal"])
+assert.deepEqual(catalog.resolve("network", ["HTTPS://API.Example.com:443/v1/**"]), ["https://api.example.com/v1/**"])
assert.equal(catalog.resolve("all", false), false)
assert.equal(catalog.resolve("all", null), null)
@@ -62,6 +74,10 @@ assert(catalog.grants("services", [], ["flambo"]))
assert(catalog.grants("services", ["flambo"], ["flambo"]))
assert(!catalog.grants("services", ["flambo"], ["terminal"]))
assert(!catalog.grants("services", ["flambo"], []))
+assert(catalog.grants("network", ["https://api.example.com"], ["https://api.example.com/v1/users"]))
+assert(catalog.grants("network", ["https://*.example.com/v1/**"], ["https://eu.example.com/v1/users"]))
+assert(!catalog.grants("network", ["https://*.example.com/v1/**"], ["https://example.com/v1/users"]))
+assert(!catalog.grants("network", ["https://api.example.com/v1/**"], ["https://api.example.com/v2/users"]))
assert(catalog.allows("all", [], null, []))
assert(!catalog.allows("all", null, null, []))
assert(catalog.allows("services", [], null, ["flambo"]))
@@ -70,12 +86,20 @@ assert.equal(catalog.combine("all", null, false), null)
assert.deepEqual(catalog.combine("all", []), [])
assert.deepEqual(catalog.combine("services", ["flambo"], ["terminal"]), ["flambo", "terminal"])
assert.deepEqual(catalog.combine("services", ["flambo"], []), [])
+assert.deepEqual(catalog.combine("network", ["https://api.example.com"], ["wss://events.example.com"]), [
+ "https://api.example.com",
+ "wss://events.example.com"
+])
assert.equal(catalog.effective("all", null, false), false)
assert.deepEqual(catalog.effective("all", [], false), [])
assert.deepEqual(catalog.merge("all", null, []), [])
assert.deepEqual(catalog.merge("services", ["flambo"], ["terminal"]), ["flambo", "terminal"])
assert.deepEqual(catalog.merge("services", ["flambo"], []), [])
assert.deepEqual(catalog.merge("services", [], ["flambo"]), [])
+assert.deepEqual(catalog.merge("network", ["https://api.example.com"], ["wss://events.example.com"]), [
+ "https://api.example.com",
+ "wss://events.example.com"
+])
assert(!catalog.changed([], []))
assert(!catalog.changed(["flambo", "terminal"], ["terminal", "flambo"]))
assert(catalog.changed(["flambo"], []))
@@ -85,6 +109,7 @@ assert(catalog.needReload("all", [], null))
assert(!catalog.needReload("all", [], []))
assert(!catalog.needReload("services", null, []))
assert(!catalog.needReload("programs", ["flambo"], []))
+assert(!catalog.needReload("network", null, []))
assert(!catalog.needReload("appearance", null, []))
assert(!catalog.needReload("desktopPreferences", null, []))
@@ -92,6 +117,7 @@ assert.throws(() => parsePermissionName("files"), /does not know/)
assert.throws(() => catalog.resolve("all", ["unknown"]), /unknown value/)
assert.throws(() => catalog.resolve("appearance", ["flambo"]), /unknown value/)
assert.throws(() => catalog.resolve("services", ["Not a Program"]), /unknown value/)
+assert.throws(() => catalog.resolve("network", ["api.example.com"]), /unknown value/)
assert.throws(() => catalog.declarations({ all: ["unknown"] }), /unknown value/)
assert.throws(() => catalog.stored({ all: true }), /unresolved shorthand/)
assert.throws(() => new PermissionCatalog({} as never), /needs a definition/)
@@ -103,6 +129,7 @@ assert.throws(() => new PermissionCatalog({
},
services: catalog.definition("services"),
programs: catalog.definition("programs"),
+ network: catalog.definition("network"),
appearance: catalog.definition("appearance"),
desktopPreferences: catalog.definition("desktopPreferences")
} as never), /invalid default/)
diff --git a/source/client/core/application.ts b/source/client/core/application.ts
index 760365f..024dc49 100644
--- a/source/client/core/application.ts
+++ b/source/client/core/application.ts
@@ -76,7 +76,7 @@ export default class Application {
return await response.json()
}
- /** Carry an unrestricted request through the desktop's authorized proxy. */
+ /** Carry one authorized System request through the desktop proxy. */
public async proxy(request: ProxyRequest, body: ClientBody | null, authorization: string, signal?: AbortSignal): Promise<ProxiedResponse> {
const response = await this.post(
diff --git a/source/client/view/components/desktop-host/host.ts b/source/client/view/components/desktop-host/host.ts
index 7721daa..0ee6008 100644
--- a/source/client/view/components/desktop-host/host.ts
+++ b/source/client/view/components/desktop-host/host.ts
@@ -988,7 +988,9 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// authorization is supplied here and never becomes a transmitted value.
if (word === "fetch") {
- await access.requireAll()
+ const description = args[0] as ProxyRequest
+
+ await access.requireNetwork(description.url)
const { control, controller } = cancellation(args[2], "fetch")
@@ -998,7 +1000,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
response = await authManager.linkManager.application.proxy(
- args[0] as ProxyRequest,
+ description,
args[1] as ClientBody | null,
@@ -1026,7 +1028,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// authority before the frame creates it; no transport proxy is needed.
if (word === "websocket") {
- await access.requireAll()
+ await access.requireNetwork(String(args[0]))
return []
}
diff --git a/source/client/view/components/desktop-host/system-access.ts b/source/client/view/components/desktop-host/system-access.ts
index 174709a..073d6c7 100644
--- a/source/client/view/components/desktop-host/system-access.ts
+++ b/source/client/view/components/desktop-host/system-access.ts
@@ -89,6 +89,11 @@ export default class SystemAccess {
await this.require("programs", [])
}
+ public async requireNetwork(scope: string) {
+
+ await this.require("network", [scope])
+ }
+
public async require<Name extends PermissionName>(name: Name, values: readonly PermissionValue<Name>[]) {
if (!await this.authManager.grantsPermission(this.pane, name, values)) throw new Error(denied)
diff --git a/source/server/core/permissions.ts b/source/server/core/permissions.ts
index 6a0a3aa..1204714 100644
--- a/source/server/core/permissions.ts
+++ b/source/server/core/permissions.ts
@@ -1,5 +1,6 @@
import {
clientPermissionCatalog,
+ networkScopeCovers,
parsePermission,
parsePermissionName,
type ClientPermissions,
@@ -141,9 +142,14 @@ export class PermissionCatalog {
if (!Array.isArray(grant)) return false
- if (programScoped(this.definition(name).valueDomain)) {
+ const domain = this.definition(name).valueDomain
- return grant.length === 0 || (requested.length > 0 && requested.every(value => grant.includes(value)))
+ if (valued(domain)) {
+
+ return grant.length === 0 || (requested.length > 0 && requested.every(value => {
+
+ return grant.some(granted => valueCovers(domain, granted, value))
+ }))
}
return requested.length === 0
@@ -173,7 +179,7 @@ export class PermissionCatalog {
const present = grants.filter((grant): grant is readonly string[] => Array.isArray(grant))
if (present.length === 0) return null
- if (!programScoped(this.definition(name).valueDomain)) return []
+ if (!valued(this.definition(name).valueDomain)) return []
if (present.some(grant => grant.length === 0)) return []
return this.resolve(name, present.flat()) as PermissionValue<Name>[]
@@ -191,7 +197,7 @@ export class PermissionCatalog {
): PermissionValue<Name>[] {
if (!Array.isArray(grant)) return [...requested]
- if (programScoped(this.definition(name).valueDomain) && (grant.length === 0 || requested.length === 0)) return []
+ if (valued(this.definition(name).valueDomain) && (grant.length === 0 || requested.length === 0)) return []
return this.resolve(name, [...grant, ...requested]) as PermissionValue<Name>[]
}
@@ -251,9 +257,9 @@ function unique(values: readonly string[]) {
return [...new Set(values)]
}
-function programScoped(domain: PermissionValueDomain) {
+function valued(domain: PermissionValueDomain) {
- if (domain === "program") return true
+ if (domain === "program" || domain === "network") return true
if (domain === "none") return false
domain satisfies never
@@ -261,6 +267,17 @@ function programScoped(domain: PermissionValueDomain) {
throw new Error("The System does not know this permission value domain")
}
+function valueCovers(domain: PermissionValueDomain, grant: string, requested: string) {
+
+ if (domain === "program") return grant === requested
+ if (domain === "network") return networkScopeCovers(grant, requested)
+ if (domain === "none") return false
+
+ domain satisfies never
+
+ return false
+}
+
/** The presentation and activation rules for every Core-defined permission. */
export const permissionCatalog = new PermissionCatalog({
all: {
@@ -279,6 +296,11 @@ export const permissionCatalog = new PermissionCatalog({
title: "Programs",
description: "Access every Program or selected Programs and their Services."
},
+ network: {
+ default: [],
+ title: "Network",
+ description: "Use System networking with every request target or selected target scopes."
+ },
appearance: {
default: [],
title: "Appearance",
diff --git a/source/server/view/http/proxy.ts b/source/server/view/http/proxy.ts
index 5e2cb12..864c46f 100644
--- a/source/server/view/http/proxy.ts
+++ b/source/server/view/http/proxy.ts
@@ -3,7 +3,7 @@ import { frame, unframe } from "@libs/framing"
import Application from "@server/core/application"
import { Hono } from "hono"
-/** The authorized HTTP door for unrestricted server-side fetch. */
+/** The authorized HTTP door for server-side System fetch. */
export default function (application: Application) {
const { authManager } = application.linkManager