@capybara-hq/kernel 0.1.0-rc.53 → 0.1.0-rc.55
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/admin/admin.schemas.d.ts +11 -1
- package/dist/catalog/catalog.module.js +1 -1
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/requests.service.d.ts +5 -5
- package/dist/catalog/requests.service.js +9 -9
- package/dist/channels/cards.d.ts +4 -3
- package/dist/channels/cards.js +25 -16
- package/dist/channels/phrases.d.ts +15 -4
- package/dist/channels/phrases.js +15 -1
- package/dist/config/load.d.ts +26 -7
- package/dist/config/load.js +60 -9
- package/dist/config/permission-migration.d.ts +17 -9
- package/dist/config/permission-migration.js +60 -9
- package/dist/config/schema.d.ts +15 -1
- package/dist/config/schema.js +66 -11
- package/dist/conversations/events.d.ts +247 -5
- package/dist/conversations/events.js +127 -16
- package/dist/conversations/permission-profiles.controller.d.ts +12 -2
- package/dist/conversations/permission-profiles.service.d.ts +12 -2
- package/dist/conversations/permission-profiles.service.js +11 -4
- package/dist/db/kernel-schema.d.ts +18 -2
- package/dist/devices/device-projects.service.d.ts +1 -1
- package/dist/devices/device-projects.service.js +6 -6
- package/dist/devices/devices.controller.d.ts +10 -8
- package/dist/devices/devices.controller.js +14 -18
- package/dist/devices/devices.schemas.d.ts +5 -5
- package/dist/devices/devices.schemas.js +5 -5
- package/dist/devices/devices.service.d.ts +188 -175
- package/dist/devices/devices.service.js +284 -292
- package/dist/devices/dto.d.ts +43 -23
- package/dist/devices/dto.js +45 -49
- package/dist/devices/project-rules.d.ts +0 -10
- package/dist/devices/project-rules.js +0 -5
- package/dist/errors.d.ts +43 -19
- package/dist/errors.js +43 -19
- package/dist/evals/reconstruct.d.ts +12 -3
- package/dist/evals/reconstruct.js +10 -6
- package/dist/evals/recorded-permission-context.d.ts +6 -4
- package/dist/evals/recorded-permission-context.js +15 -9
- package/dist/evals/run-replay.d.ts +6 -1
- package/dist/evals/run-replay.js +7 -2
- package/dist/evals/worker.js +3 -1
- package/dist/http/api.module.js +8 -0
- package/dist/http/app.js +9 -0
- package/dist/http/meta.controller.d.ts +33 -0
- package/dist/http/meta.controller.js +72 -0
- package/dist/identity/app-auth.controller.d.ts +25 -0
- package/dist/identity/app-auth.controller.js +64 -0
- package/dist/identity/app-auth.d.ts +66 -0
- package/dist/identity/app-auth.js +203 -0
- package/dist/identity/app-auth.schemas.d.ts +47 -0
- package/dist/identity/app-auth.schemas.js +43 -0
- package/dist/identity/identity.module.d.ts +1 -1
- package/dist/identity/identity.module.js +4 -1
- package/dist/identity/settings.d.ts +45 -18
- package/dist/identity/settings.js +151 -51
- package/dist/infra/config/kernel-config.service.d.ts +1 -1
- package/dist/infra/config/kernel-config.service.js +19 -5
- package/dist/infra/outbox/registry.d.ts +14 -0
- package/dist/infra/outbox/registry.js +15 -0
- package/dist/infra/throttler/named-throttlers.d.ts +3 -2
- package/dist/infra/throttler/named-throttlers.js +14 -2
- package/dist/local-devices/device-auth.controller.d.ts +1 -1
- package/dist/local-devices/device-auth.controller.js +1 -1
- package/dist/local-devices/device-auth.d.ts +4 -4
- package/dist/local-devices/device-auth.js +1 -1
- package/dist/local-devices/device-policy.service.d.ts +18 -9
- package/dist/local-devices/device-policy.service.js +21 -61
- package/dist/local-devices/device-routes.d.ts +3 -1
- package/dist/local-devices/device-routes.js +15 -4
- package/dist/local-devices/instance-bindings.js +5 -2
- package/dist/local-devices/local-device-gateway.d.ts +2 -2
- package/dist/local-devices/local-device-gateway.js +3 -3
- package/dist/local-devices/local-device-provider.d.ts +4 -4
- package/dist/local-devices/local-device-provider.js +23 -9
- package/dist/local-devices/local-devices.module.d.ts +1 -1
- package/dist/local-devices/local-devices.module.js +1 -1
- package/dist/local-devices/machines.d.ts +40 -16
- package/dist/local-devices/machines.js +89 -61
- package/dist/local-devices/owner-report.d.ts +16 -14
- package/dist/local-devices/owner-report.js +24 -30
- package/dist/local-devices/owner-reports.js +26 -8
- package/dist/local-devices/policy-summary.d.ts +25 -44
- package/dist/local-devices/policy-summary.js +26 -71
- package/dist/local-devices/relay.d.ts +6 -6
- package/dist/local-devices/relay.js +10 -8
- package/dist/net/public-address.d.ts +1 -1
- package/dist/net/public-address.js +5 -2
- package/dist/orgs/device-network-setting.d.ts +82 -0
- package/dist/orgs/device-network-setting.js +142 -0
- package/dist/orgs/settings.service.d.ts +103 -66
- package/dist/orgs/settings.service.js +201 -137
- package/dist/overrides.d.ts +5 -1
- package/dist/overrides.js +2 -0
- package/dist/platform/exclusions.js +24 -1
- package/dist/platform/operation.js +2 -2
- package/dist/platform/ops/admin.ops.js +20 -11
- package/dist/platform/ops/devices.ops.js +70 -5
- package/dist/platform/ops/org-settings.ops.js +10 -6
- package/dist/platform/ops/orgs.ops.js +2 -1
- package/dist/platform/ops/platform-icons.ops.d.ts +14 -0
- package/dist/platform/ops/platform-icons.ops.js +117 -0
- package/dist/platform/platform-ops.module.d.ts +2 -1
- package/dist/platform/platform-ops.module.js +3 -0
- package/dist/platform/route-schemas.js +3 -0
- package/dist/runtime/context/permission-profile.d.ts +17 -8
- package/dist/runtime/context/permission-profile.js +126 -45
- package/dist/runtime/maintenance.d.ts +5 -2
- package/dist/runtime/maintenance.js +15 -4
- package/dist/runtime/permission-context.service.d.ts +1 -1
- package/dist/runtime/permission-context.service.js +5 -3
- package/dist/runtime/project/knowledge.d.ts +5 -4
- package/dist/runtime/project/knowledge.js +2 -1
- package/dist/runtime/project/knowledge.service.js +16 -13
- package/dist/sandbox/cloud-permission-snapshot.js +22 -3
- package/dist/sandbox/device-sandbox-settings.d.ts +230 -0
- package/dist/sandbox/device-sandbox-settings.js +470 -0
- package/dist/sandbox/device-sandbox.d.ts +187 -0
- package/dist/sandbox/device-sandbox.js +236 -0
- package/dist/sandbox/held-operations.js +15 -7
- package/dist/sandbox/modes.d.ts +49 -24
- package/dist/sandbox/modes.js +27 -19
- package/dist/sandbox/operation-authority-types.d.ts +6 -5
- package/dist/sandbox/operation-authority-types.js +7 -8
- package/dist/sandbox/operation-authority.d.ts +23 -9
- package/dist/sandbox/operation-authority.js +179 -97
- package/dist/sandbox/permission-auto-grants.d.ts +42 -0
- package/dist/sandbox/permission-auto-grants.js +145 -0
- package/dist/sandbox/permission-call-grants.d.ts +12 -4
- package/dist/sandbox/permission-call-grants.js +84 -35
- package/dist/sandbox/permission-call-grants.module.js +10 -3
- package/dist/sandbox/permission-call-types.d.ts +39 -5
- package/dist/sandbox/permission-ceiling-writes.d.ts +29 -3
- package/dist/sandbox/permission-ceiling-writes.js +38 -4
- package/dist/sandbox/permission-config-publication.d.ts +44 -2
- package/dist/sandbox/permission-config-publication.js +230 -59
- package/dist/sandbox/permission-context-types.d.ts +2 -2
- package/dist/sandbox/permission-facts.d.ts +144 -15
- package/dist/sandbox/permission-facts.js +258 -70
- package/dist/sandbox/permission-runtime-reports.d.ts +5 -4
- package/dist/sandbox/permission-runtime-reports.js +19 -29
- package/dist/sandbox/review/classifier.d.ts +8 -0
- package/dist/sandbox/review/classifier.js +10 -0
- package/dist/sandbox/review/permission-candidate.d.ts +69 -5
- package/dist/sandbox/review/permission-candidate.js +153 -30
- package/dist/sandbox/review/review.service.d.ts +19 -3
- package/dist/sandbox/review/review.service.js +59 -11
- package/dist/sandbox/review/rules.js +23 -3
- package/dist/sandbox/sandbox.service.d.ts +29 -11
- package/dist/sandbox/sandbox.service.js +51 -26
- package/dist/skills/invoke.js +3 -2
- package/dist/themes/platform-icon-objects.d.ts +51 -0
- package/dist/themes/platform-icon-objects.js +113 -0
- package/dist/themes/platform-icon-public.controller.d.ts +17 -0
- package/dist/themes/platform-icon-public.controller.js +58 -0
- package/dist/themes/platform-icon.controller.d.ts +22 -0
- package/dist/themes/platform-icon.controller.js +65 -0
- package/dist/themes/platform-icon.schemas.d.ts +48 -0
- package/dist/themes/platform-icon.schemas.js +53 -0
- package/dist/themes/platform-icon.service.d.ts +83 -0
- package/dist/themes/platform-icon.service.js +301 -0
- package/dist/themes/themes.module.d.ts +4 -1
- package/dist/themes/themes.module.js +9 -4
- package/dist/tools/approvals.service.d.ts +26 -116
- package/dist/tools/approvals.service.js +42 -346
- package/dist/tools/builtins/sandbox/bash.d.ts +16 -3
- package/dist/tools/builtins/sandbox/bash.js +80 -28
- package/dist/tools/builtins/sandbox/common.d.ts +39 -14
- package/dist/tools/builtins/sandbox/common.js +47 -24
- package/dist/tools/builtins/sandbox/deliver.js +7 -4
- package/dist/tools/builtins/skill-load.d.ts +2 -2
- package/dist/tools/builtins/skill-load.js +4 -3
- package/dist/tools/device-op-resolver.d.ts +4 -10
- package/dist/tools/device-op-resolver.js +8 -19
- package/dist/tools/network-asks.d.ts +52 -15
- package/dist/tools/network-asks.js +55 -25
- package/dist/tools/pipeline.d.ts +26 -14
- package/dist/tools/pipeline.js +144 -61
- package/dist/tools/registry.d.ts +6 -0
- package/dist/tools/tools.module.js +1 -1
- package/package.json +4 -4
- package/src/migrations/0164_platform_icons.sql +10 -0
- package/src/migrations/0165_device_network_columns.sql +50 -0
- package/src/migrations/0166_permission_grant_full_access.sql +17 -0
- package/src/migrations/0167_device_network_backfill.sql +32 -0
- package/src/migrations/0168_binding_installed_connection.sql +6 -0
- package/dist/orgs/cloud-network-setting.d.ts +0 -33
- package/dist/orgs/cloud-network-setting.js +0 -47
- package/dist/sandbox/platform-full-access.d.ts +0 -110
- package/dist/sandbox/platform-full-access.js +0 -198
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { RouteSchemas } from "../platform/route-schemas.js";
|
|
3
|
+
/**
|
|
4
|
+
* The platform icon (mobile layout spec §10.2.1): `app` the 512 × 512 PNG app icon, `maskable`
|
|
5
|
+
* its 512 × 512 PNG variant with the subject inside the central 80 % safe zone, `favicon` an SVG.
|
|
6
|
+
*/
|
|
7
|
+
export declare const PLATFORM_ICON_PURPOSES: readonly ["app", "maskable", "favicon"];
|
|
8
|
+
export type PlatformIconPurpose = (typeof PLATFORM_ICON_PURPOSES)[number];
|
|
9
|
+
/** The largest icon file in bytes (1 MiB), whatever its purpose. */
|
|
10
|
+
export declare const PLATFORM_ICON_MAX_BYTES: number;
|
|
11
|
+
/** The one size of a PNG icon (`app`, `maskable`), in pixels. */
|
|
12
|
+
export declare const PLATFORM_ICON_PNG_SIZE = 512;
|
|
13
|
+
/**
|
|
14
|
+
* The longest `dataBase64` the route reads: the base64 of 1.5 MiB, so a file somewhat over
|
|
15
|
+
* {@link PLATFORM_ICON_MAX_BYTES} still reaches the size check and is refused with
|
|
16
|
+
* `platform_icon.too_large` (the route's body limit, `PLATFORM_ICON_BODY_LIMIT`, admits it).
|
|
17
|
+
*/
|
|
18
|
+
export declare const PLATFORM_ICON_MAX_BASE64: number;
|
|
19
|
+
/** `:purpose` of the icon routes. */
|
|
20
|
+
export declare const PlatformIconParams: z.ZodObject<{
|
|
21
|
+
purpose: z.ZodEnum<{
|
|
22
|
+
app: "app";
|
|
23
|
+
favicon: "favicon";
|
|
24
|
+
maskable: "maskable";
|
|
25
|
+
}>;
|
|
26
|
+
}, z.core.$strip>;
|
|
27
|
+
/** `PUT /api/v1/admin/platform/icons/:purpose`: the file as standard base64. */
|
|
28
|
+
export declare const PlatformIconBody: z.ZodObject<{
|
|
29
|
+
contentType: z.ZodEnum<{
|
|
30
|
+
"image/png": "image/png";
|
|
31
|
+
"image/svg+xml": "image/svg+xml";
|
|
32
|
+
}>;
|
|
33
|
+
dataBase64: z.ZodString;
|
|
34
|
+
}, z.core.$strict>;
|
|
35
|
+
/** `GET /api/v1/platform/icon/:purpose`: `v` is the SHA-256 the URL was built with. */
|
|
36
|
+
export declare const PlatformIconQuery: z.ZodObject<{
|
|
37
|
+
v: z.ZodOptional<z.ZodString>;
|
|
38
|
+
}, z.core.$strip>;
|
|
39
|
+
/** One uploaded icon as the admin routes return it. */
|
|
40
|
+
export interface PlatformIconView {
|
|
41
|
+
purpose: PlatformIconPurpose;
|
|
42
|
+
/** The public URL, carrying the hash: `/api/v1/platform/icon/<purpose>?v=<sha256>`. */
|
|
43
|
+
url: string;
|
|
44
|
+
sha256: string;
|
|
45
|
+
updatedAt: string;
|
|
46
|
+
}
|
|
47
|
+
/** The admin icon routes the Platform operations mirror (`check:platform-ops` keys). */
|
|
48
|
+
export declare const PLATFORM_ICON_ROUTES: readonly RouteSchemas[];
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* The platform icon (mobile layout spec §10.2.1): `app` the 512 × 512 PNG app icon, `maskable`
|
|
4
|
+
* its 512 × 512 PNG variant with the subject inside the central 80 % safe zone, `favicon` an SVG.
|
|
5
|
+
*/
|
|
6
|
+
export const PLATFORM_ICON_PURPOSES = ["app", "maskable", "favicon"];
|
|
7
|
+
/** The largest icon file in bytes (1 MiB), whatever its purpose. */
|
|
8
|
+
export const PLATFORM_ICON_MAX_BYTES = 1024 * 1024;
|
|
9
|
+
/** The one size of a PNG icon (`app`, `maskable`), in pixels. */
|
|
10
|
+
export const PLATFORM_ICON_PNG_SIZE = 512;
|
|
11
|
+
/**
|
|
12
|
+
* The longest `dataBase64` the route reads: the base64 of 1.5 MiB, so a file somewhat over
|
|
13
|
+
* {@link PLATFORM_ICON_MAX_BYTES} still reaches the size check and is refused with
|
|
14
|
+
* `platform_icon.too_large` (the route's body limit, `PLATFORM_ICON_BODY_LIMIT`, admits it).
|
|
15
|
+
*/
|
|
16
|
+
export const PLATFORM_ICON_MAX_BASE64 = 4 * Math.ceil((1.5 * PLATFORM_ICON_MAX_BYTES) / 3);
|
|
17
|
+
/** `:purpose` of the icon routes. */
|
|
18
|
+
export const PlatformIconParams = z.object({
|
|
19
|
+
purpose: z
|
|
20
|
+
.enum(PLATFORM_ICON_PURPOSES)
|
|
21
|
+
.describe("Which icon: app (the 512 × 512 PNG app icon), maskable (its 512 × 512 PNG variant whose subject stays inside the central 80 % safe zone) or favicon (an SVG)."),
|
|
22
|
+
});
|
|
23
|
+
/** `PUT /api/v1/admin/platform/icons/:purpose`: the file as standard base64. */
|
|
24
|
+
export const PlatformIconBody = z
|
|
25
|
+
.object({
|
|
26
|
+
contentType: z
|
|
27
|
+
.enum(["image/png", "image/svg+xml"])
|
|
28
|
+
.describe("image/png for app and maskable, image/svg+xml for favicon."),
|
|
29
|
+
dataBase64: z
|
|
30
|
+
.string()
|
|
31
|
+
.min(1)
|
|
32
|
+
.max(PLATFORM_ICON_MAX_BASE64)
|
|
33
|
+
.regex(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/, "dataBase64 must be standard base64 (A-Z, a-z, 0-9, +, / with = padding)")
|
|
34
|
+
.describe("The file's bytes as standard base64, at most 1 MiB decoded (an SVG favicon at most 128 KiB)."),
|
|
35
|
+
})
|
|
36
|
+
.strict();
|
|
37
|
+
/** `GET /api/v1/platform/icon/:purpose`: `v` is the SHA-256 the URL was built with. */
|
|
38
|
+
export const PlatformIconQuery = z.object({
|
|
39
|
+
v: z
|
|
40
|
+
.string()
|
|
41
|
+
.regex(/^[0-9a-f]{64}$/, "v must be a SHA-256 in lower-case hex")
|
|
42
|
+
.optional(),
|
|
43
|
+
});
|
|
44
|
+
/** The admin icon routes the Platform operations mirror (`check:platform-ops` keys). */
|
|
45
|
+
export const PLATFORM_ICON_ROUTES = [
|
|
46
|
+
{ route: "GET /api/v1/admin/platform/icons" },
|
|
47
|
+
{
|
|
48
|
+
route: "PUT /api/v1/admin/platform/icons/:purpose",
|
|
49
|
+
params: PlatformIconParams,
|
|
50
|
+
body: PlatformIconBody,
|
|
51
|
+
},
|
|
52
|
+
{ route: "DELETE /api/v1/admin/platform/icons/:purpose", params: PlatformIconParams },
|
|
53
|
+
];
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { SuperAdminLock } from "../identity/super-admin.js";
|
|
2
|
+
import type { WriteActor } from "../identity/write-actor.js";
|
|
3
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
4
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
5
|
+
import { LockService } from "../infra/lock/lock.service.js";
|
|
6
|
+
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
7
|
+
import { StorageService } from "../storage/storage.service.js";
|
|
8
|
+
import { type PlatformIconPurpose, type PlatformIconView } from "./platform-icon.schemas.js";
|
|
9
|
+
/** A validated icon, ready to store: the bytes to write and their SHA-256. */
|
|
10
|
+
export interface PlatformIconFile {
|
|
11
|
+
purpose: PlatformIconPurpose;
|
|
12
|
+
contentType: "image/png" | "image/svg+xml";
|
|
13
|
+
bytes: Uint8Array;
|
|
14
|
+
sha256: string;
|
|
15
|
+
}
|
|
16
|
+
/** The public URL of an icon: the hash in `v` changes on upload, so it is cached as immutable. */
|
|
17
|
+
export declare const platformIconUrl: (purpose: PlatformIconPurpose, sha256: string) => string;
|
|
18
|
+
/**
|
|
19
|
+
* Checks an upload (nothing is rendered or resized): at most 1 MiB; `app` and `maskable` a PNG
|
|
20
|
+
* (signature, an `IHDR` first chunk with a valid CRC-32, ending with `IEND`) of exactly 512 × 512;
|
|
21
|
+
* `favicon` an SVG of at most 128 KiB, parsed with the theme
|
|
22
|
+
* logo sanitizer (a strict XML parser, an `<svg>` root in the SVG namespace with a viewBox, no
|
|
23
|
+
* script, style element, event handler or external reference) and stored as its sanitized output.
|
|
24
|
+
* Throws the refusal as `platform_icon.*`.
|
|
25
|
+
*/
|
|
26
|
+
export declare function checkPlatformIcon(purpose: PlatformIconPurpose, declared: "image/png" | "image/svg+xml", dataBase64: string): PlatformIconFile;
|
|
27
|
+
/**
|
|
28
|
+
* The platform icon setting (mobile layout spec §10.2.1): the super admin's uploads (the app icon,
|
|
29
|
+
* its maskable variant, the SVG favicon), stored in object storage under the kernel's
|
|
30
|
+
* `deployment/` prefix with a row per purpose in `capybara.platform_icons`; the public read the
|
|
31
|
+
* manifest, favicons and native clients use. Writes are audited (`platform.icon.updated`,
|
|
32
|
+
* `platform.icon.cleared`) and serialized per purpose (see `platform-icon-objects.ts`).
|
|
33
|
+
*/
|
|
34
|
+
export declare class PlatformIconService {
|
|
35
|
+
private readonly db;
|
|
36
|
+
readonly txHost: TransactionHost;
|
|
37
|
+
private readonly outbox;
|
|
38
|
+
private readonly adminLock;
|
|
39
|
+
private readonly locks;
|
|
40
|
+
private readonly storage;
|
|
41
|
+
constructor(db: KernelDb, txHost: TransactionHost, outbox: OutboxService, adminLock: SuperAdminLock, locks: LockService, storage: StorageService);
|
|
42
|
+
/** Every uploaded icon, in purpose order (`app`, `maskable`, `favicon`). */
|
|
43
|
+
list(): Promise<{
|
|
44
|
+
icons: PlatformIconView[];
|
|
45
|
+
}>;
|
|
46
|
+
/**
|
|
47
|
+
* The icon of a purpose now, `{ purpose, sha256, url }`, or null when the default is in use:
|
|
48
|
+
* what other features (the meta document, the manifest) build the icon URL from.
|
|
49
|
+
*/
|
|
50
|
+
current(purpose: PlatformIconPurpose): Promise<{
|
|
51
|
+
purpose: PlatformIconPurpose;
|
|
52
|
+
sha256: string;
|
|
53
|
+
url: string;
|
|
54
|
+
} | null>;
|
|
55
|
+
/** The upload's checks alone (the Platform operation's precheck); throws the write's refusal. */
|
|
56
|
+
check(purpose: PlatformIconPurpose, contentType: "image/png" | "image/svg+xml", dataBase64: string): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Sets the icon of a purpose. Under the purpose's lock: first the orphan check of the new object
|
|
59
|
+
* is owed (a committed intent), then the object is written, then the row in a transaction that
|
|
60
|
+
* also owes the deletion of the object the row named before (none when the bytes are the same).
|
|
61
|
+
*/
|
|
62
|
+
set(purpose: PlatformIconPurpose, contentType: "image/png" | "image/svg+xml", dataBase64: string, actor: WriteActor, auditDetail?: Record<string, unknown>): Promise<PlatformIconView>;
|
|
63
|
+
/** The intent: the new object's delayed orphan check, owed before the object is written. */
|
|
64
|
+
private oweOrphanCheck;
|
|
65
|
+
/** The row naming the written object, the old object's deletion and the audit event. */
|
|
66
|
+
private commit;
|
|
67
|
+
/**
|
|
68
|
+
* Restores the default of a purpose: the row goes and its object's deletion is owed. Clearing a
|
|
69
|
+
* purpose with no upload changes nothing; returns whether a row was removed.
|
|
70
|
+
*/
|
|
71
|
+
clear(purpose: PlatformIconPurpose, actor: WriteActor, auditDetail?: Record<string, unknown>): Promise<boolean>;
|
|
72
|
+
private remove;
|
|
73
|
+
/**
|
|
74
|
+
* The stored bytes of a purpose for the public route, or `platform_icon.not_found` when the
|
|
75
|
+
* default is in use, `v` names another upload, or the object is missing. `immutable` is true
|
|
76
|
+
* when `v` is the current hash (the URL then never changes meaning).
|
|
77
|
+
*/
|
|
78
|
+
read(purpose: PlatformIconPurpose, v: string | undefined): Promise<{
|
|
79
|
+
contentType: string;
|
|
80
|
+
bytes: Uint8Array;
|
|
81
|
+
immutable: boolean;
|
|
82
|
+
}>;
|
|
83
|
+
}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
+
};
|
|
10
|
+
import { createHash } from "node:crypto";
|
|
11
|
+
import { crc32 } from "node:zlib";
|
|
12
|
+
import { sanitizeThemeSvg, THEME_LIMITS } from "@capybara-hq/plugin-sdk";
|
|
13
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
14
|
+
import { ApiError } from "../errors.js";
|
|
15
|
+
import { audit } from "../identity/audit.js";
|
|
16
|
+
import { SuperAdminLock } from "../identity/super-admin.js";
|
|
17
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
18
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
19
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
20
|
+
import { LockService } from "../infra/lock/lock.service.js";
|
|
21
|
+
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
22
|
+
import { isStorageUnavailable, StorageService } from "../storage/storage.service.js";
|
|
23
|
+
import { PLATFORM_ICON_MAX_BYTES, PLATFORM_ICON_PNG_SIZE, } from "./platform-icon.schemas.js";
|
|
24
|
+
import { owePlatformIconDelete, PLATFORM_ICON_LOCK, platformIconKey, platformIconLockKey, } from "./platform-icon-objects.js";
|
|
25
|
+
/** The type each purpose is stored and served as (fixed by the purpose, never by the upload). */
|
|
26
|
+
const CONTENT_TYPE = {
|
|
27
|
+
app: "image/png",
|
|
28
|
+
maskable: "image/png",
|
|
29
|
+
favicon: "image/svg+xml",
|
|
30
|
+
};
|
|
31
|
+
const PNG_SIG = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
|
32
|
+
const IHDR = [0x49, 0x48, 0x44, 0x52];
|
|
33
|
+
/** The IEND chunk every complete PNG ends with: length 0, type, CRC-32. */
|
|
34
|
+
const IEND = [0, 0, 0, 0, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82];
|
|
35
|
+
/** The largest SVG favicon: the theme logo sanitizer's limit (128 KiB). */
|
|
36
|
+
const SVG_MAX_BYTES = THEME_LIMITS.svgBytes;
|
|
37
|
+
/** The public URL of an icon: the hash in `v` changes on upload, so it is cached as immutable. */
|
|
38
|
+
export const platformIconUrl = (purpose, sha256) => `/api/v1/platform/icon/${purpose}?v=${sha256}`;
|
|
39
|
+
/**
|
|
40
|
+
* Checks an upload (nothing is rendered or resized): at most 1 MiB; `app` and `maskable` a PNG
|
|
41
|
+
* (signature, an `IHDR` first chunk with a valid CRC-32, ending with `IEND`) of exactly 512 × 512;
|
|
42
|
+
* `favicon` an SVG of at most 128 KiB, parsed with the theme
|
|
43
|
+
* logo sanitizer (a strict XML parser, an `<svg>` root in the SVG namespace with a viewBox, no
|
|
44
|
+
* script, style element, event handler or external reference) and stored as its sanitized output.
|
|
45
|
+
* Throws the refusal as `platform_icon.*`.
|
|
46
|
+
*/
|
|
47
|
+
export function checkPlatformIcon(purpose, declared, dataBase64) {
|
|
48
|
+
const data = Buffer.from(dataBase64, "base64");
|
|
49
|
+
if (data.byteLength > PLATFORM_ICON_MAX_BYTES) {
|
|
50
|
+
throw new ApiError("platform_icon.too_large", `The icon is ${data.byteLength} bytes; an icon file is at most 1 MiB (${PLATFORM_ICON_MAX_BYTES} bytes)`);
|
|
51
|
+
}
|
|
52
|
+
const expected = CONTENT_TYPE[purpose];
|
|
53
|
+
if (declared !== expected) {
|
|
54
|
+
throw new ApiError("platform_icon.invalid_type", purpose === "favicon"
|
|
55
|
+
? "The favicon must be an SVG (image/svg+xml)"
|
|
56
|
+
: `The ${purpose} icon must be a PNG (image/png) of ${PLATFORM_ICON_PNG_SIZE} × ${PLATFORM_ICON_PNG_SIZE} pixels; an SVG is accepted only as the favicon`);
|
|
57
|
+
}
|
|
58
|
+
let bytes = data;
|
|
59
|
+
if (expected === "image/png") {
|
|
60
|
+
checkPng(purpose, data);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
if (data.byteLength > SVG_MAX_BYTES) {
|
|
64
|
+
throw new ApiError("platform_icon.too_large", `The favicon is ${data.byteLength} bytes; an SVG favicon is at most 128 KiB (${SVG_MAX_BYTES} bytes)`);
|
|
65
|
+
}
|
|
66
|
+
const r = sanitizeThemeSvg(data);
|
|
67
|
+
if ("error" in r) {
|
|
68
|
+
throw new ApiError("platform_icon.invalid_type", `The favicon is not a usable SVG: ${r.error}`);
|
|
69
|
+
}
|
|
70
|
+
bytes = new TextEncoder().encode(r.svg);
|
|
71
|
+
}
|
|
72
|
+
const sha256 = createHash("sha256").update(bytes).digest("hex");
|
|
73
|
+
return { purpose, contentType: expected, bytes, sha256 };
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* A PNG of exactly 512 × 512 by its signature, its `IHDR` first chunk (with a valid CRC-32) and
|
|
77
|
+
* its width and height, ending with the `IEND` chunk; throws the refusal.
|
|
78
|
+
*/
|
|
79
|
+
function checkPng(purpose, data) {
|
|
80
|
+
const isPng = data.byteLength >= 33 &&
|
|
81
|
+
PNG_SIG.every((b, i) => data[i] === b) &&
|
|
82
|
+
data.readUInt32BE(8) === 13 &&
|
|
83
|
+
IHDR.every((b, i) => data[12 + i] === b);
|
|
84
|
+
if (!isPng) {
|
|
85
|
+
throw new ApiError("platform_icon.invalid_type", `The ${purpose} icon is not a PNG file (no PNG signature and IHDR header); upload a ${PLATFORM_ICON_PNG_SIZE} × ${PLATFORM_ICON_PNG_SIZE} PNG`);
|
|
86
|
+
}
|
|
87
|
+
// the IHDR CRC covers its type and data (bytes 12..28); a complete PNG ends with IEND
|
|
88
|
+
const ihdrIntact = crc32(data.subarray(12, 29)) === data.readUInt32BE(29);
|
|
89
|
+
const ended = data.byteLength >= 33 + IEND.length &&
|
|
90
|
+
IEND.every((b, i) => data[data.byteLength - IEND.length + i] === b);
|
|
91
|
+
if (!ihdrIntact || !ended) {
|
|
92
|
+
throw new ApiError("platform_icon.invalid_type", `The ${purpose} PNG is truncated or damaged (no intact IHDR chunk or no IEND chunk at the end); export it again as a ${PLATFORM_ICON_PNG_SIZE} × ${PLATFORM_ICON_PNG_SIZE} PNG`);
|
|
93
|
+
}
|
|
94
|
+
const width = data.readUInt32BE(16);
|
|
95
|
+
const height = data.readUInt32BE(20);
|
|
96
|
+
if (width !== PLATFORM_ICON_PNG_SIZE || height !== PLATFORM_ICON_PNG_SIZE) {
|
|
97
|
+
throw new ApiError("platform_icon.invalid_size", `The ${purpose} icon is ${width} × ${height} pixels; it must be exactly ${PLATFORM_ICON_PNG_SIZE} × ${PLATFORM_ICON_PNG_SIZE}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const storageUnavailable = () => new ApiError("platform_icon.storage_unavailable", "Object storage is not available in this deployment, so the platform icon cannot be stored or served; install and configure an object-store plugin");
|
|
101
|
+
/**
|
|
102
|
+
* The platform icon setting (mobile layout spec §10.2.1): the super admin's uploads (the app icon,
|
|
103
|
+
* its maskable variant, the SVG favicon), stored in object storage under the kernel's
|
|
104
|
+
* `deployment/` prefix with a row per purpose in `capybara.platform_icons`; the public read the
|
|
105
|
+
* manifest, favicons and native clients use. Writes are audited (`platform.icon.updated`,
|
|
106
|
+
* `platform.icon.cleared`) and serialized per purpose (see `platform-icon-objects.ts`).
|
|
107
|
+
*/
|
|
108
|
+
let PlatformIconService = class PlatformIconService {
|
|
109
|
+
db;
|
|
110
|
+
txHost;
|
|
111
|
+
outbox;
|
|
112
|
+
adminLock;
|
|
113
|
+
locks;
|
|
114
|
+
storage;
|
|
115
|
+
constructor(db, txHost, outbox, adminLock, locks, storage) {
|
|
116
|
+
this.db = db;
|
|
117
|
+
this.txHost = txHost;
|
|
118
|
+
this.outbox = outbox;
|
|
119
|
+
this.adminLock = adminLock;
|
|
120
|
+
this.locks = locks;
|
|
121
|
+
this.storage = storage;
|
|
122
|
+
}
|
|
123
|
+
/** Every uploaded icon, in purpose order (`app`, `maskable`, `favicon`). */
|
|
124
|
+
async list() {
|
|
125
|
+
const rows = await this.db
|
|
126
|
+
.selectFrom("platform_icons")
|
|
127
|
+
.select(["purpose", "sha256", "updated_at"])
|
|
128
|
+
.execute();
|
|
129
|
+
const order = ["app", "maskable", "favicon"];
|
|
130
|
+
rows.sort((a, b) => order.indexOf(a.purpose) - order.indexOf(b.purpose));
|
|
131
|
+
return { icons: rows.map((r) => viewOf(r.purpose, r.sha256, r.updated_at)) };
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* The icon of a purpose now, `{ purpose, sha256, url }`, or null when the default is in use:
|
|
135
|
+
* what other features (the meta document, the manifest) build the icon URL from.
|
|
136
|
+
*/
|
|
137
|
+
async current(purpose) {
|
|
138
|
+
const row = await this.db
|
|
139
|
+
.selectFrom("platform_icons")
|
|
140
|
+
.select("sha256")
|
|
141
|
+
.where("purpose", "=", purpose)
|
|
142
|
+
.executeTakeFirst();
|
|
143
|
+
return row ? { purpose, sha256: row.sha256, url: platformIconUrl(purpose, row.sha256) } : null;
|
|
144
|
+
}
|
|
145
|
+
/** The upload's checks alone (the Platform operation's precheck); throws the write's refusal. */
|
|
146
|
+
async check(purpose, contentType, dataBase64) {
|
|
147
|
+
checkPlatformIcon(purpose, contentType, dataBase64);
|
|
148
|
+
if (!this.storage.available)
|
|
149
|
+
throw storageUnavailable();
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Sets the icon of a purpose. Under the purpose's lock: first the orphan check of the new object
|
|
153
|
+
* is owed (a committed intent), then the object is written, then the row in a transaction that
|
|
154
|
+
* also owes the deletion of the object the row named before (none when the bytes are the same).
|
|
155
|
+
*/
|
|
156
|
+
async set(purpose, contentType, dataBase64, actor, auditDetail = {}) {
|
|
157
|
+
const file = checkPlatformIcon(purpose, contentType, dataBase64);
|
|
158
|
+
if (!this.storage.available)
|
|
159
|
+
throw storageUnavailable();
|
|
160
|
+
return this.locks.run(platformIconLockKey(purpose), PLATFORM_ICON_LOCK, async () => {
|
|
161
|
+
await this.oweOrphanCheck(file, actor);
|
|
162
|
+
try {
|
|
163
|
+
await this.storage.deployment().put(platformIconKey(purpose, file.sha256), file.bytes, {
|
|
164
|
+
contentType: file.contentType,
|
|
165
|
+
size: file.bytes.byteLength,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
catch (e) {
|
|
169
|
+
if (isStorageUnavailable(e))
|
|
170
|
+
throw storageUnavailable();
|
|
171
|
+
throw e;
|
|
172
|
+
}
|
|
173
|
+
return this.commit(file, actor, auditDetail);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
/** The intent: the new object's delayed orphan check, owed before the object is written. */
|
|
177
|
+
async oweOrphanCheck(file, actor) {
|
|
178
|
+
await this.adminLock.lockAndAuthorize(actor.accountId);
|
|
179
|
+
await owePlatformIconDelete(this.outbox, file.purpose, file.sha256, { orphanCheck: true });
|
|
180
|
+
}
|
|
181
|
+
/** The row naming the written object, the old object's deletion and the audit event. */
|
|
182
|
+
async commit(file, actor, auditDetail) {
|
|
183
|
+
await this.adminLock.lockAndAuthorize(actor.accountId);
|
|
184
|
+
const old = await this.db
|
|
185
|
+
.selectFrom("platform_icons")
|
|
186
|
+
.select("sha256")
|
|
187
|
+
.where("purpose", "=", file.purpose)
|
|
188
|
+
.forUpdate()
|
|
189
|
+
.executeTakeFirst();
|
|
190
|
+
const objectKey = platformIconKey(file.purpose, file.sha256);
|
|
191
|
+
const row = await this.db
|
|
192
|
+
.insertInto("platform_icons")
|
|
193
|
+
.values({
|
|
194
|
+
purpose: file.purpose,
|
|
195
|
+
object_key: objectKey,
|
|
196
|
+
sha256: file.sha256,
|
|
197
|
+
content_type: file.contentType,
|
|
198
|
+
})
|
|
199
|
+
.onConflict((oc) => oc.column("purpose").doUpdateSet({
|
|
200
|
+
object_key: objectKey,
|
|
201
|
+
sha256: file.sha256,
|
|
202
|
+
content_type: file.contentType,
|
|
203
|
+
updated_at: new Date(),
|
|
204
|
+
}))
|
|
205
|
+
.returning(["updated_at"])
|
|
206
|
+
.executeTakeFirstOrThrow();
|
|
207
|
+
if (old && old.sha256 !== file.sha256) {
|
|
208
|
+
await owePlatformIconDelete(this.outbox, file.purpose, old.sha256);
|
|
209
|
+
}
|
|
210
|
+
await audit(this.db.db, {
|
|
211
|
+
actorAccountId: actor.accountId,
|
|
212
|
+
...(actor.tokenId !== undefined && { actorTokenId: actor.tokenId }),
|
|
213
|
+
action: "platform.icon.updated",
|
|
214
|
+
target: file.purpose,
|
|
215
|
+
detail: { purpose: file.purpose, sha256: file.sha256, ...auditDetail },
|
|
216
|
+
});
|
|
217
|
+
return viewOf(file.purpose, file.sha256, row.updated_at);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Restores the default of a purpose: the row goes and its object's deletion is owed. Clearing a
|
|
221
|
+
* purpose with no upload changes nothing; returns whether a row was removed.
|
|
222
|
+
*/
|
|
223
|
+
async clear(purpose, actor, auditDetail = {}) {
|
|
224
|
+
return this.locks.run(platformIconLockKey(purpose), PLATFORM_ICON_LOCK, () => this.remove(purpose, actor, auditDetail));
|
|
225
|
+
}
|
|
226
|
+
async remove(purpose, actor, auditDetail) {
|
|
227
|
+
await this.adminLock.lockAndAuthorize(actor.accountId);
|
|
228
|
+
const gone = await this.db
|
|
229
|
+
.deleteFrom("platform_icons")
|
|
230
|
+
.where("purpose", "=", purpose)
|
|
231
|
+
.returning("sha256")
|
|
232
|
+
.executeTakeFirst();
|
|
233
|
+
if (!gone)
|
|
234
|
+
return false;
|
|
235
|
+
await owePlatformIconDelete(this.outbox, purpose, gone.sha256);
|
|
236
|
+
await audit(this.db.db, {
|
|
237
|
+
actorAccountId: actor.accountId,
|
|
238
|
+
...(actor.tokenId !== undefined && { actorTokenId: actor.tokenId }),
|
|
239
|
+
action: "platform.icon.cleared",
|
|
240
|
+
target: purpose,
|
|
241
|
+
detail: { purpose, sha256: gone.sha256, ...auditDetail },
|
|
242
|
+
});
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* The stored bytes of a purpose for the public route, or `platform_icon.not_found` when the
|
|
247
|
+
* default is in use, `v` names another upload, or the object is missing. `immutable` is true
|
|
248
|
+
* when `v` is the current hash (the URL then never changes meaning).
|
|
249
|
+
*/
|
|
250
|
+
async read(purpose, v) {
|
|
251
|
+
const row = await this.db
|
|
252
|
+
.selectFrom("platform_icons")
|
|
253
|
+
.select(["object_key", "sha256"])
|
|
254
|
+
.where("purpose", "=", purpose)
|
|
255
|
+
.executeTakeFirst();
|
|
256
|
+
const notFound = () => new ApiError("platform_icon.not_found", v === undefined || row === undefined
|
|
257
|
+
? `No ${purpose} icon is uploaded; the built-in Capybara icon applies`
|
|
258
|
+
: `The ${purpose} icon was replaced; read the current URL from the platform icon settings`);
|
|
259
|
+
if (!row || (v !== undefined && v !== row.sha256))
|
|
260
|
+
throw notFound();
|
|
261
|
+
let obj;
|
|
262
|
+
try {
|
|
263
|
+
obj = await this.storage.deployment().get(row.object_key);
|
|
264
|
+
}
|
|
265
|
+
catch (e) {
|
|
266
|
+
if (isStorageUnavailable(e))
|
|
267
|
+
throw storageUnavailable();
|
|
268
|
+
throw e;
|
|
269
|
+
}
|
|
270
|
+
if (!obj)
|
|
271
|
+
throw notFound();
|
|
272
|
+
const bytes = new Uint8Array(await new Response(obj.body).arrayBuffer());
|
|
273
|
+
// the type is the purpose's, never the store's metadata
|
|
274
|
+
return { contentType: CONTENT_TYPE[purpose], bytes, immutable: v !== undefined };
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
__decorate([
|
|
278
|
+
Transactional()
|
|
279
|
+
], PlatformIconService.prototype, "oweOrphanCheck", null);
|
|
280
|
+
__decorate([
|
|
281
|
+
Transactional()
|
|
282
|
+
], PlatformIconService.prototype, "commit", null);
|
|
283
|
+
__decorate([
|
|
284
|
+
Transactional()
|
|
285
|
+
], PlatformIconService.prototype, "remove", null);
|
|
286
|
+
PlatformIconService = __decorate([
|
|
287
|
+
Injectable(),
|
|
288
|
+
__param(0, Inject(KernelDb)),
|
|
289
|
+
__param(1, Inject(TransactionHost)),
|
|
290
|
+
__param(2, Inject(OutboxService)),
|
|
291
|
+
__param(3, Inject(SuperAdminLock)),
|
|
292
|
+
__param(4, Inject(LockService)),
|
|
293
|
+
__param(5, Inject(StorageService))
|
|
294
|
+
], PlatformIconService);
|
|
295
|
+
export { PlatformIconService };
|
|
296
|
+
const viewOf = (purpose, sha256, at) => ({
|
|
297
|
+
purpose,
|
|
298
|
+
url: platformIconUrl(purpose, sha256),
|
|
299
|
+
sha256,
|
|
300
|
+
updatedAt: at.toISOString(),
|
|
301
|
+
});
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Themes (themes spec §7-§9): resolution, the org's theme listing and delivery; and the platform
|
|
3
|
+
* icon setting (mobile layout spec §10.2.1).
|
|
4
|
+
*/
|
|
2
5
|
export declare class ThemesModule {
|
|
3
6
|
}
|
|
@@ -5,17 +5,22 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
};
|
|
7
7
|
import { Module } from "@nestjs/common";
|
|
8
|
+
import { SuperAdminLockModule } from "../identity/super-admin.js";
|
|
8
9
|
import { StorageModule } from "../storage/storage.module.js";
|
|
10
|
+
import { PlatformIconService } from "./platform-icon.service.js";
|
|
9
11
|
import { ThemeDeliveryService } from "./theme-delivery.service.js";
|
|
10
12
|
import { ThemesService } from "./themes.service.js";
|
|
11
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* Themes (themes spec §7-§9): resolution, the org's theme listing and delivery; and the platform
|
|
15
|
+
* icon setting (mobile layout spec §10.2.1).
|
|
16
|
+
*/
|
|
12
17
|
let ThemesModule = class ThemesModule {
|
|
13
18
|
};
|
|
14
19
|
ThemesModule = __decorate([
|
|
15
20
|
Module({
|
|
16
|
-
imports: [StorageModule],
|
|
17
|
-
providers: [ThemesService, ThemeDeliveryService],
|
|
18
|
-
exports: [ThemesService, ThemeDeliveryService],
|
|
21
|
+
imports: [StorageModule, SuperAdminLockModule],
|
|
22
|
+
providers: [ThemesService, ThemeDeliveryService, PlatformIconService],
|
|
23
|
+
exports: [ThemesService, ThemeDeliveryService, PlatformIconService],
|
|
19
24
|
})
|
|
20
25
|
], ThemesModule);
|
|
21
26
|
export { ThemesModule };
|