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

fix: mask Moltworld agentId in account reads and keep it out of logs (#12496)

[._.]/ Adam Eivy86b73e45fad9
diff --git a/server/integrations/moltworld/api.js b/server/integrations/moltworld/api.js
index 2c9ca093e3..c90e5084cf 100644
--- a/server/integrations/moltworld/api.js
+++ b/server/integrations/moltworld/api.js
@@ -25,6 +25,13 @@ function inferActionFromEndpoint(endpoint, method) {
return null;
}
+/**
+ * Endpoint for log lines: query strings carry agentId (auth), so drop them
+ */
+function logPath(endpoint) {
+ return endpoint.split('?')[0];
+}
+
/**
* Make an API request to Moltworld
*/
@@ -38,7 +45,7 @@ async function request(endpoint, options = {}) {
...options
};
- console.log(`🌍 Moltworld API: ${options.method || 'GET'} ${endpoint}`);
+ console.log(`🌍 Moltworld API: ${options.method || 'GET'} ${logPath(endpoint)}`);
const fetchResult = await fetchWithTimeout(url, config, MOLTWORLD_TIMEOUT_MS).then(r => ({ ok: true, response: r }), e => ({ ok: false, error: e }));
@@ -58,7 +65,7 @@ async function request(endpoint, options = {}) {
if (response.status !== 404) {
console.error(`❌ Moltworld API error: ${response.status} ${message}`);
} else {
- console.log(`🌍 Moltworld API: 404 ${endpoint}`);
+ console.log(`🌍 Moltworld API: 404 ${logPath(endpoint)}`);
}
// Sync local rate limit state when platform enforces a cooldown
if (response.status === 429) {
@@ -111,7 +118,7 @@ export async function register(name, appearance = {}) {
// Normalize: API may return { agent: { id } } or { agentId }
const agentId = result?.agentId || result?.agent?.id || result?.id;
const apiKey = result?.apiKey || result?.api_key || agentId;
- console.log(`🆕 Moltworld: Registered agent "${name}" (agentId=${agentId})`);
+ console.log(`🆕 Moltworld: Registered agent "${name}"`);
return { ...result, agentId, apiKey };
}
diff --git a/server/integrations/moltworld/api.test.js b/server/integrations/moltworld/api.test.js
new file mode 100644
index 0000000000..7797e2ec2e
--- /dev/null
+++ b/server/integrations/moltworld/api.test.js
@@ -0,0 +1,57 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+
+const fetchMock = vi.hoisted(() => vi.fn());
+vi.mock('../../lib/fetchWithTimeout.js', () => ({ fetchWithTimeout: fetchMock }));
+vi.mock('./rateLimits.js', () => ({ checkRateLimit: vi.fn(), recordAction: vi.fn(), syncFromExternal: vi.fn() }));
+
+import * as moltworld from './api.js';
+
+const SECRET = 'mw-secret-agent-9f8e7d';
+const jsonResponse = (body, status = 200) => ({
+ ok: status >= 200 && status < 300,
+ status,
+ json: async () => body,
+});
+
+// Every console line the client writes, flattened for substring checks.
+const loggedText = (spies) => spies.flatMap(s => s.mock.calls).map(args => args.join(' ')).join('\n');
+
+describe('moltworld api logging', () => {
+ let logSpy;
+ let errorSpy;
+
+ beforeEach(() => {
+ fetchMock.mockReset();
+ logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
+ errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
+ });
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it('never writes the agentId to logs, even when it rides in a query string', async () => {
+ fetchMock.mockResolvedValue(jsonResponse({ agent: { id: SECRET } }));
+ await moltworld.getProfile(SECRET);
+
+ const text = loggedText([logSpy, errorSpy]);
+ expect(text).toContain('/api/agents/profile');
+ expect(text).not.toContain(SECRET);
+ });
+
+ it('does not log the agentId for a 404 on a query-string endpoint', async () => {
+ fetchMock.mockResolvedValue(jsonResponse({ error: 'not found' }, 404));
+ await expect(moltworld.getBalance(SECRET)).rejects.toThrow();
+
+ const text = loggedText([logSpy, errorSpy]);
+ expect(text).toContain('/api/agents/balance');
+ expect(text).not.toContain(SECRET);
+ });
+
+ it('logs registration without the returned agentId', async () => {
+ fetchMock.mockResolvedValue(jsonResponse({ agentId: SECRET }));
+ const result = await moltworld.register('Example Agent');
+
+ expect(result.agentId).toBe(SECRET);
+ expect(loggedText([logSpy, errorSpy])).not.toContain(SECRET);
+ });
+});
diff --git a/server/services/platformAccounts.js b/server/services/platformAccounts.js
index e4778b7e68..242c03254c 100644
--- a/server/services/platformAccounts.js
+++ b/server/services/platformAccounts.js
@@ -15,6 +15,19 @@ const ACCOUNTS_FILE = join(PATHS.agentPersonalities, 'accounts.json');
const store = createCachedStore(ACCOUNTS_FILE, { accounts: {} }, { context: 'platformAccounts' });
const loadAccounts = store.load;
+/**
+ * Mask the secrets an account carries: the API key, and the Moltworld agentId
+ * (its auth credential). Only the last 4 characters of each survive.
+ */
+function maskAccountCredentials(credentials) {
+ const mask = (value) => (value ? '***' + value.slice(-4) : null);
+ return {
+ ...credentials,
+ apiKey: mask(credentials?.apiKey),
+ ...(credentials?.agentId ? { agentId: mask(credentials.agentId) } : {})
+ };
+}
+
// Event emitter for account changes
export const platformAccountEvents = new EventEmitter();
export const invalidateCache = store.invalidateCache;
@@ -31,11 +44,7 @@ export async function getAllAccounts() {
return Object.entries(data.accounts).map(([id, account]) => ({
id,
...account,
- // Mask the API key for security
- credentials: {
- ...account.credentials,
- apiKey: account.credentials?.apiKey ? '***' + account.credentials.apiKey.slice(-4) : null
- }
+ credentials: maskAccountCredentials(account.credentials)
}));
}
@@ -58,10 +67,7 @@ export async function getAccountById(id, maskCredentials = true) {
return {
id,
...account,
- credentials: maskCredentials ? {
- ...account.credentials,
- apiKey: account.credentials?.apiKey ? '***' + account.credentials.apiKey.slice(-4) : null
- } : account.credentials
+ credentials: maskCredentials ? maskAccountCredentials(account.credentials) : account.credentials
};
}
@@ -100,10 +106,7 @@ export async function createAccount(accountData) {
return {
id,
...account,
- credentials: {
- ...account.credentials,
- apiKey: '***' + account.credentials.apiKey.slice(-4)
- }
+ credentials: maskAccountCredentials(account.credentials)
};
}
diff --git a/server/services/platformAccounts.test.js b/server/services/platformAccounts.test.js
new file mode 100644
index 0000000000..4b09a29b70
--- /dev/null
+++ b/server/services/platformAccounts.test.js
@@ -0,0 +1,54 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+const state = vi.hoisted(() => ({ data: { accounts: {} } }));
+vi.mock('../lib/fileUtils.js', () => ({
+ PATHS: { agentPersonalities: '/unused' },
+ createCachedStore: () => ({
+ load: async () => state.data,
+ mutate: async (fn) => fn(state.data),
+ invalidateCache: () => {},
+ }),
+}));
+
+import * as platformAccounts from './platformAccounts.js';
+
+const MW_AGENT_ID = 'mw-agent-id-5a6b7c8d';
+const API_KEY = 'api-key-1234abcd';
+
+const seed = () => {
+ state.data = {
+ accounts: {
+ 'acct-mw': {
+ agentId: 'agent-1',
+ platform: 'moltworld',
+ credentials: { apiKey: API_KEY, username: 'example', agentId: MW_AGENT_ID },
+ status: 'active',
+ },
+ },
+ };
+};
+
+beforeEach(() => {
+ vi.spyOn(console, 'log').mockImplementation(() => {});
+ seed();
+});
+
+describe('platform account credential masking', () => {
+ it('masks the Moltworld agentId in list and detail reads', async () => {
+ const [listed] = await platformAccounts.getAllAccounts();
+ const detail = await platformAccounts.getAccountById('acct-mw');
+
+ for (const account of [listed, detail]) {
+ expect(account.credentials.agentId).toBe('***' + MW_AGENT_ID.slice(-4));
+ expect(account.credentials.apiKey).toBe('***' + API_KEY.slice(-4));
+ expect(JSON.stringify(account)).not.toContain(MW_AGENT_ID);
+ }
+ });
+
+ it('keeps the full credentials for internal platform calls', async () => {
+ const account = await platformAccounts.getAccountWithCredentials('acct-mw');
+
+ expect(account.credentials.agentId).toBe(MW_AGENT_ID);
+ expect(account.credentials.apiKey).toBe(API_KEY);
+ });
+});