mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
* fix(plugins): backfill sync provider bindings after schema upgrade Promote legacy sync-provider-map secrets into the host binding table on open, and infer plugin ownership from sync-credential rows so disconnect still cleans credentials when bindings were never written. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(plugins): infer sync provider owners only via pluginId. namespace Exact pluginId===providerId is not ownership for contribution provider ids; prefer the longest pluginId. prefix when backfilling disconnect cleanup. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(plugins): stop inferring sync owner from credential prefixes Parent plugin ids can namespace-prefix another plugin's provider id. Guessing ownership from sync-credential rows would let disconnect wipe the wrong secrets. Keep host bindings and legacy map backfill as the only ownership sources when no live contribution is present. * fix(plugins): do not overwrite existing sync provider bindings on backfill Legacy sync-provider-map rows from a parent plugin must not steal a provider that already has a host binding. Skip upsert when a binding exists; still delete the stale map secret. * fix(plugins): pick longest owner when legacy map rows conflict Group sync-provider-map rows by provider before binding so a shorter parent plugin cannot win just because SQLite returned it first. Still preserve existing host bindings and delete all leftover map secrets. * fix(plugins): never delete secrets during sync-provider-map backfill The map key prefix is not reserved for host internals; plugins may store ordinary secrets under the same shape. Only seed missing host bindings from candidate rows and leave plugin_secrets untouched. * fix(plugins): seed sync bindings from live provider metadata Schema-2 installs only stored sync-credential* rows. After upgrade, seed missing provider bindings from the host contribution catalog when those credentials exist, instead of guessing from secret key prefixes. * fix(plugins): seed sync bindings from installed manifests, not only enabled listProviders skips disabled/quarantined plugins. Walk installed plugin manifests at host init so schema-2 credential rows under disabled plugins still get provider bindings for disconnect cleanup. * fix(plugins): seed sync bindings from all installed version manifests Active-only listPlugins() misses providers removed/renamed on upgrade while credentials remain under the plugin. Walk every plugin_versions manifest so disconnect can still resolve ownership after schema upgrade. * fix(plugins): only auto-bind single-provider plugins from credentials Plugin-scoped sync-credential* rows are shared across providers. Binding every historical provider would let disconnect of a stale one wipe the active provider's secrets. Seed bindings only when exactly one sync provider is known for that plugin. * fix: address Codex review on PR #2752 * fix(plugins): stop resurrecting sync bindings after unbind Consume promoted sync-provider-map markers and leave an unbound tombstone on disconnect so constructor backfill cannot re-bind a provider the user already disconnected. * fix(plugins): prefer credential-backed owners for legacy map backfill Longest sync-provider-map namespace alone could bind a nested plugin over a parent that still owns sync-credential* secrets. Only promote candidates with credentials; pick the longest among those, and skip when none or ambiguous. * fix(plugins): skip ambiguous multi-credential legacy map backfill Only auto-bind when exactly one credential-backed map candidate exists. When parent and nested plugins both hold credentials for the same provider, longest pluginId is not a reliable owner signal — leave unbound. * fix(plugins): skip cross-plugin provider conflicts in live backfill When multiple credential-backed plugins declare the same providerId via nested namespaces, do not bind the first writer. A later missing-provider disconnect would otherwise delete the wrong sync-credential rows. * fix(plugins): use full installed catalog for enable-time sync backfill Enable-time binding must see the same active+inactive manifest catalog as startup so cross-plugin nested provider ambiguity is not missed when only the newly enabled plugin's live providers would be passed. * fix(plugins): count non-credential manifest claims in backfill conflicts Live nested plugins without credentials yet must still block binding a credential-backed legacy parent to the same providerId, or disconnect can prefer the live owner while the stale binding remains. * fix(plugins): skip legacy map promote on installed-manifest conflicts Constructor-time map backfill must not bind a sole credential-backed parent when another installed package also declares the same sync provider, or later disconnect cannot clear the stale binding. * fix(plugins): require single-provider maps for legacy-map promotion A plugin with one sync-credential row and maps for multiple historical providers must not bind all of them — disconnecting a stale provider would delete the live credentials via the shared prefix. * fix(plugins): count installed manifest providers in legacy-map guard A sole stale map row must not promote when the credential owner's live manifest also declares another sync provider under the same credentials. * fix(plugins): backfill sync bindings before startup activation Seed installed-manifest bindings before contributionService.initialize so an early syncDeleteSecrets cannot miss ownership and leave orphan credentials. * fix(plugins): seed sync bindings at host construction Run installed-manifest backfill when the plugin host is created, before async manager.initialize, so early syncDeleteSecrets can resolve ownership. * fix(plugins): re-seed sync bindings after package store recovery PackageStore.recover can add installed versions after the constructor seed; re-run the catalog backfill when package initialize finishes. * fix(plugins): await manager init before syncDeleteSecrets ownership Block secret cleanup until package recovery and binding seed finish so early disconnect cannot miss retained sync-credential rows. * fix(plugins): re-run legacy-map backfill after package recovery After PackageStore.recover repopulates versions, re-run legacy-map promote so recovered manifests participate in ownership conflict checks. * fix(plugins): defer legacy-map backfill until after package recovery Constructor no longer promotes sole map candidates before recovered manifests are visible. hostService re-runs backfill after package init. * fix(plugins): defer live binding seed until after package recovery Construction-time live-provider backfill can bind a stale owner against an incomplete catalog before PackageStore.recover() restores plugin_versions. Seed only after recovery; syncDeleteSecrets already awaits manager init. --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
349 lines
13 KiB
JavaScript
349 lines
13 KiB
JavaScript
"use strict";
|
|
|
|
const { randomBytes } = require("node:crypto");
|
|
|
|
const { PluginRpcError, RPC_ERRORS } = require("./rpcRouter.cjs");
|
|
|
|
const MAX_SECRET_BYTES = 64 * 1024;
|
|
|
|
function assertSecretKey(key) {
|
|
if (typeof key !== "string" || key.length < 1 || key.length > 256 || key.includes("\0")) {
|
|
throw new PluginRpcError(RPC_ERRORS.invalidArgument, "Plugin secret key is invalid");
|
|
}
|
|
return key;
|
|
}
|
|
|
|
function assertSecretRef(secret) {
|
|
if (
|
|
!secret
|
|
|| typeof secret !== "object"
|
|
|| Array.isArray(secret)
|
|
|| secret.kind !== "secret"
|
|
|| typeof secret.id !== "string"
|
|
|| secret.id.length < 16
|
|
|| secret.id.length > 256
|
|
|| typeof secret.key !== "string"
|
|
|| secret.key.length < 1
|
|
|| secret.key.length > 256
|
|
|| secret.key.includes("\0")
|
|
) throw new PluginRpcError(RPC_ERRORS.invalidArgument, "Plugin secret reference is invalid");
|
|
return { id: secret.id, key: secret.key };
|
|
}
|
|
|
|
class PluginSecretStore {
|
|
/** @type {Map<string, { value: string, secretRef: string }>} */
|
|
#overwriteStash = new Map();
|
|
|
|
constructor(options) {
|
|
this.database = options.database;
|
|
this.safeStorage = options.safeStorage ?? null;
|
|
this.randomBytes = options.randomBytes ?? randomBytes;
|
|
}
|
|
|
|
#stashKey(pluginId, key) {
|
|
return `${pluginId}\0${key}`;
|
|
}
|
|
|
|
#assertAvailable() {
|
|
const backend = this.safeStorage?.getSelectedStorageBackend?.();
|
|
if (
|
|
!this.safeStorage?.isEncryptionAvailable?.()
|
|
|| backend === "basic_text"
|
|
|| typeof this.safeStorage.encryptString !== "function"
|
|
|| typeof this.safeStorage.decryptString !== "function"
|
|
) {
|
|
throw new PluginRpcError(
|
|
RPC_ERRORS.unavailable,
|
|
"Secure OS-backed encryption is unavailable for plugin secrets",
|
|
);
|
|
}
|
|
}
|
|
|
|
getReference(pluginId, key) {
|
|
assertSecretKey(key);
|
|
const record = this.database.getSecretByKey(pluginId, key);
|
|
return record ? Object.freeze({ kind: "secret", id: record.secretRef, key: record.key }) : undefined;
|
|
}
|
|
|
|
getRecordByReference(pluginId, secret) {
|
|
const reference = assertSecretRef(secret);
|
|
const record = this.database.getSecretByRef(pluginId, reference.id);
|
|
if (!record || record.key !== reference.key) {
|
|
throw new PluginRpcError(RPC_ERRORS.notFound, "Plugin secret reference was not found");
|
|
}
|
|
return record;
|
|
}
|
|
|
|
set(pluginId, key, value, options = {}) {
|
|
this.#assertAvailable();
|
|
assertSecretKey(key);
|
|
if (typeof value !== "string" || Buffer.byteLength(value, "utf8") > MAX_SECRET_BYTES) {
|
|
throw new PluginRpcError(RPC_ERRORS.invalidArgument, "Plugin secret value is invalid or too large");
|
|
}
|
|
const stashPrevious = options.stashPrevious === true;
|
|
const stashKey = this.#stashKey(pluginId, key);
|
|
let stashed = false;
|
|
if (stashPrevious && !this.#overwriteStash.has(stashKey)) {
|
|
// Keep an existing stash (e.g. after a failed restore) so a later retry
|
|
// still recovers the original SecretRef, not a rejected replacement.
|
|
const existing = this.getReference(pluginId, key);
|
|
if (existing) {
|
|
try {
|
|
this.#overwriteStash.set(stashKey, {
|
|
value: this.resolve(pluginId, existing),
|
|
// Keep the prior SecretRef id so saved provider connections still resolve.
|
|
secretRef: existing.id,
|
|
});
|
|
stashed = true;
|
|
} catch {
|
|
/* keep going; restore may be unavailable for this key */
|
|
}
|
|
}
|
|
}
|
|
try {
|
|
const secretRef = this.randomBytes(24).toString("base64url");
|
|
const ciphertext = this.safeStorage.encryptString(value);
|
|
if (!Buffer.isBuffer(ciphertext) || ciphertext.byteLength < 1) {
|
|
throw new PluginRpcError(RPC_ERRORS.unavailable, "OS-backed plugin secret encryption failed");
|
|
}
|
|
this.database.upsertSecret({ pluginId, key, secretRef, ciphertext });
|
|
return Object.freeze({ kind: "secret", id: secretRef, key });
|
|
} catch (error) {
|
|
// Failed replacement must not leave prior plaintext stranded in memory.
|
|
if (stashed) this.#overwriteStash.delete(stashKey);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Restore the plaintext (and SecretRef id) stashed by the last overwrite.
|
|
* Returns true when a stashed value was written back.
|
|
*/
|
|
restoreOverwrite(pluginId, key) {
|
|
this.#assertAvailable();
|
|
assertSecretKey(key);
|
|
const stashKey = this.#stashKey(pluginId, key);
|
|
const previous = this.#overwriteStash.get(stashKey);
|
|
if (!previous || typeof previous.value !== "string" || typeof previous.secretRef !== "string") {
|
|
return false;
|
|
}
|
|
const ciphertext = this.safeStorage.encryptString(previous.value);
|
|
if (!Buffer.isBuffer(ciphertext) || ciphertext.byteLength < 1) {
|
|
throw new PluginRpcError(RPC_ERRORS.unavailable, "OS-backed plugin secret encryption failed");
|
|
}
|
|
this.database.upsertSecret({
|
|
pluginId,
|
|
key,
|
|
secretRef: previous.secretRef,
|
|
ciphertext,
|
|
});
|
|
// Drop the stash only after the prior value is durably written so a failed
|
|
// encrypt/upsert can still be retried.
|
|
this.#overwriteStash.delete(stashKey);
|
|
return true;
|
|
}
|
|
|
|
clearOverwriteStash(pluginId, key) {
|
|
assertSecretKey(key);
|
|
this.#overwriteStash.delete(this.#stashKey(pluginId, key));
|
|
}
|
|
|
|
delete(pluginId, key) {
|
|
assertSecretKey(key);
|
|
this.#overwriteStash.delete(this.#stashKey(pluginId, key));
|
|
this.database.deleteSecret(pluginId, key);
|
|
}
|
|
|
|
deleteByKeyPrefix(pluginId, prefix) {
|
|
assertSecretKey(prefix);
|
|
let deleted = 0;
|
|
if (typeof this.database.deleteSecretsByKeyPrefix !== "function") {
|
|
this.delete(pluginId, prefix);
|
|
deleted = 1;
|
|
} else {
|
|
deleted = this.database.deleteSecretsByKeyPrefix(pluginId, prefix);
|
|
}
|
|
const pluginPrefix = `${pluginId}\0`;
|
|
for (const stashKey of [...this.#overwriteStash.keys()]) {
|
|
if (!stashKey.startsWith(pluginPrefix)) continue;
|
|
const secretKey = stashKey.slice(pluginPrefix.length);
|
|
if (secretKey === prefix || secretKey.startsWith(`${prefix}:`)) {
|
|
this.#overwriteStash.delete(stashKey);
|
|
}
|
|
}
|
|
return deleted;
|
|
}
|
|
|
|
/**
|
|
* Durable providerId → pluginId binding so disconnect can wipe sync secrets
|
|
* after the contribution disappears (disabled/uninstalled plugin).
|
|
* Stored in a host-owned table, not plugin-writable secrets.
|
|
* Overwrites any prior empty-plugin_id unbind tombstone.
|
|
*/
|
|
bindSyncProviderPlugin(pluginId, providerId) {
|
|
if (typeof pluginId !== "string" || pluginId.length < 1) {
|
|
throw new TypeError("Plugin id is invalid");
|
|
}
|
|
assertSecretKey(providerId);
|
|
// Contribution provider ids live under the owning plugin namespace
|
|
// (`pluginId.`…); they are never equal to the plugin id itself.
|
|
if (!providerId.startsWith(`${pluginId}.`)) {
|
|
throw new TypeError("Sync provider id is outside the plugin namespace");
|
|
}
|
|
if (typeof this.database.upsertSyncProviderBinding !== "function") {
|
|
throw new Error("Plugin sync provider binding storage is unavailable");
|
|
}
|
|
this.database.upsertSyncProviderBinding(providerId, pluginId);
|
|
}
|
|
|
|
resolveSyncProviderPlugin(providerId) {
|
|
assertSecretKey(providerId);
|
|
if (typeof this.database.getSyncProviderBinding === "function") {
|
|
const row = this.database.getSyncProviderBinding(providerId);
|
|
const pluginId = row?.pluginId;
|
|
if (
|
|
typeof pluginId === "string"
|
|
&& pluginId.length > 0
|
|
&& providerId.startsWith(`${pluginId}.`)
|
|
) {
|
|
return pluginId;
|
|
}
|
|
}
|
|
// Do not infer ownership from sync-credential* key prefixes: a parent
|
|
// plugin id may namespace-prefix another plugin's provider and would cause
|
|
// disconnect to delete the wrong secrets. Pre-binding installs recover via
|
|
// (1) backfillSyncProviderBindingsFromLegacySecrets for intermediate map
|
|
// rows, (2) backfillSyncProviderBindingsFromLiveProviders using host
|
|
// contribution metadata at startup, or (3) the next successful put.
|
|
return undefined;
|
|
}
|
|
|
|
/**
|
|
* Seed missing provider→plugin bindings from contribution metadata.
|
|
*
|
|
* Real schema-2 installs only stored plugin-scoped `sync-credential*` rows
|
|
* (no per-provider ownership). After upgrade, seed a binding only when a
|
|
* plugin with credentials has exactly one sync provider id across the
|
|
* catalog: binding every historical/active provider would let disconnecting
|
|
* a stale provider wipe the shared credential prefix used by the live one.
|
|
* Multi-provider plugins wait for the next successful put (or map backfill).
|
|
*
|
|
* @param {Array<{ pluginId?: string, id?: string, provider?: { id?: string } }>} providers
|
|
* @returns {number} number of newly written bindings
|
|
*/
|
|
backfillSyncProviderBindingsFromLiveProviders(providers) {
|
|
if (!Array.isArray(providers)) return 0;
|
|
if (typeof this.database.listPluginIdsWithSecretKeyPrefix !== "function") return 0;
|
|
const credentialOwners = new Set(
|
|
this.database.listPluginIdsWithSecretKeyPrefix("sync-credential")
|
|
.filter((id) => typeof id === "string" && id.length > 0),
|
|
);
|
|
if (credentialOwners.size === 0) return 0;
|
|
/** @type {Map<string, Set<string>>} pluginId -> provider ids it declares */
|
|
const providersByPlugin = new Map();
|
|
/** @type {Map<string, Set<string>>} providerId -> plugins claiming it */
|
|
const pluginsByProvider = new Map();
|
|
for (const entry of providers) {
|
|
const pluginId = entry?.pluginId;
|
|
const providerId = entry?.provider?.id ?? entry?.id;
|
|
if (typeof pluginId !== "string" || pluginId.length < 1) continue;
|
|
if (typeof providerId !== "string" || providerId.length < 1) continue;
|
|
if (!providerId.startsWith(`${pluginId}.`)) continue;
|
|
// Count every manifest claim for conflict detection — including live
|
|
// plugins that have not stored credentials yet. Binding only a
|
|
// credential-backed legacy parent would orphan credentials when a later
|
|
// disconnect prefers the live nested owner (Codex P2 on 8d64ea20).
|
|
const claimants = pluginsByProvider.get(providerId) || new Set();
|
|
claimants.add(pluginId);
|
|
pluginsByProvider.set(providerId, claimants);
|
|
// Only credential-backed plugins are eligible to become the binding owner.
|
|
if (!credentialOwners.has(pluginId)) continue;
|
|
const set = providersByPlugin.get(pluginId) || new Set();
|
|
set.add(providerId);
|
|
providersByPlugin.set(pluginId, set);
|
|
}
|
|
let promoted = 0;
|
|
for (const [pluginId, providerIds] of providersByPlugin) {
|
|
if (providerIds.size !== 1) continue;
|
|
const providerId = [...providerIds][0];
|
|
// Two credential-backed plugins can both declare the same provider via
|
|
// nested namespaces (legacy disabled parent + live child). Binding the
|
|
// first writer would make a later missing-provider disconnect delete the
|
|
// wrong sync-credential* rows. Skip any cross-plugin claim conflict.
|
|
const claimants = pluginsByProvider.get(providerId);
|
|
if (!claimants || claimants.size !== 1) continue;
|
|
// Skip active binds and explicit-unbind tombstones alike.
|
|
if (typeof this.database.getSyncProviderBinding === "function"
|
|
&& this.database.getSyncProviderBinding(providerId)) {
|
|
continue;
|
|
}
|
|
try {
|
|
this.bindSyncProviderPlugin(pluginId, providerId);
|
|
promoted += 1;
|
|
} catch {
|
|
/* invalid contribution ids stay unbound */
|
|
}
|
|
}
|
|
return promoted;
|
|
}
|
|
|
|
unbindSyncProviderPlugin(pluginId, providerId) {
|
|
assertSecretKey(providerId);
|
|
if (typeof this.database.upsertSyncProviderBinding !== "function") return;
|
|
const existing = typeof this.database.getSyncProviderBinding === "function"
|
|
? this.database.getSyncProviderBinding(providerId)
|
|
: null;
|
|
// Do not steal another plugin's active binding. Empty plugin_id is an
|
|
// unbind tombstone and may be refreshed idempotently.
|
|
if (
|
|
existing
|
|
&& typeof existing.pluginId === "string"
|
|
&& existing.pluginId.length > 0
|
|
&& existing.pluginId !== pluginId
|
|
) {
|
|
return;
|
|
}
|
|
// Consume leftover intermediate-build map markers so they cannot re-seed
|
|
// a binding if the tombstone is ever cleared. Delete under this plugin and
|
|
// any residual map rows for the same provider across plugins.
|
|
const mapKey = `sync-provider-map:${providerId}`;
|
|
if (typeof this.database.deleteSecret === "function") {
|
|
try {
|
|
this.database.deleteSecret(pluginId, mapKey);
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
if (typeof this.database.findSecretsByKey === "function" && typeof this.database.deleteSecret === "function") {
|
|
for (const row of this.database.findSecretsByKey(mapKey) || []) {
|
|
if (typeof row?.pluginId !== "string" || typeof row?.key !== "string") continue;
|
|
try {
|
|
this.database.deleteSecret(row.pluginId, row.key);
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
}
|
|
// Tombstone with empty plugin_id so leftover sync-provider-map:* secret
|
|
// rows cannot re-promote this provider on the next database open.
|
|
this.database.upsertSyncProviderBinding(providerId, "");
|
|
}
|
|
|
|
resolve(pluginId, secret) {
|
|
this.#assertAvailable();
|
|
const record = this.getRecordByReference(pluginId, secret);
|
|
try {
|
|
return this.safeStorage.decryptString(record.ciphertext);
|
|
} catch {
|
|
throw new PluginRpcError(RPC_ERRORS.dataLoss, "Plugin secret could not be decrypted");
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
MAX_SECRET_BYTES,
|
|
PluginSecretStore,
|
|
assertSecretKey,
|
|
assertSecretRef,
|
|
};
|