mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
* feat(plugins): add sync providers and encrypted sidecar sync
Implement PR 8 of the plugin platform (#2269): namespaced sync Providers
with provider.sync, encrypted-object storage operations, WebDAV through the
shared storage surface, dynamic cloud provider IDs, and non-cascade sidecars
for sync:true plugin settings plus account/CRDT baselines.
Related to #2269
* fix(plugins): wire sync providers and sidecars into production paths
Close PR 8 integration gaps: dynamic plugin provider registry and
getConnectedAdapter createPluginStorage, WebDAV through EncryptedObjectStorage,
cloud payload collect/apply for pluginSidecars, and IPC/preload bridges.
Related to #2269
* fix(plugins): preserve WebDAV auth reuse and resourceId through EOS wrap
encryptedObjectStorageAsCloudAdapter now reports initiallyAuthenticated when
config exists (matching raw WebDAVAdapter) so getConnectedAdapter reuses the
cached instance, and preserves/refreshes resourceId from the backing adapter
instead of always forcing the default object key.
Related to #2269
* fix(plugins): address Codex PR 8 review findings
Carry pluginSidecars through merge/convergent uploads, lazy-connect before
object I/O with revision-aware writes, fail closed on sidecar collect errors,
expose connectPluginProvider, preserve remote timestamps, and drop deleted
installed-plugin settings from collection.
Related to #2269
* fix(plugins): close remaining Codex P1/P2 sync issues
Lazy stream cancel for inline sync reads, must-not-exist conditional writes,
three-way sidecar merge for local resets, validated apply path for settings,
and merge remote sidecars into convergent uploads.
Related to #2269
* fix(plugins): harden sidecar collect/apply and write verification
Preserve last-known sidecars when the host is offline, apply remote
deletions for installed plugins, force convergent upload on sidecar
divergence, verify plugin object writes, and pick plugin providers in
auto-sync startup checks.
Related to #2269
* fix(plugins): close remaining Codex sidecar/sync path gaps
Include sidecars in auto-sync payload and hash, keep explicit empty bundles,
fail apply when host is offline, require getCapabilities, and carry merged
sidecars into convergent local apply/upload decisions.
Related to #2269
* fix(plugins): close Codex P1 sidecar collect/apply/merge gaps
Treat null host responses as unavailable, persist collected settings into
the non-cascade table, retain decoded convergent payloads, use three-way
sidecar merge for deletions, and fail apply only on operational errors.
Also treat non-empty plugin sidecars as meaningful sync data and re-run
auto-sync when plugin contributions change.
Related to #2269
* fix(plugins): close remaining Codex P1 sidecar and provider gaps
Replay offline-cached sidecars before collect, keep explicit empty
sidecar fields after three-way merge, thread sidecars through convergent
conflict/downgrade materialization, keep gated-off plugin providers from
joining sync as connected, and rebind plugin EOS sessions after runtime
replacement.
Related to #2269
* fix(plugins): separate pending remote sidecars from last-known cache
Replay only host-offline remote applies into the DB, keep last-known as
upload fallback, use contribution reset for deleted settings, probe real
plugin host readiness, rebind storage events for plugin providers, clear
plugin bases on reset, and swallow disconnect rejections.
Related to #2269
* fix(plugins): hydrate retained sidecars and gate missing providers
Materialize installed-plugin settings from retained sidecars before
collect pruning, clear sidecars on setting reset, fail closed when
pending remote replay fails, and only restore plugin providers that are
contribution-available. Also initialize dynamic provider counters on
connect.
Related to #2269
* fix(plugins): surface plugin sync providers and preserve conflict sidecars
Collect live sidecars before convergent conflict apply/upload, include
dynamic providers in status and disconnect paths, and list plugin sync
providers in Cloud Sync settings for connect/disconnect.
Related to #2269
* fix(plugins): per-provider sidecar baselines and safer collect/UI
Merge convergent sidecars against each provider's own baseline, keep
retained settings in sidecars without unvalidated plugin_settings writes,
guard isConnectDisabled for never-connected plugins, and fall back plugin
provider labels/icons in the status popup.
Related to #2269
* fix(plugins): correct plugin provider discovery and sidecar reverify
Read nested contribution metadata for sync provider cards, gate empty-config
connects, enforce single-provider disconnect for plugins, and re-verify after
sidecar-only convergent uploads so baselines track the post-write payload.
Related to #2269
* fix(plugins): refresh sync provider availability from live contributions
Replace the contribution-available provider ID set when plugins change so
disabled or uninstalled sync providers leave the auto-sync ready set and
drop to disconnected with config retained.
Related to #2269
* fix(plugins): preserve sidecars in backups, pending queue, and migration
Attach last-known plugin sidecars to protective local vault backups, fail
operationally when pending/last-known storage writes are rejected, carry
sidecars through convergent migration publish, and reject trailing garbage
on encrypted object parse.
Related to #2269
* fix(plugins): abort conflict collect failures and merge migration sidecars
Rethrow operational sidecar collection errors during convergent conflict
resolution, LWW-union all local/provider sidecars during convergent
migration, re-collect last-known after apply, and prefer live host collect
for protective local vault backups.
Related to #2269
* fix(plugins): restore pluginBridge tests, fix import path, union downgrade sidecars
Correct SettingsSyncTab dynamic import path so production builds resolve,
add ipcMain.on to the plugin bridge test double, union all provider
sidecars on convergent downgrade, keep empty last-known resets, and skip
force-delete when contribution reset validation rejects.
Related to #2269
* fix(plugins): abort backup collect failures and honor empty remote wipes
Only fall back to last-known sidecars when the host is unavailable during
protective backups, treat explicit empty remote sidecar bundles as
authoritative wipes, remove lint-blocking anys from harness tests, and
use a stable equal-timestamp sidecar tie-break.
Related to #2269
* fix(plugins): include sidecars in equality, preserve merge base on reconnect
Compare pluginSidecars in cloudSyncPayloadsEqual for migration freshness,
clear plugin merge state only when account/resource changes on reconnect,
cancel accepted sync read streams on failure, and stop re-connecting the
plugin EOS adapter on every I/O.
Related to #2269
* fix(plugins): drop cached adapters on contribution refresh
Invalidate plugin sync adapters when live contributions change so a
restarted runtime gets a fresh connect, take offline any non-disconnected
missing providers, and latch version-change backups only after cancellation
checks pass.
Related to #2269
* fix(plugins): preserve omitted sidecars and hydrate on plugin enable
Treat missing pluginSidecars as legacy/unsupported in three-way merge
(only explicit empty is a wipe), hydrate retained settings when a plugin
enables, and re-enable retained provider configs when contributions return.
Related to #2269
* fix(plugins): keep newer retained sidecars over older stored settings
When collection merges existing sidecars with plugin_settings rows, prefer
the newer updatedAt so a schema-rejected remote value retained only in
the sidecar table is not overwritten by a stale local setting.
Related to #2269
* fix(plugins): seal plugin provider config and clear base on config change
Encrypt opaque plugin provider configuration before persistence, clear
merge state when reconnect configuration changes, and stream sync objects
below a base64-safe inline threshold so control-plane JSON stays in budget.
Related to #2269
* fix(plugins): seal plugin configs by provider id and hydrate before start
Encrypt plugin provider configuration only for non-builtin provider IDs
so field-name collisions cannot skip sealing, hydrate retained sidecars
before plugin enable starts the runtime, and stop tearing down live
adapters on no-op availability refreshes.
Related to #2269
* fix(plugins): seal scalar configs, safe inline reads, stable config fingerprint
Encrypt any non-builtin plugin configuration JSON shape, enforce the
base64-safe inline read cutoff, and compare reconnect configurations with
order-independent fingerprints so merge bases are not cleared spuriously.
Related to #2269
* fix(plugins): thread sync credentials and harden availability checks
Pass SyncConnectPayload.credential through the object-storage and IPC host
path, treat only null/undefined config as missing, and avoid wiping the
plugin provider availability catalog on transient discovery failures.
Related to #2269
* fix(plugins): unblock schema-required sync connect and scalar configs
Add a JSON config dialog for plugin sync providers that declare a
required configurationSchema, reuse retained configs on reconnect, and
treat falsy scalar configs as present in readiness and adapter lookup.
Related to #2269
* fix(plugins): rebind sync adapters on contribution refresh
Drop cached plugin sync adapters when live contributions still include
the provider so replaced runtimes get a fresh connect, and seal falsy
scalar plugin configs with nullish presence checks end-to-end.
Related to #2269
* fix(plugins): apply sidecars under current declared setting scopes
When a plugin update changes a syncable setting scope, write under the
live declaration and drop the obsolete scope row so collection does not
republish duplicates.
Related to #2269
* fix(plugins): seal config envelopes and stabilize provider counters
Use an unambiguous host-owned sealed-config envelope, initialize
dynamic-provider sequence counters before cross-window decrypt, keep
three-way sidecar deletions during convergent migration, and create the
sidecar table for existing schema-1 plugin databases.
Related to #2269
* fix(plugins): avoid mid-sync adapter drops and sidecar resurrection
Keep plugin adapters across setting-only contribution refreshes and
rebind sessions on ensureConnected; stop preserving local-only missing
plugin sidecars when remote already mentions the plugin; compare configs
with nullish semantics across windows; swallow quick-connect failures
after toasting.
Related to #2269
* fix(plugins): restore remoteKeys for installed sidecar resets
Reintroduce the remote key set used when dropping installed-plugin
settings the remote no longer carries so applyFromSync cannot throw
after replacing the sidecar table.
Related to #2269
* fix(plugins): honor conflict policy and re-key scoped sidecars
Pass preferCloud/preferLocal into three-way sidecar merge, use
per-provider baselines on convergent downgrade so local resets are not
resurrected, and re-key applied settings under the current declared
scope coordinates.
Related to #2269
* fix(plugins): keep strategy on final sidecar merge and block dual legacy
Pass preferCloud/preferLocal through the final verified sidecar merge,
avoid auto-reactivating a retained plugin while another legacy provider
is already connected, cache plugin adapters only after initializeSync,
and preserve explicit null configuration values.
Related to #2269
* fix(plugins): strip plugin avatars and protect last-known sidecars
Do not load plugin-supplied avatar URLs in the main renderer, and avoid
overwriting last-known sidecars with an unmerged remote bundle when the
post-apply collect fails or is non-authoritative.
Related to #2269
* fix(plugins): strip top-bar plugin avatars and stop always-rebind
Hide plugin avatar URLs in the sync status popover, drop always-on
rebindSession for plugin adapters, and read convergent mode from the
local config module when deciding whether to auto-reactivate providers.
Related to #2269
* fix(plugins): use window.netcatty for sync IPC and allow null configs
Read plugin sync and sidecar APIs from the production preload global,
and treat an explicit config property (including JSON null) as present
for readiness and adapter lookup.
Related to #2269
* fix(plugins): close residual Codex P2 sync edge cases
Three-way merge sidecars during v2 convergent migration so local
resets are not resurrected, preserve explicit null plugin configs on
adapter rebuild, rebind EOS sessions after I/O failure for replaced
runtimes, and accept the public inlineObjectBytes limit on readObject.
Related to #2269
* fix(plugins): keep inline read cutoff under control-plane budget
Restore the base64-safe inline cutoff so advertised-size reads cannot
overflow the 128 KiB provider JSON envelope; providers near the public
inlineObjectBytes limit must stream.
Related to #2269
* fix(plugins): harden sidecar empty-vault, hydrate LWW, and credential seals
Close remaining Codex/review gaps for PR8: keep empty sidecar shells from
bypassing upload guards, skip startup hydrate when local settings are newer,
seal/validate durable plugin credential refs, and cover schema 1→2 migration.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): rebind plugin sync sessions and keep null configs
Address Codex P2 on 8ae52c54: re-issue connect after runtime replacement,
and treat stored config:null as present in the dashboard reconnect path.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): defer empty sidecar last-known until sync succeeds
Keep prior non-empty last-known across an authoritative empty collect so the
empty-vault guard can still recognize a plugin-only reset, then commit the
empty cache after a successful upload.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): avoid restricted localStorage global in sidecar test
Bind the mock to a local binding so eslint no-restricted-globals stays clean.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): commit applied sidecar cache and reset orphan settings
Prefer merged payload sidecars when updating last-known after sync, and clear
syncable plugin_settings that lack sidecar rows on authoritative remote deletes.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): reseal marker-shaped plugin configs instead of skipping
Exact __netcatty_plugin_config_v1 collisions no longer bypass encryption; trusted
host envelopes are unwrapped then resealed, and false collisions are sealed whole.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): keep merged sidecars in last-known after apply collect fails
Return applyFromSync entries from IPC and use them when follow-up collect fails,
so preserved missing-plugin rows are not dropped from the offline upload cache.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): delete stale-scope settings at stored coordinates on reset
After resetSetting against the current declared scope, also remove the row at
the sidecar's stored scope so scope migrations cannot republish remote deletes.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): close sync-provider credential, stream, and sidecar gaps
Make SecretRef usable end-to-end via lease-bound network.request, keep
renderer/main transfers abortable and bounded, and stop sidecar
last-known/pending from corrupting backups and conflict apply.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): harden single-provider, secret schema, and conditional conflicts
Keep legacy restart from dual-connecting providers, extract writeOnly schema
secrets into OS storage, and surface conditional-write precondition failures
as conflict UI instead of a generic sync error.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): dedupe sync type imports for lint
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix: address Codex review on PR #2713
* fix(plugins): wait for sidecar host before version backup latch
Defer the one-shot upgrade snapshot until the plugin sidecar host is ready (or a short grace), and catch getDataHash failures so pending/baseline paths do not reject unhandled.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): require sidecar grace before version backup
Always wait for contributions/grace tick before the upgrade snapshot, and only latch after a non-cancelled attempt so cleanup cannot suppress retries.
Co-authored-by: Cursor <cursoragent@cursor.com>
---------
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
816 lines
30 KiB
JavaScript
816 lines
30 KiB
JavaScript
"use strict";
|
|
|
|
const { createHash } = require("node:crypto");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const { DatabaseSync } = require("node:sqlite");
|
|
|
|
const SCHEMA_VERSION = 2;
|
|
const MAX_SECURITY_AUDIT_DETAILS_BYTES = 16 * 1024;
|
|
const MAX_SECURITY_AUDIT_RECORDS_PER_PLUGIN = 1_000;
|
|
const REQUIRED_SCHEMA_COLUMNS = Object.freeze({
|
|
plugins: ["id", "enabled", "active_version", "installed_at", "updated_at"],
|
|
plugin_versions: ["plugin_id", "version", "manifest_json", "archive_sha256", "package_relative_path", "installed_at"],
|
|
plugin_runtime_state: ["plugin_id", "plugin_version", "status", "runtime_kind", "last_error", "quarantined_at", "updated_at"],
|
|
plugin_crashes: ["plugin_id", "plugin_version", "crashed_at"],
|
|
plugin_kv: ["plugin_id", "key", "value_json", "updated_at"],
|
|
plugin_settings: ["plugin_id", "setting_id", "scope", "scope_id", "value_json", "updated_at"],
|
|
plugin_view_state: ["plugin_id", "view_id", "scope_id", "state_json", "updated_at"],
|
|
plugin_permission_grants: ["plugin_id", "permission", "resource", "resource_kind", "declaration_hash", "granted_at"],
|
|
plugin_secrets: ["plugin_id", "key", "secret_ref", "ciphertext", "created_at", "updated_at"],
|
|
plugin_security_audit: ["id", "plugin_id", "event", "details_json", "created_at"],
|
|
// User-owned encrypted-sync sidecars: no FK cascade on package uninstall.
|
|
plugin_sync_sidecars: ["plugin_id", "kind", "key", "value_json", "updated_at"],
|
|
});
|
|
|
|
function parseJson(text, label) {
|
|
try {
|
|
return JSON.parse(text);
|
|
} catch {
|
|
throw new Error(`Corrupt plugin database ${label}`);
|
|
}
|
|
}
|
|
|
|
class PluginDatabase {
|
|
constructor(databasePath, options = {}) {
|
|
if (typeof databasePath !== "string" || !path.isAbsolute(databasePath)) {
|
|
throw new TypeError("Plugin database path must be absolute");
|
|
}
|
|
fs.mkdirSync(path.dirname(databasePath), { recursive: true, mode: 0o700 });
|
|
this.databasePath = databasePath;
|
|
this.clock = options.clock ?? (() => Date.now());
|
|
const ownsDatabase = !options.database;
|
|
this.db = options.database ?? new DatabaseSync(databasePath);
|
|
try {
|
|
this.#initializeSchema();
|
|
} catch (error) {
|
|
if (ownsDatabase) {
|
|
try { this.db.close(); } catch {}
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
#initializeSchema() {
|
|
this.db.exec("PRAGMA foreign_keys = ON; PRAGMA journal_mode = WAL; PRAGMA synchronous = FULL;");
|
|
const version = Number(this.db.prepare("PRAGMA user_version").get()?.user_version ?? 0);
|
|
if (version > SCHEMA_VERSION) {
|
|
throw new Error(`Plugin database schema ${version} is newer than supported ${SCHEMA_VERSION}`);
|
|
}
|
|
if (version === 0) {
|
|
this.transaction(() => {
|
|
this.db.exec(`
|
|
CREATE TABLE plugins (
|
|
id TEXT PRIMARY KEY,
|
|
enabled INTEGER NOT NULL DEFAULT 0 CHECK (enabled IN (0, 1)),
|
|
active_version TEXT,
|
|
installed_at INTEGER NOT NULL,
|
|
updated_at INTEGER NOT NULL
|
|
);
|
|
CREATE TABLE plugin_versions (
|
|
plugin_id TEXT NOT NULL REFERENCES plugins(id) ON DELETE CASCADE,
|
|
version TEXT NOT NULL,
|
|
manifest_json TEXT NOT NULL,
|
|
archive_sha256 TEXT NOT NULL,
|
|
package_relative_path TEXT NOT NULL,
|
|
installed_at INTEGER NOT NULL,
|
|
PRIMARY KEY (plugin_id, version)
|
|
);
|
|
CREATE TABLE plugin_runtime_state (
|
|
plugin_id TEXT NOT NULL,
|
|
plugin_version TEXT NOT NULL,
|
|
status TEXT NOT NULL DEFAULT 'stopped',
|
|
runtime_kind TEXT,
|
|
last_error TEXT,
|
|
quarantined_at INTEGER,
|
|
updated_at INTEGER NOT NULL,
|
|
PRIMARY KEY (plugin_id, plugin_version),
|
|
FOREIGN KEY (plugin_id, plugin_version)
|
|
REFERENCES plugin_versions(plugin_id, version) ON DELETE CASCADE
|
|
);
|
|
CREATE TABLE plugin_crashes (
|
|
plugin_id TEXT NOT NULL,
|
|
plugin_version TEXT NOT NULL,
|
|
crashed_at INTEGER NOT NULL,
|
|
FOREIGN KEY (plugin_id, plugin_version)
|
|
REFERENCES plugin_versions(plugin_id, version) ON DELETE CASCADE
|
|
);
|
|
CREATE INDEX plugin_crashes_lookup
|
|
ON plugin_crashes(plugin_id, plugin_version, crashed_at);
|
|
CREATE TABLE plugin_kv (
|
|
plugin_id TEXT NOT NULL REFERENCES plugins(id) ON DELETE CASCADE,
|
|
key TEXT NOT NULL,
|
|
value_json TEXT NOT NULL,
|
|
updated_at INTEGER NOT NULL,
|
|
PRIMARY KEY (plugin_id, key)
|
|
);
|
|
CREATE TABLE plugin_settings (
|
|
plugin_id TEXT NOT NULL,
|
|
setting_id TEXT NOT NULL,
|
|
scope TEXT NOT NULL CHECK (scope IN ('application', 'workspace', 'host', 'session', 'device')),
|
|
scope_id TEXT NOT NULL,
|
|
value_json TEXT NOT NULL,
|
|
updated_at INTEGER NOT NULL,
|
|
PRIMARY KEY (plugin_id, setting_id, scope, scope_id)
|
|
);
|
|
CREATE INDEX plugin_settings_lookup
|
|
ON plugin_settings(plugin_id, scope, scope_id, setting_id);
|
|
CREATE TABLE plugin_view_state (
|
|
plugin_id TEXT NOT NULL,
|
|
view_id TEXT NOT NULL,
|
|
scope_id TEXT NOT NULL,
|
|
state_json TEXT NOT NULL,
|
|
updated_at INTEGER NOT NULL,
|
|
PRIMARY KEY (plugin_id, view_id, scope_id)
|
|
);
|
|
CREATE INDEX plugin_view_state_lookup
|
|
ON plugin_view_state(plugin_id, scope_id, view_id);
|
|
CREATE TABLE plugin_permission_grants (
|
|
plugin_id TEXT NOT NULL,
|
|
permission TEXT NOT NULL,
|
|
resource TEXT NOT NULL,
|
|
resource_kind TEXT NOT NULL CHECK (resource_kind IN ('exact', 'directory')),
|
|
declaration_hash TEXT NOT NULL,
|
|
granted_at INTEGER NOT NULL,
|
|
PRIMARY KEY (plugin_id, permission, resource)
|
|
);
|
|
CREATE TABLE plugin_secrets (
|
|
plugin_id TEXT NOT NULL,
|
|
key TEXT NOT NULL,
|
|
secret_ref TEXT NOT NULL UNIQUE,
|
|
ciphertext BLOB NOT NULL,
|
|
created_at INTEGER NOT NULL,
|
|
updated_at INTEGER NOT NULL,
|
|
PRIMARY KEY (plugin_id, key)
|
|
);
|
|
CREATE INDEX plugin_secrets_ref_lookup
|
|
ON plugin_secrets(plugin_id, secret_ref);
|
|
CREATE TABLE plugin_security_audit (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
plugin_id TEXT NOT NULL,
|
|
event TEXT NOT NULL,
|
|
details_json TEXT NOT NULL,
|
|
created_at INTEGER NOT NULL
|
|
);
|
|
CREATE INDEX plugin_security_audit_lookup
|
|
ON plugin_security_audit(plugin_id, created_at DESC);
|
|
CREATE TABLE plugin_sync_sidecars (
|
|
plugin_id TEXT NOT NULL,
|
|
kind TEXT NOT NULL CHECK (kind IN ('settings', 'account_baseline', 'crdt_baseline')),
|
|
key TEXT NOT NULL,
|
|
value_json TEXT NOT NULL,
|
|
updated_at INTEGER NOT NULL,
|
|
PRIMARY KEY (plugin_id, kind, key)
|
|
);
|
|
CREATE INDEX plugin_sync_sidecars_lookup
|
|
ON plugin_sync_sidecars(plugin_id, kind, key);
|
|
PRAGMA user_version = 2;
|
|
`);
|
|
});
|
|
} else if (version === 1) {
|
|
// Pre-sidecar schema-1 databases only created the original tables.
|
|
// Migrate in place to schema 2 with the non-cascade sidecar table.
|
|
this.transaction(() => {
|
|
this.db.exec(`
|
|
CREATE TABLE IF NOT EXISTS plugin_sync_sidecars (
|
|
plugin_id TEXT NOT NULL,
|
|
kind TEXT NOT NULL CHECK (kind IN ('settings', 'account_baseline', 'crdt_baseline')),
|
|
key TEXT NOT NULL,
|
|
value_json TEXT NOT NULL,
|
|
updated_at INTEGER NOT NULL,
|
|
PRIMARY KEY (plugin_id, kind, key)
|
|
);
|
|
CREATE INDEX IF NOT EXISTS plugin_sync_sidecars_lookup
|
|
ON plugin_sync_sidecars(plugin_id, kind, key);
|
|
PRAGMA user_version = 2;
|
|
`);
|
|
});
|
|
}
|
|
this.#assertSchemaLayout();
|
|
}
|
|
|
|
#assertSchemaLayout() {
|
|
for (const [table, columns] of Object.entries(REQUIRED_SCHEMA_COLUMNS)) {
|
|
const actual = this.db.prepare(`PRAGMA table_info(${table})`).all().map(({ name }) => name);
|
|
if (JSON.stringify(actual) !== JSON.stringify(columns)) {
|
|
throw new Error(
|
|
"Pre-release plugin database schema is obsolete; reset userData/plugins/plugins.sqlite",
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
transaction(callback) {
|
|
this.db.exec("BEGIN IMMEDIATE");
|
|
try {
|
|
const result = callback();
|
|
if (result && typeof result.then === "function") {
|
|
throw new TypeError("Plugin database transactions must be synchronous");
|
|
}
|
|
this.db.exec("COMMIT");
|
|
return result;
|
|
} catch (error) {
|
|
try { this.db.exec("ROLLBACK"); } catch {}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
installVersion(record, options = {}) {
|
|
if (options.enable === true && options.forceDisabled === true) {
|
|
throw new TypeError("Plugin version cannot be enabled and force-disabled together");
|
|
}
|
|
const now = this.clock();
|
|
const manifestJson = JSON.stringify(record.manifest);
|
|
const requestedEnabled = options.enable === true ? 1 : 0;
|
|
const overwriteEnabled = options.enable === true || options.forceDisabled === true;
|
|
this.transaction(() => {
|
|
this.db.prepare(`
|
|
INSERT INTO plugins(id, enabled, active_version, installed_at, updated_at)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
ON CONFLICT(id) DO UPDATE SET
|
|
active_version = excluded.active_version,
|
|
enabled = CASE WHEN ? THEN excluded.enabled ELSE plugins.enabled END,
|
|
updated_at = excluded.updated_at
|
|
`).run(
|
|
record.pluginId,
|
|
requestedEnabled,
|
|
record.version,
|
|
now,
|
|
now,
|
|
overwriteEnabled ? 1 : 0,
|
|
);
|
|
this.db.prepare(`
|
|
INSERT INTO plugin_versions(
|
|
plugin_id, version, manifest_json, archive_sha256, package_relative_path, installed_at
|
|
) VALUES (?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(plugin_id, version) DO UPDATE SET
|
|
manifest_json = excluded.manifest_json,
|
|
archive_sha256 = excluded.archive_sha256,
|
|
package_relative_path = excluded.package_relative_path,
|
|
installed_at = excluded.installed_at
|
|
`).run(
|
|
record.pluginId,
|
|
record.version,
|
|
manifestJson,
|
|
record.archiveSha256,
|
|
record.packageRelativePath,
|
|
now,
|
|
);
|
|
this.db.prepare(`
|
|
INSERT INTO plugin_runtime_state(plugin_id, plugin_version, status, updated_at)
|
|
VALUES (?, ?, 'stopped', ?)
|
|
ON CONFLICT(plugin_id, plugin_version) DO NOTHING
|
|
`).run(record.pluginId, record.version, now);
|
|
});
|
|
}
|
|
|
|
getVersion(pluginId, version) {
|
|
const row = this.db.prepare(`
|
|
SELECT plugin_id, version, manifest_json, archive_sha256,
|
|
package_relative_path, installed_at
|
|
FROM plugin_versions WHERE plugin_id = ? AND version = ?
|
|
`).get(pluginId, version);
|
|
return row ? this.#mapVersion(row) : null;
|
|
}
|
|
|
|
getActivePlugin(pluginId) {
|
|
const row = this.db.prepare(`
|
|
SELECT p.id, p.enabled, p.active_version, p.installed_at, p.updated_at,
|
|
v.manifest_json, v.archive_sha256, v.package_relative_path,
|
|
r.status, r.runtime_kind, r.last_error, r.quarantined_at
|
|
FROM plugins p
|
|
LEFT JOIN plugin_versions v
|
|
ON v.plugin_id = p.id AND v.version = p.active_version
|
|
LEFT JOIN plugin_runtime_state r ON r.plugin_id = p.id
|
|
AND r.plugin_version = p.active_version
|
|
WHERE p.id = ?
|
|
`).get(pluginId);
|
|
return row ? this.#mapPlugin(row) : null;
|
|
}
|
|
|
|
listPlugins() {
|
|
return this.db.prepare(`
|
|
SELECT p.id, p.enabled, p.active_version, p.installed_at, p.updated_at,
|
|
v.manifest_json, v.archive_sha256, v.package_relative_path,
|
|
r.status, r.runtime_kind, r.last_error, r.quarantined_at
|
|
FROM plugins p
|
|
LEFT JOIN plugin_versions v
|
|
ON v.plugin_id = p.id AND v.version = p.active_version
|
|
LEFT JOIN plugin_runtime_state r ON r.plugin_id = p.id
|
|
AND r.plugin_version = p.active_version
|
|
ORDER BY p.id COLLATE BINARY
|
|
`).all().map((row) => this.#mapPlugin(row));
|
|
}
|
|
|
|
#mapVersion(row) {
|
|
return {
|
|
pluginId: row.plugin_id,
|
|
version: row.version,
|
|
manifest: parseJson(row.manifest_json, "manifest"),
|
|
archiveSha256: row.archive_sha256,
|
|
packageRelativePath: row.package_relative_path,
|
|
installedAt: Number(row.installed_at),
|
|
};
|
|
}
|
|
|
|
#mapPlugin(row) {
|
|
return {
|
|
id: row.id,
|
|
enabled: row.enabled === 1,
|
|
activeVersion: row.active_version ?? null,
|
|
installedAt: Number(row.installed_at),
|
|
updatedAt: Number(row.updated_at),
|
|
manifest: row.manifest_json ? parseJson(row.manifest_json, "manifest") : null,
|
|
archiveSha256: row.archive_sha256 ?? null,
|
|
packageRelativePath: row.package_relative_path ?? null,
|
|
runtime: {
|
|
status: row.status ?? "stopped",
|
|
kind: row.runtime_kind ?? null,
|
|
lastError: row.last_error ?? null,
|
|
quarantinedAt: row.quarantined_at == null ? null : Number(row.quarantined_at),
|
|
},
|
|
};
|
|
}
|
|
|
|
setEnabled(pluginId, enabled) {
|
|
const result = this.db.prepare("UPDATE plugins SET enabled = ?, updated_at = ? WHERE id = ?")
|
|
.run(enabled ? 1 : 0, this.clock(), pluginId);
|
|
if (Number(result.changes) !== 1) throw new Error(`Plugin is not installed: ${pluginId}`);
|
|
}
|
|
|
|
setActiveVersion(pluginId, version, options = {}) {
|
|
this.transaction(() => {
|
|
if (!this.getVersion(pluginId, version)) {
|
|
throw new Error(`Plugin version is not installed: ${pluginId}@${version}`);
|
|
}
|
|
const enabled = options.enabled === true ? 1 : 0;
|
|
const expectedActiveVersion = options.expectedActiveVersion;
|
|
const result = expectedActiveVersion === undefined
|
|
? this.db.prepare(`
|
|
UPDATE plugins
|
|
SET active_version = ?, enabled = ?, updated_at = ?
|
|
WHERE id = ?
|
|
`).run(version, enabled, this.clock(), pluginId)
|
|
: this.db.prepare(`
|
|
UPDATE plugins
|
|
SET active_version = ?, enabled = ?, updated_at = ?
|
|
WHERE id = ? AND active_version = ?
|
|
`).run(version, enabled, this.clock(), pluginId, expectedActiveVersion);
|
|
if (Number(result.changes) !== 1) {
|
|
throw new Error(`Plugin active version changed before it could be restored: ${pluginId}`);
|
|
}
|
|
});
|
|
return this.getActivePlugin(pluginId);
|
|
}
|
|
|
|
setRuntimeState(pluginId, status, options = {}) {
|
|
const activeVersion = this.db.prepare(
|
|
"SELECT active_version FROM plugins WHERE id = ?",
|
|
).get(pluginId)?.active_version;
|
|
const pluginVersion = options.pluginVersion ?? activeVersion;
|
|
if (!pluginVersion) throw new Error(`Plugin is not installed: ${pluginId}`);
|
|
if (!this.getVersion(pluginId, pluginVersion)) {
|
|
throw new Error(`Plugin version is not installed: ${pluginId}@${pluginVersion}`);
|
|
}
|
|
this.db.prepare(`
|
|
INSERT INTO plugin_runtime_state(
|
|
plugin_id, plugin_version, status, runtime_kind,
|
|
last_error, quarantined_at, updated_at
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(plugin_id, plugin_version) DO UPDATE SET
|
|
status = excluded.status,
|
|
runtime_kind = excluded.runtime_kind,
|
|
last_error = excluded.last_error,
|
|
quarantined_at = COALESCE(excluded.quarantined_at, plugin_runtime_state.quarantined_at),
|
|
updated_at = excluded.updated_at
|
|
`).run(
|
|
pluginId,
|
|
pluginVersion,
|
|
status,
|
|
options.kind ?? null,
|
|
options.error == null ? null : String(options.error).slice(0, 4_096),
|
|
options.quarantinedAt ?? null,
|
|
this.clock(),
|
|
);
|
|
}
|
|
|
|
recordCrash(pluginId, pluginVersion, windowMs, threshold) {
|
|
const now = this.clock();
|
|
return this.transaction(() => {
|
|
if (!this.getVersion(pluginId, pluginVersion)) {
|
|
throw new Error(`Plugin version is not installed: ${pluginId}@${pluginVersion}`);
|
|
}
|
|
this.db.prepare(`
|
|
DELETE FROM plugin_crashes
|
|
WHERE plugin_id = ? AND plugin_version = ? AND crashed_at < ?
|
|
`).run(pluginId, pluginVersion, now - windowMs);
|
|
this.db.prepare(`
|
|
INSERT INTO plugin_crashes(plugin_id, plugin_version, crashed_at)
|
|
VALUES (?, ?, ?)
|
|
`).run(pluginId, pluginVersion, now);
|
|
const count = Number(this.db.prepare(
|
|
"SELECT COUNT(*) AS count FROM plugin_crashes WHERE plugin_id = ? AND plugin_version = ?",
|
|
).get(pluginId, pluginVersion)?.count ?? 0);
|
|
if (count >= threshold) {
|
|
this.db.prepare(`
|
|
UPDATE plugin_runtime_state
|
|
SET status = 'quarantined', quarantined_at = ?, updated_at = ?
|
|
WHERE plugin_id = ? AND plugin_version = ?
|
|
`).run(now, now, pluginId, pluginVersion);
|
|
}
|
|
return { count, quarantined: count >= threshold, quarantinedAt: count >= threshold ? now : null };
|
|
});
|
|
}
|
|
|
|
clearQuarantine(pluginId, pluginVersion) {
|
|
const now = this.clock();
|
|
this.transaction(() => {
|
|
const activeVersion = this.db.prepare(
|
|
"SELECT active_version FROM plugins WHERE id = ?",
|
|
).get(pluginId)?.active_version;
|
|
const targetVersion = pluginVersion ?? activeVersion;
|
|
if (!targetVersion) throw new Error(`Plugin is not installed: ${pluginId}`);
|
|
if (!this.getVersion(pluginId, targetVersion)) {
|
|
throw new Error(`Plugin version is not installed: ${pluginId}@${targetVersion}`);
|
|
}
|
|
this.db.prepare(`
|
|
DELETE FROM plugin_crashes WHERE plugin_id = ? AND plugin_version = ?
|
|
`).run(pluginId, targetVersion);
|
|
this.db.prepare(`
|
|
UPDATE plugin_runtime_state
|
|
SET status = 'stopped', last_error = NULL, quarantined_at = NULL, updated_at = ?
|
|
WHERE plugin_id = ? AND plugin_version = ?
|
|
`).run(now, pluginId, targetVersion);
|
|
});
|
|
}
|
|
|
|
removePlugin(pluginId) {
|
|
this.db.prepare("DELETE FROM plugins WHERE id = ?").run(pluginId);
|
|
}
|
|
|
|
getValue(pluginId, key) {
|
|
const row = this.db.prepare(
|
|
"SELECT value_json FROM plugin_kv WHERE plugin_id = ? AND key = ?",
|
|
).get(pluginId, key);
|
|
return row ? parseJson(row.value_json, "key/value entry") : undefined;
|
|
}
|
|
|
|
setValue(pluginId, key, value) {
|
|
const serialized = JSON.stringify(value);
|
|
if (serialized === undefined) throw new TypeError("Plugin storage value must be JSON serializable");
|
|
this.db.prepare(`
|
|
INSERT INTO plugin_kv(plugin_id, key, value_json, updated_at)
|
|
VALUES (?, ?, ?, ?)
|
|
ON CONFLICT(plugin_id, key) DO UPDATE SET
|
|
value_json = excluded.value_json,
|
|
updated_at = excluded.updated_at
|
|
`).run(pluginId, key, serialized, this.clock());
|
|
}
|
|
|
|
deleteValue(pluginId, key) {
|
|
this.db.prepare("DELETE FROM plugin_kv WHERE plugin_id = ? AND key = ?").run(pluginId, key);
|
|
}
|
|
|
|
listKeys(pluginId) {
|
|
return this.db.prepare(
|
|
"SELECT key FROM plugin_kv WHERE plugin_id = ? ORDER BY key COLLATE BINARY",
|
|
).all(pluginId).map((row) => row.key);
|
|
}
|
|
|
|
getSetting(pluginId, settingId, scope, scopeId) {
|
|
const row = this.db.prepare(`
|
|
SELECT value_json FROM plugin_settings
|
|
WHERE plugin_id = ? AND setting_id = ? AND scope = ? AND scope_id = ?
|
|
`).get(pluginId, settingId, scope, scopeId);
|
|
return row ? parseJson(row.value_json, "setting value") : undefined;
|
|
}
|
|
|
|
setSetting(pluginId, settingId, scope, scopeId, value, updatedAt = this.clock()) {
|
|
const serialized = JSON.stringify(value);
|
|
if (serialized === undefined) throw new TypeError("Plugin setting value must be JSON serializable");
|
|
const stamp = Number.isFinite(Number(updatedAt)) ? Number(updatedAt) : this.clock();
|
|
this.db.prepare(`
|
|
INSERT INTO plugin_settings(plugin_id, setting_id, scope, scope_id, value_json, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(plugin_id, setting_id, scope, scope_id) DO UPDATE SET
|
|
value_json = excluded.value_json,
|
|
updated_at = excluded.updated_at
|
|
`).run(pluginId, settingId, scope, scopeId, serialized, stamp);
|
|
}
|
|
|
|
deleteSetting(pluginId, settingId, scope, scopeId) {
|
|
this.db.prepare(`
|
|
DELETE FROM plugin_settings
|
|
WHERE plugin_id = ? AND setting_id = ? AND scope = ? AND scope_id = ?
|
|
`).run(pluginId, settingId, scope, scopeId);
|
|
}
|
|
|
|
listSettings(pluginId) {
|
|
return this.db.prepare(`
|
|
SELECT setting_id, scope, scope_id, value_json, updated_at
|
|
FROM plugin_settings WHERE plugin_id = ?
|
|
ORDER BY setting_id COLLATE BINARY, scope COLLATE BINARY, scope_id COLLATE BINARY
|
|
`).all(pluginId).map((row) => ({
|
|
settingId: row.setting_id,
|
|
scope: row.scope,
|
|
scopeId: row.scope_id,
|
|
value: parseJson(row.value_json, "setting value"),
|
|
updatedAt: Number(row.updated_at),
|
|
}));
|
|
}
|
|
|
|
listAllSettings() {
|
|
return this.db.prepare(`
|
|
SELECT plugin_id, setting_id, scope, scope_id, value_json, updated_at
|
|
FROM plugin_settings
|
|
ORDER BY plugin_id COLLATE BINARY, setting_id COLLATE BINARY, scope COLLATE BINARY, scope_id COLLATE BINARY
|
|
`).all().map((row) => ({
|
|
pluginId: row.plugin_id,
|
|
settingId: row.setting_id,
|
|
scope: row.scope,
|
|
scopeId: row.scope_id,
|
|
value: parseJson(row.value_json, "setting value"),
|
|
updatedAt: Number(row.updated_at),
|
|
}));
|
|
}
|
|
|
|
getSyncSidecar(pluginId, kind, key) {
|
|
const row = this.db.prepare(`
|
|
SELECT value_json, updated_at FROM plugin_sync_sidecars
|
|
WHERE plugin_id = ? AND kind = ? AND key = ?
|
|
`).get(pluginId, kind, key);
|
|
if (!row) return undefined;
|
|
return {
|
|
pluginId,
|
|
kind,
|
|
key,
|
|
value: parseJson(row.value_json, "sync sidecar value"),
|
|
updatedAt: Number(row.updated_at),
|
|
};
|
|
}
|
|
|
|
setSyncSidecar(pluginId, kind, key, value, updatedAt = this.clock()) {
|
|
const serialized = JSON.stringify(value);
|
|
if (serialized === undefined) throw new TypeError("Plugin sync sidecar value must be JSON serializable");
|
|
this.db.prepare(`
|
|
INSERT INTO plugin_sync_sidecars(plugin_id, kind, key, value_json, updated_at)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
ON CONFLICT(plugin_id, kind, key) DO UPDATE SET
|
|
value_json = excluded.value_json,
|
|
updated_at = excluded.updated_at
|
|
`).run(pluginId, kind, key, serialized, updatedAt);
|
|
}
|
|
|
|
deleteSyncSidecar(pluginId, kind, key) {
|
|
this.db.prepare(`
|
|
DELETE FROM plugin_sync_sidecars
|
|
WHERE plugin_id = ? AND kind = ? AND key = ?
|
|
`).run(pluginId, kind, key);
|
|
}
|
|
|
|
listSyncSidecars(pluginId) {
|
|
return this.db.prepare(`
|
|
SELECT plugin_id, kind, key, value_json, updated_at
|
|
FROM plugin_sync_sidecars
|
|
WHERE plugin_id = ?
|
|
ORDER BY kind COLLATE BINARY, key COLLATE BINARY
|
|
`).all(pluginId).map((row) => ({
|
|
pluginId: row.plugin_id,
|
|
kind: row.kind,
|
|
key: row.key,
|
|
value: parseJson(row.value_json, "sync sidecar value"),
|
|
updatedAt: Number(row.updated_at),
|
|
}));
|
|
}
|
|
|
|
listAllSyncSidecars() {
|
|
return this.db.prepare(`
|
|
SELECT plugin_id, kind, key, value_json, updated_at
|
|
FROM plugin_sync_sidecars
|
|
ORDER BY plugin_id COLLATE BINARY, kind COLLATE BINARY, key COLLATE BINARY
|
|
`).all().map((row) => ({
|
|
pluginId: row.plugin_id,
|
|
kind: row.kind,
|
|
key: row.key,
|
|
value: parseJson(row.value_json, "sync sidecar value"),
|
|
updatedAt: Number(row.updated_at),
|
|
}));
|
|
}
|
|
|
|
replaceAllSyncSidecars(entries) {
|
|
this.transaction(() => {
|
|
this.db.prepare("DELETE FROM plugin_sync_sidecars").run();
|
|
const insert = this.db.prepare(`
|
|
INSERT INTO plugin_sync_sidecars(plugin_id, kind, key, value_json, updated_at)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
`);
|
|
for (const entry of entries) {
|
|
const serialized = JSON.stringify(entry.value);
|
|
if (serialized === undefined) {
|
|
throw new TypeError("Plugin sync sidecar value must be JSON serializable");
|
|
}
|
|
insert.run(
|
|
entry.pluginId,
|
|
entry.kind,
|
|
entry.key,
|
|
serialized,
|
|
Number(entry.updatedAt) || this.clock(),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
getViewState(pluginId, viewId, scopeId) {
|
|
const row = this.db.prepare(`
|
|
SELECT state_json FROM plugin_view_state
|
|
WHERE plugin_id = ? AND view_id = ? AND scope_id = ?
|
|
`).get(pluginId, viewId, scopeId);
|
|
return row ? parseJson(row.state_json, "view state") : undefined;
|
|
}
|
|
|
|
setViewState(pluginId, viewId, scopeId, state) {
|
|
const serialized = JSON.stringify(state);
|
|
if (serialized === undefined) throw new TypeError("Plugin view state must be JSON serializable");
|
|
this.db.prepare(`
|
|
INSERT INTO plugin_view_state(plugin_id, view_id, scope_id, state_json, updated_at)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
ON CONFLICT(plugin_id, view_id, scope_id) DO UPDATE SET
|
|
state_json = excluded.state_json,
|
|
updated_at = excluded.updated_at
|
|
`).run(pluginId, viewId, scopeId, serialized, this.clock());
|
|
}
|
|
|
|
deleteViewState(pluginId, viewId, scopeId) {
|
|
this.db.prepare(`
|
|
DELETE FROM plugin_view_state
|
|
WHERE plugin_id = ? AND view_id = ? AND scope_id = ?
|
|
`).run(pluginId, viewId, scopeId);
|
|
}
|
|
|
|
listPermissionGrants(pluginId) {
|
|
return this.db.prepare(`
|
|
SELECT plugin_id, permission, resource, resource_kind, declaration_hash, granted_at
|
|
FROM plugin_permission_grants
|
|
WHERE plugin_id = ?
|
|
ORDER BY permission COLLATE BINARY, resource COLLATE BINARY
|
|
`).all(pluginId).map((row) => ({
|
|
pluginId: row.plugin_id,
|
|
permission: row.permission,
|
|
resource: row.resource,
|
|
resourceKind: row.resource_kind,
|
|
declarationHash: row.declaration_hash,
|
|
grantedAt: Number(row.granted_at),
|
|
}));
|
|
}
|
|
|
|
upsertPermissionGrant(record) {
|
|
this.db.prepare(`
|
|
INSERT INTO plugin_permission_grants(
|
|
plugin_id, permission, resource, resource_kind, declaration_hash, granted_at
|
|
) VALUES (?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(plugin_id, permission, resource) DO UPDATE SET
|
|
resource_kind = excluded.resource_kind,
|
|
declaration_hash = excluded.declaration_hash,
|
|
granted_at = excluded.granted_at
|
|
`).run(
|
|
record.pluginId,
|
|
record.permission,
|
|
record.resource,
|
|
record.resourceKind,
|
|
record.declarationHash,
|
|
this.clock(),
|
|
);
|
|
}
|
|
|
|
deletePermissionGrant(pluginId, permission, resource) {
|
|
this.db.prepare(`
|
|
DELETE FROM plugin_permission_grants
|
|
WHERE plugin_id = ? AND permission = ? AND resource = ?
|
|
`).run(pluginId, permission, resource);
|
|
}
|
|
|
|
deleteAllPermissionGrants(pluginId) {
|
|
this.db.prepare("DELETE FROM plugin_permission_grants WHERE plugin_id = ?").run(pluginId);
|
|
}
|
|
|
|
getSecretByKey(pluginId, key) {
|
|
const row = this.db.prepare(`
|
|
SELECT plugin_id, key, secret_ref, ciphertext, created_at, updated_at
|
|
FROM plugin_secrets WHERE plugin_id = ? AND key = ?
|
|
`).get(pluginId, key);
|
|
return row ? this.#mapSecret(row) : null;
|
|
}
|
|
|
|
getSecretByRef(pluginId, secretRef) {
|
|
const row = this.db.prepare(`
|
|
SELECT plugin_id, key, secret_ref, ciphertext, created_at, updated_at
|
|
FROM plugin_secrets WHERE plugin_id = ? AND secret_ref = ?
|
|
`).get(pluginId, secretRef);
|
|
return row ? this.#mapSecret(row) : null;
|
|
}
|
|
|
|
#mapSecret(row) {
|
|
return {
|
|
pluginId: row.plugin_id,
|
|
key: row.key,
|
|
secretRef: row.secret_ref,
|
|
ciphertext: Buffer.from(row.ciphertext),
|
|
createdAt: Number(row.created_at),
|
|
updatedAt: Number(row.updated_at),
|
|
};
|
|
}
|
|
|
|
upsertSecret(record) {
|
|
const now = this.clock();
|
|
this.db.prepare(`
|
|
INSERT INTO plugin_secrets(
|
|
plugin_id, key, secret_ref, ciphertext, created_at, updated_at
|
|
) VALUES (?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(plugin_id, key) DO UPDATE SET
|
|
secret_ref = excluded.secret_ref,
|
|
ciphertext = excluded.ciphertext,
|
|
updated_at = excluded.updated_at
|
|
`).run(
|
|
record.pluginId,
|
|
record.key,
|
|
record.secretRef,
|
|
record.ciphertext,
|
|
now,
|
|
now,
|
|
);
|
|
}
|
|
|
|
deleteSecret(pluginId, key) {
|
|
this.db.prepare("DELETE FROM plugin_secrets WHERE plugin_id = ? AND key = ?")
|
|
.run(pluginId, key);
|
|
}
|
|
|
|
/** Delete all secrets whose key equals prefix or starts with `${prefix}:`. */
|
|
deleteSecretsByKeyPrefix(pluginId, prefix) {
|
|
const result = this.db.prepare(`
|
|
DELETE FROM plugin_secrets
|
|
WHERE plugin_id = ?
|
|
AND (key = ? OR key LIKE ? ESCAPE '\\')
|
|
`).run(
|
|
pluginId,
|
|
prefix,
|
|
`${String(prefix).replace(/[%_\\]/g, (ch) => `\\${ch}`)}:%`,
|
|
);
|
|
return Number(result?.changes) || 0;
|
|
}
|
|
|
|
recordSecurityAudit(pluginId, event, details) {
|
|
let detailsJson = JSON.stringify(details ?? {});
|
|
const originalBytes = Buffer.byteLength(detailsJson);
|
|
if (originalBytes > MAX_SECURITY_AUDIT_DETAILS_BYTES) {
|
|
detailsJson = JSON.stringify({
|
|
truncated: true,
|
|
originalBytes,
|
|
sha256: createHash("sha256").update(detailsJson).digest("hex"),
|
|
});
|
|
}
|
|
this.transaction(() => {
|
|
this.db.prepare(`
|
|
INSERT INTO plugin_security_audit(plugin_id, event, details_json, created_at)
|
|
VALUES (?, ?, ?, ?)
|
|
`).run(pluginId, event, detailsJson, this.clock());
|
|
this.db.prepare(`
|
|
DELETE FROM plugin_security_audit
|
|
WHERE plugin_id = ? AND id NOT IN (
|
|
SELECT id FROM plugin_security_audit
|
|
WHERE plugin_id = ? ORDER BY id DESC LIMIT ${MAX_SECURITY_AUDIT_RECORDS_PER_PLUGIN}
|
|
)
|
|
`).run(pluginId, pluginId);
|
|
});
|
|
}
|
|
|
|
listSecurityAudit(pluginId, limit = 100) {
|
|
const requestedLimit = Number(limit);
|
|
const normalizedLimit = Math.max(1, Math.min(
|
|
1_000,
|
|
Number.isFinite(requestedLimit) ? Math.trunc(requestedLimit) : 100,
|
|
));
|
|
return this.db.prepare(`
|
|
SELECT event, details_json, created_at
|
|
FROM plugin_security_audit
|
|
WHERE plugin_id = ? ORDER BY id DESC LIMIT ?
|
|
`).all(pluginId, normalizedLimit).map((row) => ({
|
|
event: row.event,
|
|
details: parseJson(row.details_json, "security audit entry"),
|
|
createdAt: Number(row.created_at),
|
|
}));
|
|
}
|
|
|
|
close() {
|
|
this.db.close();
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
MAX_SECURITY_AUDIT_DETAILS_BYTES,
|
|
MAX_SECURITY_AUDIT_RECORDS_PER_PLUGIN,
|
|
PluginDatabase,
|
|
REQUIRED_SCHEMA_COLUMNS,
|
|
SCHEMA_VERSION,
|
|
};
|