Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): never inject sensitive-type vault records; gate reverse-lookup on table existence

Adam Eivyb9074ee261d3
diff --git a/server/services/privacyOrgs.crosslink.test.js b/server/services/privacyOrgs.crosslink.test.js
index db65adfd8a..1276081db6 100644
--- a/server/services/privacyOrgs.crosslink.test.js
+++ b/server/services/privacyOrgs.crosslink.test.js
@@ -5,27 +5,31 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
-const query = vi.fn();
-vi.mock('../lib/db.js', () => ({
- query: (...args) => query(...args),
- withTransaction: vi.fn(),
-}));
+vi.mock('../lib/db.js', () => ({ query: vi.fn(), withTransaction: vi.fn() }));
+const { query } = await import('../lib/db.js');
const { getOrgsBySocialAccounts } = await import('./privacyOrgs.js');
+// Route by SQL: the existence gate runs first, then the select.
+function routeQueries({ orgsTable = 'privacy_orgs', rows = [] } = {}) {
+ query.mockImplementation((sql) => {
+ if (sql?.includes('to_regclass')) return Promise.resolve({ rows: [{ orgs: orgsTable }] });
+ return Promise.resolve({ rows });
+ });
+}
+
beforeEach(() => query.mockReset());
describe('getOrgsBySocialAccounts', () => {
it('maps only orgs that reference a social account, keyed for the twin', async () => {
- query.mockResolvedValue({
- rows: [
- { id: 'org-1', name: 'GitHub', social_account_id: 'acct-1' },
- { id: 'org-2', name: 'Acme', social_account_id: 'acct-2' },
- ],
- });
+ routeQueries({ rows: [
+ { id: 'org-1', name: 'GitHub', social_account_id: 'acct-1' },
+ { id: 'org-2', name: 'Acme', social_account_id: 'acct-2' },
+ ] });
const result = await getOrgsBySocialAccounts();
// Filters to non-null social_account_id in SQL.
- expect(query.mock.calls[0][0]).toContain('social_account_id IS NOT NULL');
+ const selectCall = query.mock.calls.find(([sql]) => sql?.includes('WHERE social_account_id IS NOT NULL'));
+ expect(selectCall).toBeDefined();
expect(result).toEqual([
{ socialAccountId: 'acct-1', orgId: 'org-1', orgName: 'GitHub' },
{ socialAccountId: 'acct-2', orgId: 'org-2', orgName: 'Acme' },
@@ -33,7 +37,14 @@ describe('getOrgsBySocialAccounts', () => {
});
it('returns an empty array when no orgs are linked', async () => {
- query.mockResolvedValue({ rows: [] });
+ routeQueries({ rows: [] });
+ expect(await getOrgsBySocialAccounts()).toEqual([]);
+ });
+
+ it('degrades to an empty array when the privacy_orgs table is absent', async () => {
+ routeQueries({ orgsTable: null });
expect(await getOrgsBySocialAccounts()).toEqual([]);
+ // Never runs the select when the table doesn't exist.
+ expect(query.mock.calls.some(([sql]) => sql?.includes('WHERE social_account_id IS NOT NULL'))).toBe(false);
});
});
diff --git a/server/services/privacyOrgs.js b/server/services/privacyOrgs.js
index e2b08d456f..34fa4d7e9e 100644
--- a/server/services/privacyOrgs.js
+++ b/server/services/privacyOrgs.js
@@ -239,6 +239,11 @@ export async function setHoldingsStatus(vaultRecordId, fromStatus, toStatus) {
* Returns `[{ socialAccountId, orgId, orgName }]`.
*/
export async function getOrgsBySocialAccounts() {
+ // Degrade gracefully to an empty index when Privacy Center isn't provisioned
+ // yet (fresh install pre-migration) — the twin's Accounts tab calls this on
+ // load, so a missing table must yield no badges rather than a 500.
+ const { rows: tableRows } = await query(`SELECT to_regclass('public.privacy_orgs') AS orgs`);
+ if (!tableRows[0]?.orgs) return [];
const { rows } = await query(
`SELECT id, name, social_account_id
FROM privacy_orgs
diff --git a/server/services/privacyTwinContext.js b/server/services/privacyTwinContext.js
index ae332847a1..751710d671 100644
--- a/server/services/privacyTwinContext.js
+++ b/server/services/privacyTwinContext.js
@@ -22,6 +22,7 @@
*/
import { query } from '../lib/db.js';
+import { PRIVACY_SENSITIVE_TYPES } from '../lib/privacyValidation.js';
// vaultCrypto is imported lazily (inside getPrivacyTwinContext) so merely
// importing this module — which digital-twin-context.js does at top level —
// doesn't eagerly evaluate vaultCrypto's install-root .env path resolution.
@@ -81,11 +82,17 @@ export async function getPrivacyTwinContext() {
const sections = [];
// Per-field gate: only records the user explicitly flagged for the twin.
+ // Sensitive types (SSN, passport, driver's license, financial account) are
+ // NEVER injected into a prompt even if flagged — the same never-disclose
+ // posture as broker scans (use_for_scans), enforced here so an already-stored
+ // flag on a sensitive record can't leak highly-sensitive IDs into an LLM.
const { rows: records } = await query(
`SELECT type, label, status, value_enc
FROM privacy_vault_records
WHERE share_with_twin = true
+ AND type <> ALL($1::text[])
ORDER BY type, label`,
+ [PRIVACY_SENSITIVE_TYPES],
);
if (records.length > 0) {
const { decryptValue } = await import('../lib/vaultCrypto.js');
diff --git a/server/services/privacyTwinContext.test.js b/server/services/privacyTwinContext.test.js
index 10c3b74e53..0613dacf53 100644
--- a/server/services/privacyTwinContext.test.js
+++ b/server/services/privacyTwinContext.test.js
@@ -20,7 +20,7 @@ const { decryptValue } = await import('../lib/vaultCrypto.js');
function routeQueries(map) {
query.mockImplementation((sql) => {
for (const [needle, rows] of map) {
- if (sql.includes(needle)) return Promise.resolve({ rows });
+ if (sql?.includes(needle)) return Promise.resolve({ rows });
}
return Promise.resolve({ rows: [] });
});
@@ -50,9 +50,12 @@ describe('getPrivacyTwinContext', () => {
]],
]);
const out = await getPrivacyTwinContext();
- // Per-field gate is enforced in SQL.
+ // Per-field gate + sensitive-type exclusion are enforced in SQL.
const recordCall = query.mock.calls.find(([sql]) => sql.includes('FROM privacy_vault_records'));
expect(recordCall[0]).toContain('share_with_twin = true');
+ expect(recordCall[0]).toContain('type <> ALL');
+ // Sensitive types (ssn/passport/drivers_license/financial_account) never inject.
+ expect(recordCall[1][0]).toEqual(expect.arrayContaining(['ssn', 'passport', 'drivers_license', 'financial_account']));
// Decrypted values present; masked/ciphertext never leaks.
expect(out).toContain('Ada Lovelace');
expect(out).toContain('address (previous): 1 Old St');