Skip to content

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

Allow Clients to use their Program services

zohayrslileh4fe9f0be58a3
diff --git a/scripts/contracts/client-system-access.ts b/scripts/contracts/client-system-access.ts
index d987e0f..0f7a8c8 100644
--- a/scripts/contracts/client-system-access.ts
+++ b/scripts/contracts/client-system-access.ts
@@ -8,7 +8,11 @@ const outside = { identity: "process:outside", program: "outside" }
let unrestricted = false
const authManager = {
- processManager: { processes: new Map([[owner.identity, owner]]) },
+ processManager: { processes: new Map([
+ [owner.identity, owner],
+ [sibling.identity, sibling],
+ [outside.identity, outside]
+ ]) },
async grantsPermission(process: string, name: "all", values: readonly never[]) {
assert.equal(process, owner.identity)
@@ -21,18 +25,28 @@ const authManager = {
const access = new SystemAccess(authManager, owner.identity)
const ownProgram = { identity: "owner" }
+const ownService = { program: "owner", process: "main", endpoint: "server" } as const
+const ownExactService = { process: sibling.identity, endpoint: "client" } as const
+const outsideService = { program: "outside", process: "main", endpoint: "server" } as const
assert(access.ownsProgram({ identity: "owner" }))
assert(!access.ownsProgram({ identity: "outside" }))
assert(access.ownsProcess(sibling as never))
assert(!access.ownsProcess(outside as never))
+assert(access.ownsService(ownService))
+assert(access.ownsService(ownExactService))
+assert(!access.ownsService(outsideService))
assert.equal(await access.program(ownProgram as never), ownProgram)
+assert.equal(await access.service(ownService), ownService)
+assert.equal(await access.service(ownExactService), ownExactService)
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.service(outsideService), /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)
+assert.equal(await access.service(outsideService), outsideService)
await access.requireAll()
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 83fe9b8..77fce09 100644
--- a/source/client/view/components/desktop-host/client-process-boundary.ts
+++ b/source/client/view/components/desktop-host/client-process-boundary.ts
@@ -4,7 +4,7 @@ import host, { TransferredAnswer } from "./host"
import ClientTraffic from "./client-traffic"
import { failed, succeeded } from "@server/core/outcome"
import { type TrafficKind } from "@server/core/link-manager/auth-manager/process-manager/process-traffic"
-import { type DesktopSurfaceSnapshot, type ShellOptions } from "@phreshos/core"
+import { isServiceKey, type DesktopSurfaceSnapshot, type ShellOptions } from "@phreshos/core"
import { type LocalWindowHost } from "./local-window"
import messagepack from "@libs/messagepack"
import { sdkProcess, type SdkProcessSource } from "./sdk-records"
@@ -37,7 +37,7 @@ export default class ClientProcessBoundary extends TheLink {
private readonly trafficSubscriptions = new Map<string, TrafficSubscription>()
- /** Program ownership of Endpoint subscriptions; `null` marks a Service. */
+ /** Program ownership of each System-routed subscription. */
private readonly systemSubscriptions = new Map<string, string | null>()
private readonly desktopPreferencesSubscriptions = new Set<string>()
@@ -350,7 +350,10 @@ export default class ClientProcessBoundary extends TheLink {
if (operation === "service-follow") {
- this.systemSubscriptions.set(subscription, null)
+ this.systemSubscriptions.set(
+ subscription,
+ isServiceKey(target) ? this.systemAccess.serviceProgram(target) : null
+ )
return
}
diff --git a/source/client/view/components/desktop-host/host.ts b/source/client/view/components/desktop-host/host.ts
index 3c7cdeb..8148bbf 100644
--- a/source/client/view/components/desktop-host/host.ts
+++ b/source/client/view/components/desktop-host/host.ts
@@ -102,6 +102,13 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return access.process(holdProcess(value))
}
+ function permittedService(value: unknown) {
+
+ if (!isServiceKey(value)) throw new Error("A complete service key is required")
+
+ return access.service(value)
+ }
+
function resolveProcess(value: unknown) {
if (!isHandleAddress(value)) return null
@@ -275,26 +282,20 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "service-exists") {
- await access.requireAll()
+ const service = await permittedService(args[0])
- if (!isServiceKey(args[0])) throw new Error("A complete service key is required")
-
- return [await processManager.serviceExists(args[0])]
+ return [await processManager.serviceExists(service)]
}
if (word === "service-wait-ready") {
- await access.requireAll()
-
- if (!isServiceKey(args[0])) throw new Error("A complete service key is required")
+ const service = await permittedService(args[0])
- return [await processManager.waitServiceReady(args[0], args[1] as number | undefined)]
+ return [await processManager.waitServiceReady(service, args[1] as number | undefined)]
}
if (word === "service-follow") {
- await access.requireAll()
-
const [subscription, key, scope, event] = args
if (typeof subscription !== "string" || !isServiceKey(key)) return []
@@ -303,6 +304,8 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (event !== null && typeof event !== "string") return []
+ await access.service(key)
+
const owner = frameOwner()
if (owner) await processManager.followService(pane, owner, subscription, key, scope, event)
@@ -321,22 +324,22 @@ 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])
+ const service = await access.service(args[0])
+
+ await processManager.sendService(pane, service, args[1], args[2])
return []
}
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))
+ const service = await access.service(args[0])
+
+ await processManager.askService(pane, service, args.slice(1))
return []
}
diff --git a/source/client/view/components/desktop-host/system-access.ts b/source/client/view/components/desktop-host/system-access.ts
index 3ebfc1a..09ac6a7 100644
--- a/source/client/view/components/desktop-host/system-access.ts
+++ b/source/client/view/components/desktop-host/system-access.ts
@@ -1,6 +1,7 @@
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"
+import type { ServiceKey } from "@phreshos/core"
const denied = "Execution is not permitted"
@@ -19,6 +20,16 @@ export default class SystemAccess {
return this.owner().program === process.program
}
+ public serviceProgram(service: ServiceKey) {
+
+ return service.program ?? this.authManager.processManager.processes.get(service.process)?.program ?? null
+ }
+
+ public ownsService(service: ServiceKey) {
+
+ return this.serviceProgram(service) === this.owner().program
+ }
+
public async all() {
return await this.authManager.grantsPermission(this.pane, "all", [])
@@ -38,6 +49,13 @@ export default class SystemAccess {
return process
}
+ public async service(service: ServiceKey) {
+
+ if (!this.ownsService(service) && !await this.all()) throw new Error(denied)
+
+ return service
+ }
+
public async requireAll() {
if (!await this.all()) throw new Error(denied)