File size: 9,296 Bytes
3d700dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { beforeEach, describe, expect, it, vi } from "vitest";
import SettingsService, {
  type SettingsApiResponse,
} from "#/api/settings-service/settings-service.api";
import { substituteRedactedMcpCredentials } from "./mcp-redacted-credentials";
import { REDACTED_MCP_SECRET_VALUE } from "#/utils/mcp-config";

describe("substituteRedactedMcpCredentials", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("preserves encrypted stdio env when the server is renamed", async () => {
    // Regression: renaming a stdio server left a redacted env value unchanged,
    // so the lookup by the new display name missed the stored entry and the
    // literal redaction placeholder overwrote the stored encrypted secret.
    vi.spyOn(SettingsService, "fetchSettingsFromApi").mockResolvedValue({
      agent_settings: {
        mcp_config: {
          "old-name": {
            command: "npx",
            env: { API_KEY: "gAAAAA-encrypted-api-key" },
          },
        },
      },
    } as unknown as SettingsApiResponse);

    const result = await substituteRedactedMcpCredentials({
      id: "old-name",
      type: "stdio",
      name: "new-name",
      command: "npx",
      env: { API_KEY: REDACTED_MCP_SECRET_VALUE },
    });

    expect(result.env).toEqual({ API_KEY: "gAAAAA-encrypted-api-key" });
    expect(result.name).toBe("new-name");
  });

  it("does not restore another server's secret when renamed onto an existing name", async () => {
    // Renaming "alpha" onto "beta"'s name must still resolve alpha's stored
    // entry by stable key, not beta's entry.
    vi.spyOn(SettingsService, "fetchSettingsFromApi").mockResolvedValue({
      agent_settings: {
        mcp_config: {
          alpha: { command: "npx", env: { TOKEN: "gAAAAA-alpha-token" } },
          beta: { command: "npx", env: { TOKEN: "gAAAAA-beta-token" } },
        },
      },
    } as unknown as SettingsApiResponse);

    const result = await substituteRedactedMcpCredentials({
      id: "alpha",
      type: "stdio",
      name: "beta",
      command: "npx",
      env: { TOKEN: REDACTED_MCP_SECRET_VALUE },
    });

    expect(result.env).toEqual({ TOKEN: "gAAAAA-alpha-token" });
  });

  it("leaves typed (non-redacted) env values untouched", async () => {
    vi.spyOn(SettingsService, "fetchSettingsFromApi").mockResolvedValue({
      agent_settings: {
        mcp_config: {
          "my-server": {
            command: "npx",
            env: { API_KEY: "gAAAAA-encrypted", REGION: "us-east-1" },
          },
        },
      },
    } as unknown as SettingsApiResponse);

    const result = await substituteRedactedMcpCredentials({
      id: "my-server",
      type: "stdio",
      name: "my-server",
      command: "npx",
      env: {
        API_KEY: REDACTED_MCP_SECRET_VALUE,
        REGION: "eu-west-1",
      },
    });

    expect(result.env).toEqual({
      API_KEY: "gAAAAA-encrypted",
      REGION: "eu-west-1",
    });
  });

  it("returns the server unchanged when no env value is redacted", async () => {
    const server = {
      id: "stdio-0",
      type: "stdio" as const,
      name: "my-server",
      command: "npx",
      env: { API_KEY: "plaintext" },
    };

    const result = await substituteRedactedMcpCredentials(server);

    expect(result).toBe(server);
    expect(SettingsService.fetchSettingsFromApi).not.toHaveBeenCalled();
  });

  it("keeps the placeholder when the stored stdio entry is missing", async () => {
    vi.spyOn(SettingsService, "fetchSettingsFromApi").mockResolvedValue({
      agent_settings: { mcp_config: {} },
    } as unknown as SettingsApiResponse);

    const result = await substituteRedactedMcpCredentials({
      id: "stdio-0",
      type: "stdio",
      name: "new-name",
      command: "npx",
      env: { API_KEY: REDACTED_MCP_SECRET_VALUE },
    });

    expect(result.env).toEqual({ API_KEY: REDACTED_MCP_SECRET_VALUE });
  });

  it("replaces redacted OAuth state with the encrypted stored subtree", async () => {
    vi.spyOn(SettingsService, "fetchSettingsFromApi").mockResolvedValue({
      agent_settings: {
        mcp_config: {
          "superhuman-mail": {
            url: "https://mcp.mail.superhuman.com/mcp",
            auth: {
              strategy: "oauth2",
              state: {
                tokens: {
                  access_token: "gAAAAA-encrypted-access-token",
                  refresh_token: "gAAAAA-encrypted-refresh-token",
                },
                client_info: {
                  client_id: "superhuman-client",
                  client_secret: "gAAAAA-encrypted-client-secret",
                },
              },
            },
          },
        },
      },
    } as unknown as SettingsApiResponse);

    const result = await substituteRedactedMcpCredentials({
      id: "superhuman-mail",
      type: "shttp",
      name: "superhuman-mail",
      url: "https://mcp.mail.superhuman.com/mcp",
      auth: {
        strategy: "oauth2",
        state: {
          tokens: {
            access_token: REDACTED_MCP_SECRET_VALUE,
            refresh_token: REDACTED_MCP_SECRET_VALUE,
          },
          client_info: {
            client_id: "superhuman-client",
            client_secret: REDACTED_MCP_SECRET_VALUE,
          },
        },
      },
    });

    expect(result.auth).toEqual({
      strategy: "oauth2",
      state: {
        tokens: {
          access_token: "gAAAAA-encrypted-access-token",
          refresh_token: "gAAAAA-encrypted-refresh-token",
        },
        client_info: {
          client_id: "superhuman-client",
          client_secret: "gAAAAA-encrypted-client-secret",
        },
      },
    });
  });

  it("preserves fresh OAuth edits while substituting only redacted leaves", async () => {
    vi.spyOn(SettingsService, "fetchSettingsFromApi").mockResolvedValue({
      agent_settings: {
        mcp_config: {
          "superhuman-mail": {
            url: "https://mcp.mail.superhuman.com/mcp",
            auth: {
              strategy: "oauth2",
              state: {
                tokens: {
                  access_token: "gAAAAA-encrypted-access-token",
                  refresh_token: "gAAAAA-encrypted-refresh-token",
                },
                client_info: {
                  client_id: "old-client-id",
                  client_secret: "gAAAAA-encrypted-client-secret",
                },
              },
            },
          },
        },
      },
    } as unknown as SettingsApiResponse);

    const result = await substituteRedactedMcpCredentials({
      id: "superhuman-mail",
      type: "shttp",
      name: "superhuman-mail",
      url: "https://mcp.mail.superhuman.com/mcp",
      auth: {
        strategy: "oauth2",
        state: {
          tokens: {
            access_token: REDACTED_MCP_SECRET_VALUE,
            refresh_token: REDACTED_MCP_SECRET_VALUE,
          },
          client_info: {
            client_id: "new-client-id",
            client_secret: REDACTED_MCP_SECRET_VALUE,
          },
        },
      },
    });

    expect(result.auth).toEqual({
      strategy: "oauth2",
      state: {
        tokens: {
          access_token: "gAAAAA-encrypted-access-token",
          refresh_token: "gAAAAA-encrypted-refresh-token",
        },
        client_info: {
          client_id: "new-client-id",
          client_secret: "gAAAAA-encrypted-client-secret",
        },
      },
    });
  });

  it("preserves fresh header-auth edits while substituting only redacted leaves", async () => {
    vi.spyOn(SettingsService, "fetchSettingsFromApi").mockResolvedValue({
      agent_settings: {
        mcp_config: {
          mail: {
            url: "https://mail.example/mcp",
            auth: {
              strategy: "header",
              headers: {
                "X-API-Key": "gAAAAA-encrypted-api-key",
                "X-Region": "us-east-1",
              },
            },
          },
        },
      },
    } as unknown as SettingsApiResponse);

    const result = await substituteRedactedMcpCredentials({
      id: "mail",
      type: "shttp",
      name: "mail",
      url: "https://mail.example/mcp",
      auth: {
        strategy: "header",
        headers: {
          "X-API-Key": REDACTED_MCP_SECRET_VALUE,
          "X-Region": "eu-west-1",
        },
      },
    });

    expect(result.auth).toEqual({
      strategy: "header",
      headers: {
        "X-API-Key": "gAAAAA-encrypted-api-key",
        "X-Region": "eu-west-1",
      },
    });
  });

  it("replaces redacted raw remote headers with encrypted stored values", async () => {
    vi.spyOn(SettingsService, "fetchSettingsFromApi").mockResolvedValue({
      agent_settings: {
        mcp_config: {
          github: {
            url: "https://github.example/mcp",
            headers: {
              Authorization: "Bearer gAAAAA-encrypted-github-token",
            },
          },
        },
      },
    } as unknown as SettingsApiResponse);

    const result = await substituteRedactedMcpCredentials({
      id: "github",
      type: "shttp",
      name: "github",
      url: "https://github.example/mcp",
      headers: { Authorization: REDACTED_MCP_SECRET_VALUE },
    });

    expect(result.headers).toEqual({
      Authorization: "Bearer gAAAAA-encrypted-github-token",
    });
  });
});