Skip to content

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

Implement permission assignment layers

zohayrslilehc6325dfd24c0
diff --git a/scripts/contracts/process-lifecycle.ts b/scripts/contracts/process-lifecycle.ts
index 8d761f3..905c9a6 100644
--- a/scripts/contracts/process-lifecycle.ts
+++ b/scripts/contracts/process-lifecycle.ts
@@ -51,6 +51,36 @@ async function register(manager: ProcessManager, identity: string) {
return await manager.register(identity, null, program(identity), {}, launch, null, false, null, null)
}
+// A Process permission handle represents only its temporary assignment layer.
+// Coverage within that layer includes its own `all`, but never consults the
+// Program's persistent or declared values.
+{
+ const manager = processManager()
+ const process = await register(manager, "temporary-permissions")
+
+ assert.equal(manager.processPermission(process, "network"), null)
+ assert.deepEqual(manager.processPermissions(process), {})
+ assert.equal(manager.processAllows(process, "network", ["https://api.example.com"]), false)
+ assert.equal(manager.grants(process.identity, "network", ["https://api.example.com"]), false)
+
+ await manager.setProcessPermission(process, "network", ["https://api.example.com/v1/**"])
+
+ assert.deepEqual(manager.processPermission(process, "network"), ["https://api.example.com/v1/**"])
+ assert(manager.processAllows(process, "network", ["https://api.example.com/v1/users"]))
+ assert(!manager.processAllows(process, "network", ["https://other.example.com/users"]))
+ assert(manager.grants(process.identity, "network", ["https://api.example.com/v1/users"]))
+
+ await manager.setProcessPermission(process, "all", true)
+
+ assert(manager.processAllows(process, "appearance"))
+ assert(manager.grants(process.identity, "appearance", []))
+
+ await manager.deleteProcessPermission(process, "network")
+ await manager.deleteProcessPermission(process, "all")
+
+ assert.deepEqual(manager.processPermissions(process), {})
+}
+
// Parentage retains the exact Process entity after it exits. Absence and
// ended lineage therefore remain distinct states at every System boundary.
{
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 afea55a..4eaa466 100644
--- a/source/client/core/link-manager/auth-manager/auth-manager.ts
+++ b/source/client/core/link-manager/auth-manager/auth-manager.ts
@@ -8,8 +8,7 @@ import LinkManager from "../link-manager"
import {
type PermissionChange,
type PermissionName,
- type PermissionRequest,
- type PermissionValue
+ type PermissionRequest
} from "@phreshos/core"
import ShellManager from "./shell-manager"
@@ -78,7 +77,7 @@ export default class AuthManager extends TheLink {
return await this.$outbound.publishFirst("/permission/get", process, name)
}
- public async grantsPermission<Name extends PermissionName>(process: string, name: Name, requested: readonly PermissionValue<Name>[]) {
+ public async grantsPermission<Name extends PermissionName>(process: string, name: Name, requested: PermissionRequest<Name>) {
return await this.$outbound.publishFirst("/permission/grants", process, name, requested) as boolean
}
diff --git a/source/client/core/link-manager/auth-manager/process-manager/process-manager.ts b/source/client/core/link-manager/auth-manager/process-manager/process-manager.ts
index d4b244a..aab9cb3 100644
--- a/source/client/core/link-manager/auth-manager/process-manager/process-manager.ts
+++ b/source/client/core/link-manager/auth-manager/process-manager/process-manager.ts
@@ -5,7 +5,7 @@ import { Publish, Subscribe } from "@the-link/core/decorators"
import { TheLink } from "@the-link/core"
import AuthManager from "../auth-manager"
import Process from "./process"
-import { type ClientLaunch, type ServerLaunch } from "@phreshos/core"
+import { type ClientLaunch, type PermissionInput, type PermissionName, type ServerLaunch } from "@phreshos/core"
import { type TrafficKind } from "@server/core/link-manager/auth-manager/process-manager/process-traffic"
import { type ServiceKey, type WindowLayer } from "@phreshos/core"
import { type ServiceScope } from "@server/core/link-manager/auth-manager/process-manager/endpoint-services"
@@ -192,6 +192,16 @@ export default class ProcessManager extends TheLink {
await this.$outbound.publish("/endpoint/stop", identity, which)
}
+ public async permissions<Name extends PermissionName>(
+ subject: unknown,
+ operation: "all" | "get" | "allows" | "set" | "delete",
+ name?: Name,
+ value?: PermissionInput<Name>
+ ) {
+
+ return await this.$outbound.publishFirst("/permissions", subject, operation, name, value)
+ }
+
/** Announces a locally changed counterpart to desktop representations. */
public async changed() {
diff --git a/source/client/core/link-manager/auth-manager/program-manager/program-manager.ts b/source/client/core/link-manager/auth-manager/program-manager/program-manager.ts
index ce7c196..4d87574 100644
--- a/source/client/core/link-manager/auth-manager/program-manager/program-manager.ts
+++ b/source/client/core/link-manager/auth-manager/program-manager/program-manager.ts
@@ -82,7 +82,7 @@ export default class ProgramManager extends TheLink {
public async permissions<Name extends PermissionName>(
subject: unknown,
- operation: "all" | "get" | "set" | "delete",
+ operation: "all" | "get" | "allows" | "set" | "delete",
name?: Name,
value?: Exclude<PermissionInput<Name>, null>
) {
diff --git a/source/client/view/components/desktop-host/host.ts b/source/client/view/components/desktop-host/host.ts
index f5603b7..3e5858e 100644
--- a/source/client/view/components/desktop-host/host.ts
+++ b/source/client/view/components/desktop-host/host.ts
@@ -581,6 +581,13 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "context-permission-get") return [await authManager.permission(pane, parsePermissionName(args[0]))]
+ if (word === "context-permission-allows") {
+
+ const permission = parsePermissionName(args[0])
+
+ return [await authManager.grantsPermission(pane, permission, args[1] as PermissionRequest<typeof permission>)]
+ }
+
if (word === "context-permission-request") {
if (typeof args[0] !== "string") throw new Error("A permission request needs an identity")
@@ -596,18 +603,72 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
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)}"`)
+ if (operation !== "all" && operation !== "get" && operation !== "allows" && operation !== "set" && operation !== "delete") throw new Error(`The System does not know the Program permission operation "${String(operation)}"`)
if (operation === "all") return [await programManager.permissions(address(holdProgram(args[0])), operation)]
const permission = parsePermissionName(args[2])
- return [await programManager.permissions(
+ if (operation === "allows") return [await programManager.permissions(
address(holdProgram(args[0])),
operation,
permission,
- args[3] as Exclude<PermissionInput<typeof permission>, null>
+ args[3] as PermissionRequest<typeof permission>
)]
+
+ if (operation === "set") {
+
+ await programManager.permissions(
+ address(holdProgram(args[0])),
+ operation,
+ permission,
+ args[3] as Exclude<PermissionInput<typeof permission>, null>
+ )
+
+ return []
+ }
+
+ await programManager.permissions(address(holdProgram(args[0])), operation, permission)
+
+ return []
+ }
+
+ if (word === "process-permissions") {
+
+ await access.requireAll()
+
+ const operation = args[1]
+
+ if (operation !== "all" && operation !== "get" && operation !== "allows" && operation !== "set" && operation !== "delete") throw new Error(`The System does not know the Process permission operation "${String(operation)}"`)
+
+ const target = address(holdProcess(args[0]))
+
+ if (operation === "all") return [await processManager.permissions(target, operation)]
+
+ const permission = parsePermissionName(args[2])
+
+ if (operation === "allows") return [await processManager.permissions(
+ target,
+ operation,
+ permission,
+ args[3] as PermissionRequest<typeof permission>
+ )]
+
+ if (operation === "set") {
+
+ await processManager.permissions(
+ target,
+ operation,
+ permission,
+ args[3] as Exclude<PermissionInput<typeof permission>, null>
+ )
+
+ return []
+ }
+
+ await processManager.permissions(target, operation, permission)
+
+ return []
}
if (word === "startup") {
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 2d8c9d0..98fc5bd 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
@@ -27,6 +27,7 @@ import {
type PermissionName,
type PermissionRequest,
type PermissionValue,
+ type Permissions,
type ServerLaunch,
type ServiceKey,
type WindowGeometry,
@@ -1526,6 +1527,95 @@ export default class ProcessManager extends TheLink {
return this.authManager.programManager.permission(this.find(identity).program, name)
}
+ /** Reads one assignment from this Process's temporary layer only. */
+ public processPermission<Name extends PermissionName>(process: Process, name: Name): Permission<Name> {
+
+ return clonePermission((process.permissions.get(name) ?? null) as Permission<Name>)
+ }
+
+ /** Returns an independent snapshot of this Process's temporary layer. */
+ public processPermissions(process: Process): Permissions {
+
+ return clonePermissions(Object.fromEntries(process.permissions) as Permissions)
+ }
+
+ /** Tests one request against this Process's temporary layer only. */
+ public processAllows<Name extends PermissionName>(
+ process: Process,
+ name: Name,
+ input: PermissionRequest<Name> = true
+ ) {
+
+ const requested = permissionCatalog.resolve(name, input)
+
+ if (!Array.isArray(requested)) throw new Error("A permission check must be true or a list of values")
+
+ return permissionCatalog.allows(
+ name,
+ this.processPermission(process, "all"),
+ this.processPermission(process, name),
+ requested
+ )
+ }
+
+ /** Replaces one assignment in this Process's temporary layer. */
+ public async setProcessPermission<Name extends PermissionName>(
+ process: Process,
+ name: Name,
+ input: Exclude<PermissionInput<Name>, null>
+ ) {
+
+ const before = this.processPermission(process, name)
+ const permission = permissionCatalog.resolve(name, input)
+
+ if (permission === null) throw new Error("A temporary Process permission cannot be null")
+
+ if (permissionCatalog.changed(before, permission)) process.permissions.set(name, permission)
+
+ await this.processPermissionChanged(process, name, before, permission)
+ }
+
+ /** Removes one assignment from this Process's temporary layer. */
+ public async deleteProcessPermission<Name extends PermissionName>(process: Process, name: Name) {
+
+ const before = this.processPermission(process, name)
+
+ process.permissions.delete(name)
+
+ await this.processPermissionChanged(process, name, before, null)
+ }
+
+ @Connect("/permissions")
+ protected async permissionOperations(subject: unknown, operation: unknown, name?: unknown, value?: unknown) {
+
+ const process = this.system.holdProcess(subject)
+
+ if (operation === "all") return this.processPermissions(process)
+ if (operation === "get") return this.processPermission(process, parsePermissionName(name))
+ if (operation === "allows") {
+
+ const permission = parsePermissionName(name)
+
+ return this.processAllows(process, permission, value as PermissionRequest<typeof permission>)
+ }
+ if (operation === "set") {
+
+ const permission = parsePermissionName(name)
+
+ await this.setProcessPermission(process, permission, value as Exclude<PermissionInput<typeof permission>, null>)
+
+ return
+ }
+ if (operation === "delete") {
+
+ await this.deleteProcessPermission(process, parsePermissionName(name))
+
+ return
+ }
+
+ throw new Error(`The System does not know the Process permission operation "${String(operation)}"`)
+ }
+
/** Tests one concrete permission against this Process's effective grants. */
public grants<Name extends PermissionName>(identity: string, name: Name, requested: readonly PermissionValue<Name>[]) {
@@ -1592,16 +1682,9 @@ export default class ProcessManager extends TheLink {
const permission = permissionCatalog.merge(name, temporary, requested)
const next = permissionCatalog.effective(name, declared, permission, stored)
- process.permissions.set(name, permission)
-
const needReload = permissionCatalog.needReload(name, effective, next)
- if (needReload) {
-
- process.setClientSameOrigin(permissionCatalog.granted(next))
-
- await this.$outbound.publish("/client-access", process.identity, process.hosted())
- }
+ await this.setProcessPermission(process, name, permission)
return permissionChange(next, needReload)
}
@@ -1641,8 +1724,6 @@ export default class ProcessManager extends TheLink {
permission: Permission<Name>
) {
- let needReload = false
-
for (const process of this.processes.values()) {
if (process.program !== program || !process.client) continue
@@ -1660,14 +1741,34 @@ export default class ProcessManager extends TheLink {
if (!changed) continue
- needReload = true
-
if (name === "all") process.setClientSameOrigin(permissionCatalog.granted(this.effectivePermission(process, name, permission)))
await this.$outbound.publish("/client-access", process.identity, process.hosted())
}
+ }
+
+ private async processPermissionChanged<Name extends PermissionName>(
+ process: Process,
+ name: Name,
+ before: Permission<Name>,
+ permission: Permission<Name>
+ ) {
+
+ if (!process.client) return
+
+ const declared = process.program.clientPermissions[name] ?? null
+ const stored = this.authManager.programManager.permission(process.program, name)
+ const changed = permissionCatalog.needReload(
+ name,
+ permissionCatalog.effective(name, declared, before, stored),
+ permissionCatalog.effective(name, declared, permission, stored)
+ )
+
+ if (!changed) return
- return needReload
+ if (name === "all") process.setClientSameOrigin(permissionCatalog.granted(this.effectivePermission(process, name)))
+
+ await this.$outbound.publish("/client-access", process.identity, process.hosted())
}
public cancelPermission(identity: string, request: string) {
@@ -1720,21 +1821,65 @@ export default class ProcessManager extends TheLink {
if (operation === "all") return [this.authManager.programManager.permissions(program)]
if (operation === "get") return [this.authManager.programManager.permission(program, parsePermissionName(rest[2]))]
+ if (operation === "allows") {
+
+ const permission = parsePermissionName(rest[2])
+
+ return [this.authManager.programManager.allowsPermission(program, permission, rest[3] as PermissionRequest<typeof permission>)]
+ }
if (operation === "set") {
const permission = parsePermissionName(rest[2])
- return [await this.authManager.programManager.setPermission(
+ await this.authManager.programManager.setPermission(
program,
permission,
rest[3] as Exclude<PermissionInput<typeof permission>, null>
- )]
+ )
+
+ return []
+ }
+ if (operation === "delete") {
+
+ await this.authManager.programManager.deletePermission(program, parsePermissionName(rest[2]))
+
+ return []
}
- if (operation === "delete") return [await this.authManager.programManager.deletePermission(program, parsePermissionName(rest[2]))]
throw new Error(`The System does not know the Program permission operation "${String(operation)}"`)
}
+ if (word === "process-permissions") {
+
+ const target = this.system.holdProcess(rest[0], process)
+ const operation = rest[1]
+
+ if (operation === "all") return [this.processPermissions(target)]
+ if (operation === "get") return [this.processPermission(target, parsePermissionName(rest[2]))]
+ if (operation === "allows") {
+
+ const permission = parsePermissionName(rest[2])
+
+ return [this.processAllows(target, permission, rest[3] as PermissionRequest<typeof permission>)]
+ }
+ if (operation === "set") {
+
+ const permission = parsePermissionName(rest[2])
+
+ await this.setProcessPermission(target, permission, rest[3] as Exclude<PermissionInput<typeof permission>, null>)
+
+ return []
+ }
+ if (operation === "delete") {
+
+ await this.deleteProcessPermission(target, parsePermissionName(rest[2]))
+
+ return []
+ }
+
+ throw new Error(`The System does not know the Process permission operation "${String(operation)}"`)
+ }
+
if (word === "program-agent") {
const program = this.system.holdProgram(rest[0], process.program)
@@ -2817,6 +2962,16 @@ function permissionChange<Name extends PermissionName>(permission: Permission<Na
})
}
+function clonePermission<Name extends PermissionName>(permission: Permission<Name>): Permission<Name> {
+
+ return Array.isArray(permission) ? [...permission] : permission
+}
+
+function clonePermissions(permissions: Permissions): Permissions {
+
+ return Object.fromEntries(Object.entries(permissions).map(([name, permission]) => [name, clonePermission(permission)]))
+}
+
// Where an answer goes, read out of the question itself.
//
// A question is named by its asker before it leaves — which half of
diff --git a/source/server/core/link-manager/auth-manager/process-manager/process.ts b/source/server/core/link-manager/auth-manager/process-manager/process.ts
index 9baeb4b..1582459 100644
--- a/source/server/core/link-manager/auth-manager/process-manager/process.ts
+++ b/source/server/core/link-manager/auth-manager/process-manager/process.ts
@@ -8,7 +8,7 @@ import { randomUUID } from "node:crypto"
import { type Layer } from "@phreshos/core"
import { Tunnel } from "@the-link/core"
import type { ServerRuntime } from "@server/core/server-runtime"
-import type { PermissionName, PermissionValue } from "@phreshos/core"
+import type { Permission, PermissionName } from "@phreshos/core"
/**
* One live execution of a Program.
@@ -72,7 +72,7 @@ export default class Process {
public readonly parent: Process | null
/** User grants retained only for this Process lifetime. */
- public readonly permissions = new Map<PermissionName, readonly PermissionValue<PermissionName>[]>()
+ public readonly permissions = new Map<PermissionName, Exclude<Permission, null>>()
private readonly hostTraffic: HostTraffic
diff --git a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts b/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
index 71da633..23e1a84 100644
--- a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
+++ b/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
@@ -24,9 +24,9 @@ import { readPermissions, writePermissions } from "./permissions"
import {
parsePermissionName,
type Permission,
- type PermissionChange,
type PermissionInput,
type PermissionName,
+ type PermissionRequest,
type Permissions
} from "@phreshos/core"
@@ -169,12 +169,33 @@ export default class ProgramManager extends TheLink {
return clonePermissions(readPermissions(program))
}
- /** Stores one canonical user grant and reports its Client activation need. */
+ /** Tests one requested value against this Program's persistent layer only. */
+ public allowsPermission<Name extends PermissionName>(
+ program: Program,
+ name: Name,
+ input: PermissionRequest<Name> = true
+ ) {
+
+ const requested = permissionCatalog.resolve(name, input)
+
+ if (!Array.isArray(requested)) throw new Error("A permission check must be true or a list of values")
+
+ const permissions = readPermissions(program)
+
+ return permissionCatalog.allows(
+ name,
+ permissions.all ?? null,
+ permissions[name] ?? null,
+ requested
+ )
+ }
+
+ /** Stores one canonical user grant and propagates its effective access change. */
public async setPermission<Name extends PermissionName>(
program: Program,
name: Name,
value: Exclude<PermissionInput<Name>, null>
- ): Promise<PermissionChange<Name>> {
+ ): Promise<void> {
if (value === null) throw new Error("A stored Program permission cannot be null")
@@ -186,11 +207,11 @@ export default class ProgramManager extends TheLink {
writePermissions(program, { ...permissions, [name]: permission })
}
- return await this.permissionChange(program, name, before, permission)
+ await this.authManager.processManager.storedPermissionChanged(program, name, before, permission)
}
/** Removes one stored user grant without changing Program declarations. */
- public async deletePermission<Name extends PermissionName>(program: Program, name: Name): Promise<PermissionChange<Name>> {
+ public async deletePermission<Name extends PermissionName>(program: Program, name: Name): Promise<void> {
const permissions = readPermissions(program)
const before = permissions[name] ?? null
@@ -201,7 +222,7 @@ export default class ProgramManager extends TheLink {
writePermissions(program, permissions)
}
- return await this.permissionChange(program, name, before, null)
+ await this.authManager.processManager.storedPermissionChanged(program, name, before, null)
}
@Connect("/permissions")
@@ -211,30 +232,28 @@ export default class ProgramManager extends TheLink {
if (operation === "all") return this.permissions(program)
if (operation === "get") return this.permission(program, parsePermissionName(name))
- if (operation === "set") {
+ if (operation === "allows") {
const permission = parsePermissionName(name)
- return await this.setPermission(program, permission, value as Exclude<PermissionInput<typeof permission>, null>)
+ return this.allowsPermission(program, permission, value as PermissionRequest<typeof permission>)
}
- if (operation === "delete") return await this.deletePermission(program, parsePermissionName(name))
+ if (operation === "set") {
- throw new Error(`The System does not know the Program permission operation "${String(operation)}"`)
- }
+ const permission = parsePermissionName(name)
- private async permissionChange<Name extends PermissionName>(
- program: Program,
- name: Name,
- before: Permission<Name>,
- permission: Permission<Name>
- ): Promise<PermissionChange<Name>> {
+ await this.setPermission(program, permission, value as Exclude<PermissionInput<typeof permission>, null>)
+
+ return
+ }
+ if (operation === "delete") {
- const needReload = await this.authManager.processManager.storedPermissionChanged(program, name, before, permission)
+ await this.deletePermission(program, parsePermissionName(name))
- return Object.freeze({
- permission: clonePermission(permission),
- needReload
- })
+ return
+ }
+
+ throw new Error(`The System does not know the Program permission operation "${String(operation)}"`)
}
@Connect("/create-program")