Skip to content

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

Add the first Client System permission

zohayrslileh0d87b82e2ff3
diff --git a/scripts/contracts/client-system-access.ts b/scripts/contracts/client-system-access.ts
new file mode 100644
index 0000000..720c9a2
--- /dev/null
+++ b/scripts/contracts/client-system-access.ts
@@ -0,0 +1,38 @@
+import assert from "node:assert/strict"
+import type AuthManager from "@client/core/link-manager/auth-manager/auth-manager"
+import SystemAccess from "@client/view/components/desktop-host/system-access"
+
+const owner = { identity: "process:owner", program: "owner" }
+const sibling = { identity: "process:sibling", program: "owner" }
+const outside = { identity: "process:outside", program: "outside" }
+let unrestricted = false
+
+const authManager = {
+ processManager: { processes: new Map([[owner.identity, owner]]) },
+ async grantsPermission(process: string, name: string, values: readonly string[]) {
+
+ assert.equal(process, owner.identity)
+ assert.equal(name, "system")
+ assert.deepEqual(values, ["all"])
+
+ return unrestricted
+ }
+} as unknown as AuthManager
+
+const access = new SystemAccess(authManager, owner.identity)
+const ownProgram = { identity: "owner" }
+
+assert(access.ownsProgram({ identity: "owner" }))
+assert(!access.ownsProgram({ identity: "outside" }))
+assert(access.ownsProcess(sibling as never))
+assert(!access.ownsProcess(outside as never))
+assert.equal(await access.program(ownProgram as never), ownProgram)
+await assert.rejects(access.program({ identity: "outside" } as never), /Execution is not permitted/)
+await assert.rejects(access.process(outside as never), /Execution is not permitted/)
+await assert.rejects(access.requireAll(), /Execution is not permitted/)
+
+unrestricted = true
+
+assert.equal((await access.program({ identity: "outside" } as never)).identity, "outside")
+assert.equal((await access.process(outside as never)).identity, outside.identity)
+await access.requireAll()
diff --git a/scripts/contracts/permissions.ts b/scripts/contracts/permissions.ts
index aab7fc7..fa0b67e 100644
--- a/scripts/contracts/permissions.ts
+++ b/scripts/contracts/permissions.ts
@@ -64,4 +64,16 @@ assert.throws(() => new PermissionCatalog({
}
}), /invalid default/)
+assert.deepEqual(permissionCatalog.definition("system"), {
+ values: ["all"],
+ default: ["all"],
+ title: "System",
+ description: "Access every System capability."
+})
+assert.deepEqual(permissionCatalog.resolve("system", true), ["all"])
+assert.deepEqual(permissionCatalog.declarations({ system: true }), { system: ["all"] })
+assert(permissionCatalog.grants(["all"], ["all"]))
+assert(!permissionCatalog.grants(null, ["all"]))
+assert(!permissionCatalog.needReload("system", null, ["all"]))
+assert(!permissionCatalog.needReload("system", ["all"], null))
assert.throws(() => permissionCatalog.definition("files"), /does not know/)
diff --git a/source/client/core/link-manager/auth-manager/auth-manager.ts b/source/client/core/link-manager/auth-manager/auth-manager.ts
index 2bf6788..758ecb4 100644
--- a/source/client/core/link-manager/auth-manager/auth-manager.ts
+++ b/source/client/core/link-manager/auth-manager/auth-manager.ts
@@ -63,6 +63,11 @@ export default class AuthManager extends TheLink {
return await this.$outbound.publishFirst("/permission/get", process, name)
}
+ public async grantsPermission(process: string, name: string, requested: readonly string[]) {
+
+ return await this.$outbound.publishFirst("/permission/grants", process, name, requested) as boolean
+ }
+
public async requestPermission(process: string, request: string, name: string, permission: PermissionRequest) {
return await this.$outbound.publishFirst("/permission/request", request, process, name, permission) as PermissionChange
diff --git a/source/client/view/components/desktop-host/announcements.ts b/source/client/view/components/desktop-host/announcements.ts
index c8f4814..a72f28e 100644
--- a/source/client/view/components/desktop-host/announcements.ts
+++ b/source/client/view/components/desktop-host/announcements.ts
@@ -5,6 +5,7 @@ import { useCallback } from "react"
import ClientProcessBoundary from "./client-process-boundary"
import ClientTraffic from "./client-traffic"
import { sdkProcess, sdkProgram } from "./sdk-records"
+import SystemAccess from "./system-access"
/** Projects authoritative System announcements into every Client frame. */
export default function useAnnouncements(authManager: AuthManager, panes: Map<string, ClientProcessBoundary>, traffic: ClientTraffic) {
@@ -12,11 +13,20 @@ export default function useAnnouncements(authManager: AuthManager, panes: Map<st
const processes = ReactTunnel.useFactory(authManager.processManager.$inbound)
const programs = ReactTunnel.useFactory(authManager.programManager.$inbound)
- const post = useCallback(function (route: string, ...message: unknown[]) {
+ const post = useCallback(function (program: string, route: string, ...message: unknown[]) {
- for (const pane of panes.keys()) traffic.emit(pane, route, ...message).catch(() => undefined)
+ for (const pane of panes.keys()) {
- }, [panes, traffic])
+ const access = new SystemAccess(authManager, pane)
+
+ if (access.ownsProgram({ identity: program })) traffic.emit(pane, route, ...message).catch(() => undefined)
+ else access.all().then(granted => {
+
+ if (granted) return traffic.emit(pane, route, ...message)
+ }).catch(() => undefined)
+ }
+
+ }, [authManager, panes, traffic])
processes.useSubscribe("/created", useCallback((payload: ProcessRecord | null) => {
@@ -24,8 +34,8 @@ export default function useAnnouncements(authManager: AuthManager, panes: Map<st
const record = processRecord(authManager, payload)
- post("host-process", "create", payload.program, record)
- post("program-process", "create", program(authManager, payload.program).reference, record)
+ post(payload.program, "host-process", "create", payload.program, record)
+ post(payload.program, "program-process", "create", program(authManager, payload.program).reference, record)
}, [authManager, post]))
@@ -35,9 +45,9 @@ export default function useAnnouncements(authManager: AuthManager, panes: Map<st
const record = processRecord(authManager, payload)
- post("host-process", "exit", payload.program, record, code, signal)
- post("program-process", "exit", program(authManager, payload.program).reference, record, code, signal)
- post("process-host", "exit", payload.reference, code, signal)
+ post(payload.program, "host-process", "exit", payload.program, record, code, signal)
+ post(payload.program, "program-process", "exit", program(authManager, payload.program).reference, record, code, signal)
+ post(payload.program, "process-host", "exit", payload.reference, code, signal)
}, [authManager, post]))
@@ -45,7 +55,7 @@ export default function useAnnouncements(authManager: AuthManager, panes: Map<st
if (!payload) return
- post("process-host", event, payload.reference, processRecord(authManager, payload), endpoint)
+ post(payload.program, "process-host", event, payload.reference, processRecord(authManager, payload), endpoint)
}, [authManager, post])
@@ -58,7 +68,7 @@ export default function useAnnouncements(authManager: AuthManager, panes: Map<st
const target = authManager.processManager.processes.get(identity)
- if (target) post("host-end", event, target.reference, value)
+ if (target) post(target.program, "host-end", event, target.reference, value)
}, [authManager, post]))
@@ -68,10 +78,10 @@ export default function useAnnouncements(authManager: AuthManager, panes: Map<st
const record = sdkProgram(entry)
- post("host-program", event, entry.identity, record, everything === true)
+ post(entry.identity, "host-program", event, entry.identity, record, everything === true)
- if (event === "uninstall") post("program-host", event, entry.reference, everything === true)
- if (event === "forget") post("program-host", event, entry.reference)
+ if (event === "uninstall") post(entry.identity, "program-host", event, entry.reference, everything === true)
+ if (event === "forget") post(entry.identity, "program-host", event, entry.reference)
}, [post])
diff --git a/source/client/view/components/desktop-host/client-process-boundary.ts b/source/client/view/components/desktop-host/client-process-boundary.ts
index 099a8eb..c50cb84 100644
--- a/source/client/view/components/desktop-host/client-process-boundary.ts
+++ b/source/client/view/components/desktop-host/client-process-boundary.ts
@@ -8,6 +8,7 @@ import { type DesktopSurfaceSnapshot } from "@phreshos/core"
import { type LocalWindowHost } from "./local-window"
import messagepack from "@libs/messagepack"
import { sdkProcess, type SdkProcessSource } from "./sdk-records"
+import SystemAccess from "./system-access"
/** The desktop boundary around one iframe execution endpoint. */
export default class ClientProcessBoundary extends TheLink {
@@ -24,6 +25,8 @@ export default class ClientProcessBoundary extends TheLink {
private readonly localWindow: LocalWindowHost
+ private readonly systemAccess: SystemAccess
+
private readonly subscriptions = new Map<string, EndpointSubscription>()
private readonly expected = new Set<string>()
@@ -34,6 +37,9 @@ export default class ClientProcessBoundary extends TheLink {
private readonly trafficSubscriptions = new Map<string, TrafficSubscription>()
+ /** Program ownership of Endpoint subscriptions; `null` marks a Service. */
+ private readonly systemSubscriptions = new Map<string, string | null>()
+
private readonly desktopPreferencesSubscriptions = new Set<string>()
private stopDesktopPreferences: (() => void) | null = null
@@ -70,6 +76,8 @@ export default class ClientProcessBoundary extends TheLink {
this.localWindow = localWindow
+ this.systemAccess = new SystemAccess(authManager, pane)
+
this.$outbound.forwardTo((route, ...values) => {
this.send([route, ...values])
@@ -223,6 +231,8 @@ export default class ClientProcessBoundary extends TheLink {
})
}
+ this.trackSystemSubscription(values)
+
host(this.authManager, this.pane, this.desktop, () => this.owner, this.localWindow)(values[0], ...values.slice(1)).catch((error: Error) => {
if (values[0] === "observe" && typeof values[1] === "string" && values[6] === true) {
@@ -239,12 +249,37 @@ export default class ClientProcessBoundary extends TheLink {
return
}
+ if (values[0] === "service-follow" && typeof values[1] === "string" && values[5] === true) {
+
+ this.impossible(values[1], error.message)
+
+ return
+ }
+
+ if (values[0] === "ask" && typeof values[3] === "string") {
+
+ this.requests.delete(values[3])
+ this.deliver("end-end", "answer", values[3], values[4], values[5], failed(error)).catch(() => undefined)
+
+ return
+ }
+
+ if (values[0] === "service-ask" && typeof values[2] === "string") {
+
+ this.requests.delete(values[2])
+ this.deliver("end-end", "answer", values[2], values[3], values[4], failed(error)).catch(() => undefined)
+ }
+
})
}
/** Deliver one routed envelope only when this endpoint requested it. */
public async deliver(route: string, ...values: unknown[]) {
+ if (!await this.canDeliverSystemSubscription(route, values[0])) return
+
+ values = await this.visibleReferences(values)
+
if (values[0] === "answer" && typeof values[1] === "string") {
if (!this.expected.has(values[1])) return
@@ -307,6 +342,78 @@ export default class ClientProcessBoundary extends TheLink {
this.$outbound.publish("boundary", "impossible", subscription, reason).catch(() => undefined)
}
+ private trackSystemSubscription(values: unknown[]) {
+
+ const [operation, subscription, target] = values
+
+ if (typeof subscription !== "string") return
+
+ if (operation === "service-follow") {
+
+ this.systemSubscriptions.set(subscription, null)
+
+ return
+ }
+
+ if (operation === "observe" || operation === "follow") {
+
+ const process = target === null
+ ? this.authManager.processManager.processes.get(this.pane)
+ : isHandleAddress(target)
+ ? this.authManager.processManager.processes.get(target.identity)
+ : undefined
+
+ this.systemSubscriptions.set(subscription, process?.program ?? null)
+
+ return
+ }
+
+ if (operation === "service-unfollow" || operation === "unobserve" || operation === "unfollow") {
+
+ this.systemSubscriptions.delete(subscription)
+ }
+ }
+
+ private async canDeliverSystemSubscription(route: string, subscription: unknown) {
+
+ if (route !== "observed" && route !== "emitted" && route !== "service-event") return true
+ if (typeof subscription !== "string" || !this.systemSubscriptions.has(subscription)) return false
+
+ const program = this.systemSubscriptions.get(subscription)
+
+ return program !== null && program !== undefined && this.systemAccess.ownsProgram({ identity: program })
+ ? true
+ : await this.systemAccess.all()
+ }
+
+ private async visibleReferences(values: unknown[]) {
+
+ let unrestricted: Promise<boolean> | null = null
+
+ const visible = async (reference: EndpointReference) => {
+
+ const process = reference.process
+
+ if (this.systemAccess.ownsProgram(process.program)) return reference
+
+ unrestricted ??= this.systemAccess.all()
+
+ return await unrestricted ? reference : null
+ }
+
+ return await Promise.all(values.map(async value => {
+
+ if (!value || typeof value !== "object" || Array.isArray(value)) return value
+
+ const envelope = value as Record<string, unknown>
+
+ if (isEndpointReference(envelope.from)) return { ...envelope, from: await visible(envelope.from) }
+ if (isEndpointReference(envelope.to)) return { ...envelope, to: await visible(envelope.to) }
+
+ return value
+ }))
+ }
+
private control(values: unknown[]) {
const [operation, ...args] = values
@@ -495,6 +602,14 @@ export default class ClientProcessBoundary extends TheLink {
if (args[0] !== "install" && args[0] !== "uninstall" && args[0] !== "run") throw new Error(`The desktop does not know the stream operation "${String(args[0])}"`)
+ if (!isHandleAddress(args[1])) throw new Error("A Program handle is required")
+
+ const program = this.authManager.programManager.programs.get(args[1].identity)
+
+ if (!program || program.reference !== args[1].reference) throw new Error("The Program represented by this handle does not exist")
+
+ await this.systemAccess.program(program)
+
const operation = this.authManager.programManager.command(args[1], args[0], args[2], this.pane)
iterator = operation[Symbol.asyncIterator]()
@@ -522,7 +637,7 @@ export default class ClientProcessBoundary extends TheLink {
})
}
- private waitReady(question: string, target: unknown, endpoint: unknown, requireCurrentIncarnation: boolean) {
+ private async waitReady(question: string, target: unknown, endpoint: unknown, requireCurrentIncarnation: boolean) {
const process = target === undefined || target === null
? this.authManager.processManager.processes.get(this.pane)
@@ -537,6 +652,15 @@ export default class ClientProcessBoundary extends TheLink {
return
}
+ try { await this.systemAccess.process(process) }
+
+ catch (error) {
+
+ this.deliver("host-end", "answer", question, failed(error)).catch(() => undefined)
+
+ return
+ }
+
if (endpoint !== "server" && endpoint !== "client") {
this.deliver("host-end", "answer", question, failed(new Error("Readiness needs an Endpoint"))).catch(() => undefined)
@@ -793,6 +917,8 @@ export default class ClientProcessBoundary extends TheLink {
this.trafficSubscriptions.clear()
+ this.systemSubscriptions.clear()
+
this.expected.clear()
this.waiting.clear()
@@ -905,6 +1031,25 @@ function isHandleAddress(value: unknown): value is { identity: string, reference
&& typeof value.identity === "string" && typeof value.reference === "string"
}
+type EndpointReference = Readonly<{
+ kind: "server" | "client"
+ process: Readonly<{ program: { identity: string } }>
+}>
+
+function isEndpointReference(value: unknown): value is EndpointReference {
+
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false
+
+ const reference = value as { kind?: unknown, process?: unknown }
+
+ if (reference.kind !== "server" && reference.kind !== "client") return false
+ if (!reference.process || typeof reference.process !== "object" || Array.isArray(reference.process)) return false
+
+ const program = (reference.process as { program?: unknown }).program
+
+ return !!program && typeof program === "object" && !Array.isArray(program) && typeof (program as { identity?: unknown }).identity === "string"
+}
+
interface WaitingQuestion {
route: string
diff --git a/source/client/view/components/desktop-host/host.ts b/source/client/view/components/desktop-host/host.ts
index eb9f8b3..821012b 100644
--- a/source/client/view/components/desktop-host/host.ts
+++ b/source/client/view/components/desktop-host/host.ts
@@ -18,6 +18,7 @@ import {
visualTransaction,
type LocalWindowHost
} from "./local-window"
+import SystemAccess from "./system-access"
/** A host answer whose stream must be transferred rather than cloned. */
export class TransferredAnswer {
@@ -30,6 +31,8 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
const { processManager, programManager } = authManager
+ const access = new SystemAccess(authManager, pane)
+
function process() {
const found = processManager.processes.get(pane)
@@ -81,6 +84,16 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return found
}
+ function permittedProgram(value: unknown) {
+
+ return access.program(holdProgram(value))
+ }
+
+ function permittedProcess(value: unknown) {
+
+ return access.process(holdProcess(value))
+ }
+
function resolveProcess(value: unknown) {
if (!isHandleAddress(value)) return null
@@ -133,17 +146,27 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// only because it was added on the other.
if (word === "current-process") return [record(process())]
- if (word === "host-program-list") return [[...programManager.programs.values()].filter(program => args[0] !== true || program.installed).map(sdkProgram)]
+ if (word === "host-program-list") {
+
+ const programs = [...programManager.programs.values()].filter(program => args[0] !== true || program.installed)
+ const visible = await access.all() ? programs : programs.filter(program => access.ownsProgram(program))
+
+ return [visible.map(sdkProgram)]
+ }
if (word === "host-program-find") {
const program = programManager.programs.get(String(args[0]))
+ if (program) await access.program(program)
+
return [program ? sdkProgram(program) : null]
}
if (word === "host-program-create" || word === "host-program-force-create") {
+ await access.requireAll()
+
const identity = word === "host-program-create"
? await programManager.create(args[0])
: await programManager.forceCreate(args[0], pane)
@@ -151,12 +174,20 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return [sdkProgram(requireProgram(identity))]
}
- if (word === "host-process-list") return [[...processManager.processes.values()].map(record)]
+ if (word === "host-process-list") {
+
+ const processes = [...processManager.processes.values()]
+ const visible = await access.all() ? processes : processes.filter(process => access.ownsProcess(process))
+
+ return [visible.map(record)]
+ }
if (word === "host-process-find") {
const found = processManager.processes.get(String(args[0]))
+ if (found) await access.process(found)
+
return [found ? record(found) : null]
}
@@ -164,6 +195,8 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "updateAppearance") {
+ await access.requireAll()
+
await authManager.updateAppearance(args[0])
return []
@@ -182,7 +215,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "exists") {
- const target = holdProcess(args[1])
+ const target = await permittedProcess(args[1])
if (args[0] === "server") return [target.server !== null]
@@ -193,7 +226,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "start-endpoint") {
- const target = holdProcess(args[0])
+ const target = await permittedProcess(args[0])
if (args[1] !== "server" && args[1] !== "client") throw new Error("A Process endpoint is server or client")
@@ -204,7 +237,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "stop-endpoint") {
- const target = holdProcess(args[0])
+ const target = await permittedProcess(args[0])
if (args[1] !== "server" && args[1] !== "client") throw new Error("A Process endpoint is server or client")
@@ -222,7 +255,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "is-service") {
- const target = holdProcess(args[1])
+ const target = await permittedProcess(args[1])
const endpoint = args[0] ?? "client"
if (endpoint !== "server" && endpoint !== "client") throw new Error("A Process endpoint is server or client")
@@ -232,6 +265,8 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "service-exists") {
+ await access.requireAll()
+
if (!isServiceKey(args[0])) throw new Error("A complete service key is required")
return [await processManager.serviceExists(args[0])]
@@ -239,6 +274,8 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "service-wait-ready") {
+ await access.requireAll()
+
if (!isServiceKey(args[0])) throw new Error("A complete service key is required")
return [await processManager.waitServiceReady(args[0], args[1] as number | undefined)]
@@ -246,6 +283,8 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "service-follow") {
+ await access.requireAll()
+
const [subscription, key, scope, event] = args
if (typeof subscription !== "string" || !isServiceKey(key)) return []
@@ -272,6 +311,8 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "service-send") {
+ await access.requireAll()
+
if (!isServiceKey(args[0]) || typeof args[1] !== "string") return []
await processManager.sendService(pane, args[0], args[1], args[2])
@@ -281,6 +322,8 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "service-ask") {
+ await access.requireAll()
+
if (!isServiceKey(args[0]) || args[0].endpoint !== "server") throw new Error("Only a Server service can be asked")
await processManager.askService(pane, args[0], args.slice(1))
@@ -291,11 +334,13 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// A Process parent is resolved by its exact retained address.
if (word === "parent") {
- const target = holdProcess(args[0])
+ const target = await permittedProcess(args[0])
const parent = target.parent ? resolveProcess(target.parent) : null
if (target.parent && !parent) throw new Error("The parent Process no longer exists")
+ if (parent) await access.process(parent)
+
return [parent ? record(parent) : null]
}
@@ -304,15 +349,17 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (!isHandleAddress(args[0])) throw new Error("The boundary returned an invalid Process handle")
- if (!isHandleAddress(args[0])) throw new Error("A Process handle is required")
+ const current = resolveProcess(args[0])
+
+ if (current) await access.process(current)
- return [resolveProcess(args[0]) === null]
+ return [current === null]
}
// Every live Process belonging to one exact Program handle.
if (word === "program-process-list") {
- const program = holdProgram(args[0])
+ const program = await permittedProgram(args[0])
return [[...processManager.processes.values()].filter(entry => entry.program === program.identity).map(record)]
}
@@ -321,7 +368,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// living program-local name. An exact identity always wins.
if (word === "program-process-find") {
- const program = holdProgram(args[0])
+ const program = await permittedProgram(args[0])
const wanted = String(args[1])
const exact = processManager.processes.get(wanted)
const found = exact?.program === program.identity
@@ -334,7 +381,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// Create another Process from one exact Program handle.
if (word === "program-process-create") {
- const program = holdProgram(args[0])
+ const program = await permittedProgram(args[0])
const started = await programManager.createProcess(address(program), args[1] as Launch, process().identity)
return [record(requireProcess(started))]
@@ -342,7 +389,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "program-process-find-or-create") {
- const program = holdProgram(args[0])
+ const program = await permittedProgram(args[0])
const launch = args[1] as Launch & { name: string }
const found = await programManager.findOrCreateProcess(address(program), launch, process().identity)
@@ -352,7 +399,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// Every instance ended, the asker last. One implementation on the
// core serves this and a server half both.
- if (word === "program-process-exit-all") return [await processManager.exitAll(holdProgram(args[0]).identity, pane)]
+ if (word === "program-process-exit-all") return [await processManager.exitAll((await permittedProgram(args[0])).identity, pane)]
if (word === "observe") {
@@ -381,6 +428,13 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return []
}
+ if (!access.ownsProcess(target) && !await access.all()) {
+
+ if (reportImpossible) throw new Error("Execution is not permitted")
+
+ return []
+ }
+
if (!programOf(target)[half]) {
if (reportImpossible) throw new Error(`This program declared no ${half} half`)
@@ -425,6 +479,13 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return []
}
+ if (!access.ownsProcess(target) && !await access.all()) {
+
+ if (reportImpossible) throw new Error("Execution is not permitted")
+
+ return []
+ }
+
if (!programOf(target)[half]) {
if (reportImpossible) throw new Error(`This program declared no ${half} half`)
@@ -465,7 +526,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (args[1] !== "server" && args[1] !== "client") throw new Error(`A process has no "${String(args[1])}" end`)
- await processManager.publish(pane, holdProcess(args[0]).identity, args[1], args.slice(2))
+ await processManager.publish(pane, (await permittedProcess(args[0])).identity, args[1], args.slice(2))
return []
}
@@ -477,7 +538,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (args[1] !== "server") throw new Error("Only a server end can be asked — a client end has no one answerer")
- await processManager.askOf(pane, holdProcess(args[0]).identity, args.slice(2))
+ await processManager.askOf(pane, (await permittedProcess(args[0])).identity, args.slice(2))
return []
}
@@ -485,7 +546,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// What a launch said. An empty subject is this frame's Process.
if (word === "option") {
- const found = holdProcess(args[0])
+ const found = await permittedProcess(args[0])
return [found.options[String(args[1])]]
}
@@ -512,6 +573,8 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "program-permissions") {
+ await access.requireAll()
+
const operation = args[1]
if (operation !== "all" && operation !== "get" && operation !== "set" && operation !== "delete") throw new Error(`The System does not know the Program permission operation "${String(operation)}"`)
@@ -519,10 +582,17 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return [await programManager.permissions(address(holdProgram(args[0])), operation, typeof args[2] === "string" ? args[2] : undefined, args[3] as never)]
}
- if (word === "startup") return [await programManager.startup(args[0], String(args[1]), args[2])]
+ if (word === "startup") {
+
+ const program = await permittedProgram(args[0])
+
+ return [await programManager.startup(address(program), String(args[1]), args[2])]
+ }
if (word === "fork") {
+ await access.requireAll()
+
const identity = await programManager.fork(args[0], String(args[1]))
const program = programManager.programs.get(identity)
@@ -531,20 +601,20 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return [sdkProgram(program)]
}
- if (word === "program-agent") return [await programManager.agent(address(holdProgram(args[0])))]
+ if (word === "program-agent") return [await programManager.agent(address(await permittedProgram(args[0])))]
// Icon bytes are requested only when a concrete Program handle asks.
- if (word === "icon") return [await programManager.icon(address(holdProgram(args[0])), args[1] as ProgramIconSize)]
+ if (word === "icon") return [await programManager.icon(address(await permittedProgram(args[0])), args[1] as ProgramIconSize)]
// Installation state and lifecycle operations of one held Program.
if (word === "installed") {
- return [holdProgram(args[0]).installed === true]
+ return [(await permittedProgram(args[0])).installed === true]
}
if (word === "forget") {
- const program = holdProgram(args[0])
+ const program = await permittedProgram(args[0])
return [await program.forget(pane)]
}
@@ -556,7 +626,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// was being used to ask.
if (word === "window") {
- const target = holdProcess(args[0])
+ const target = await permittedProcess(args[0])
const shown = clientOf(target).window
return [{
@@ -584,21 +654,21 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "move") {
- await clientOf(holdProcess(args[0])).window.move(args[1] as never)
+ await clientOf(await permittedProcess(args[0])).window.move(args[1] as never)
return [pane]
}
if (word === "resize") {
- await clientOf(holdProcess(args[0])).window.resize(args[1] as never)
+ await clientOf(await permittedProcess(args[0])).window.resize(args[1] as never)
return [pane]
}
if (word === "setGeometry") {
- await clientOf(holdProcess(args[0])).window.setGeometry(args[1] as never)
+ await clientOf(await permittedProcess(args[0])).window.setGeometry(args[1] as never)
return [pane]
}
@@ -650,28 +720,28 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "changeTitle") {
- await clientOf(holdProcess(args[0])).window.changeTitle(String(args[1] ?? ""))
+ await clientOf(await permittedProcess(args[0])).window.changeTitle(String(args[1] ?? ""))
return [pane]
}
if (word === "raise") {
- await clientOf(holdProcess(args[0])).window.raise()
+ await clientOf(await permittedProcess(args[0])).window.raise()
return [pane]
}
if (word === "minimize") {
- await clientOf(holdProcess(args[0])).window.minimize(args[1] !== false)
+ await clientOf(await permittedProcess(args[0])).window.minimize(args[1] !== false)
return [pane]
}
if (word === "exit") {
- await holdProcess(args[0]).exit()
+ await (await permittedProcess(args[0])).exit()
return [pane]
}
@@ -680,7 +750,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
const [subject, operation, key, value, ttl] = args as [unknown, string, string, unknown, number | undefined]
- return [await programManager.store(address(holdProgram(subject)), operation, key, value, ttl)]
+ return [await programManager.store(address(await permittedProgram(subject)), operation, key, value, ttl)]
}
// This program's places. Metadata operations remain ordinary values;
@@ -693,7 +763,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
const operation = String(args[1])
- const program = holdProgram(args[0])
+ const program = await permittedProgram(args[0])
if (operation === "stream" || operation === "write") {
@@ -744,22 +814,26 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// Read-only logs belonging to one exact Program.
if (word === "logs") {
- return [await programManager.logs(address(holdProgram(args[0])), String(args[1]), Array.isArray(args[2]) ? args[2] : [])]
+ return [await programManager.logs(address(await permittedProgram(args[0])), String(args[1]), Array.isArray(args[2]) ? args[2] : [])]
}
// Read and write one exact Program database.
if (word === "database") {
- return [await programManager.database(address(holdProgram(args[0])), String(args[1]), Array.isArray(args[2]) ? args[2] : [])]
+ return [await programManager.database(address(await permittedProgram(args[0])), String(args[1]), Array.isArray(args[2]) ? args[2] : [])]
}
if (word === "host-storage") {
+ await access.requireAll()
+
return [await authManager.storage(String(args[0]), args.slice(1).map(String))]
}
if (word === "host-storage-stream" || word === "host-storage-write") {
+ await access.requireAll()
+
const path = args[0]
if (!Array.isArray(path) || path.some(part => typeof part !== "string")) throw new Error("A storage path is a list of names")
@@ -802,6 +876,8 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// one opaque file key; it never receives a filesystem path or route.
if (word === "uploads") {
+ await access.requireAll()
+
const operation = args[0]
if (operation === "stat") {
@@ -882,6 +958,8 @@ 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 { control, controller } = cancellation(args[2], "fetch")
let response: ProxiedResponse
diff --git a/source/client/view/components/desktop-host/system-access.ts b/source/client/view/components/desktop-host/system-access.ts
new file mode 100644
index 0000000..076f7f6
--- /dev/null
+++ b/source/client/view/components/desktop-host/system-access.ts
@@ -0,0 +1,54 @@
+import type AuthManager from "@client/core/link-manager/auth-manager/auth-manager"
+import type Program from "@client/core/link-manager/auth-manager/program-manager/program"
+import type Process from "@client/core/link-manager/auth-manager/process-manager/process"
+
+const denied = "Execution is not permitted"
+
+/** The System authority available to one structurally identified Client frame. */
+export default class SystemAccess {
+
+ public constructor(private readonly authManager: AuthManager, private readonly pane: string) { }
+
+ public ownsProgram(program: Pick<Program, "identity">) {
+
+ return this.owner().program === program.identity
+ }
+
+ public ownsProcess(process: Pick<Process, "program">) {
+
+ return this.owner().program === process.program
+ }
+
+ public async all() {
+
+ return await this.authManager.grantsPermission(this.pane, "system", ["all"])
+ }
+
+ public async program(program: Program) {
+
+ if (!this.ownsProgram(program) && !await this.all()) throw new Error(denied)
+
+ return program
+ }
+
+ public async process(process: Process) {
+
+ if (!this.ownsProcess(process) && !await this.all()) throw new Error(denied)
+
+ return process
+ }
+
+ public async requireAll() {
+
+ if (!await this.all()) throw new Error(denied)
+ }
+
+ private owner() {
+
+ const process = this.authManager.processManager.processes.get(this.pane)
+
+ if (!process) throw new Error("The desktop does not know this process")
+
+ return process
+ }
+}
diff --git a/source/server/core/link-manager/auth-manager/auth-manager.ts b/source/server/core/link-manager/auth-manager/auth-manager.ts
index 80c8574..c572877 100644
--- a/source/server/core/link-manager/auth-manager/auth-manager.ts
+++ b/source/server/core/link-manager/auth-manager/auth-manager.ts
@@ -121,6 +121,16 @@ export default class AuthManager extends TheLink {
return this.processManager.permission(process, name)
}
+ @Connect("/permission/grants")
+ protected async grantsPermission(process: unknown, name: unknown, requested: unknown) {
+
+ if (typeof process !== "string" || typeof name !== "string" || !Array.isArray(requested) || requested.some(value => typeof value !== "string")) {
+ throw new Error("A permission check is invalid")
+ }
+
+ return this.processManager.grants(process, name, requested)
+ }
+
@Connect("/permission/request")
protected async requestPermission(request: unknown, process: unknown, name: unknown, permission: unknown) {
diff --git a/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts b/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts
index d52bfc4..0cc94f8 100644
--- a/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts
+++ b/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts
@@ -1356,6 +1356,12 @@ export default class ProcessManager extends TheLink {
return this.authManager.programManager.permission(this.find(identity).program, name)
}
+ /** Tests one concrete permission against this Process's effective grants. */
+ public grants(identity: string, name: string, requested: readonly string[]) {
+
+ return permissionCatalog.grants(this.effectivePermission(this.find(identity), name), requested)
+ }
+
/** Resolves one permission request through declarations, grants, then the owner. */
public async requestPermission(identity: string, request: string, name: string, input: PermissionRequest): Promise<PermissionChange> {
@@ -1370,7 +1376,7 @@ export default class ProcessManager extends TheLink {
const declared = process.program.clientPermissions[name] ?? null
const temporary = process.permissions.get(name) ?? null
const stored = this.authManager.programManager.permission(process.program, name)
- const effective = permissionCatalog.effective(name, declared, temporary, stored)
+ const effective = this.effectivePermission(process, name, stored)
if (permissionCatalog.grants(effective, requested)) return permissionChange(effective, false)
if (effective === false) return Object.freeze({ permission: false, needReload: false })
@@ -1400,6 +1406,16 @@ export default class ProcessManager extends TheLink {
return Object.freeze({ permission: choice, needReload: false })
}
+ private effectivePermission(process: Process, name: string, stored = this.authManager.programManager.permission(process.program, name)) {
+
+ return permissionCatalog.effective(
+ name,
+ process.program.clientPermissions[name] ?? null,
+ process.permissions.get(name) ?? null,
+ stored
+ )
+ }
+
public cancelPermission(identity: string, request: string) {
const process = this.processes.get(identity)
diff --git a/source/server/core/permissions.ts b/source/server/core/permissions.ts
index 1b3dc32..bf6e081 100644
--- a/source/server/core/permissions.ts
+++ b/source/server/core/permissions.ts
@@ -171,4 +171,11 @@ function unique(values: string[]) {
}
/** The complete permission domain recognized by this System release. */
-export const permissionCatalog = new PermissionCatalog({})
+export const permissionCatalog = new PermissionCatalog({
+ system: {
+ values: ["all"],
+ default: ["all"],
+ title: "System",
+ description: "Access every System capability."
+ }
+})