@chatpanel/gateway 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chatpanel/gateway",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Local privacy gateway — redacts PII out of OpenAI/Anthropic API traffic before it reaches a model, then restores it in the reply. Point opencode, codex, aider, Claude Code, etc. at it.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -29,7 +29,8 @@ export function persistConfig(cfg, path = configPath()) {
29
29
  export function publicConfig(cfg, { proUnlocked = false } = {}) {
30
30
  return {
31
31
  backend: cfg.backend,
32
- destinations: Array.isArray(cfg.destinations) ? cfg.destinations : [],
32
+ // Strip per-destination apiKey (write-only).
33
+ destinations: (Array.isArray(cfg.destinations) ? cfg.destinations : []).map((d) => { const { apiKey, ...rest } = d; return { ...rest, hasKey: !!apiKey }; }),
33
34
  bridge: { url: cfg.bridge?.url, agent: cfg.bridge?.agent, hasToken: !!cfg.bridge?.token },
34
35
  upstreams: cfg.upstreams,
35
36
  redaction: {
@@ -50,14 +51,22 @@ export function publicConfig(cfg, { proUnlocked = false } = {}) {
50
51
  export function applyConfigPatch(cfg, patch = {}) {
51
52
  if (patch.backend === 'bridge' || patch.backend === 'api') cfg.backend = patch.backend;
52
53
  if (Array.isArray(patch.destinations)) {
54
+ // Preserve a destination's apiKey when the patch omits it (it's write-only —
55
+ // publicConfig strips it, so the UI never round-trips it back).
56
+ const prev = new Map((Array.isArray(cfg.destinations) ? cfg.destinations : []).map((d) => [d.id, d]));
53
57
  cfg.destinations = patch.destinations
54
58
  .filter((d) => d && typeof d.id === 'string' && (d.type === 'agent' || d.type === 'api'))
55
- .map((d) => ({
56
- id: d.id, type: d.type,
57
- ...(d.type === 'agent' ? { agent: d.agent || d.id } : {}),
58
- ...(d.type === 'api' ? { baseUrl: String(d.baseUrl || ''), protocol: d.protocol === 'anthropic' ? 'anthropic' : 'openai' } : {}),
59
- models: Array.isArray(d.models) ? d.models.filter((m) => typeof m === 'string' && m) : [],
60
- }));
59
+ .map((d) => {
60
+ const out = { id: d.id, type: d.type, models: Array.isArray(d.models) ? d.models.filter((m) => typeof m === 'string' && m) : [] };
61
+ if (d.type === 'agent') out.agent = d.agent || d.id;
62
+ if (d.type === 'api') {
63
+ out.baseUrl = String(d.baseUrl || '');
64
+ out.protocol = d.protocol === 'anthropic' ? 'anthropic' : 'openai';
65
+ const key = (typeof d.apiKey === 'string' && d.apiKey) ? d.apiKey : prev.get(d.id)?.apiKey;
66
+ if (key) out.apiKey = key;
67
+ }
68
+ return out;
69
+ });
61
70
  }
62
71
  if (patch.bridge && typeof patch.bridge === 'object') {
63
72
  if (typeof patch.bridge.url === 'string') cfg.bridge.url = patch.bridge.url;
package/src/server.js CHANGED
@@ -32,7 +32,7 @@ import * as openai from './openai.js';
32
32
  import * as responses from './responses.js';
33
33
  import * as anthropic from './anthropic.js';
34
34
 
35
- export const VERSION = '0.2.0';
35
+ export const VERSION = '0.2.1';
36
36
 
37
37
  const KNOWN_AGENTS = new Set(['codex', 'claude', 'opencode', 'pi', 'kiro', 'antigravity']);
38
38
 
@@ -83,6 +83,18 @@ function pickAgent(model, cfg) {
83
83
  return KNOWN_AGENTS.has(model) ? model : cfg.bridge.agent;
84
84
  }
85
85
 
86
+ // Guard against an api destination pointing back at THIS gateway (loopback host +
87
+ // our own port) — forwarding there would loop forever.
88
+ function isSelfUrl(baseUrl, cfg) {
89
+ try {
90
+ const u = new URL(baseUrl);
91
+ const host = u.hostname.replace(/^\[|\]$/g, '');
92
+ const loop = host === '127.0.0.1' || host === 'localhost' || host === '::1';
93
+ const port = u.port || (u.protocol === 'https:' ? '443' : '80');
94
+ return loop && String(port) === String(cfg.port);
95
+ } catch { return false; }
96
+ }
97
+
86
98
  async function readBody(req, maxBytes) {
87
99
  const chunks = [];
88
100
  let size = 0;
@@ -156,12 +168,19 @@ async function handleBridge(req, res, { kind, adapter, redactable, pathname, age
156
168
 
157
169
  // ---- backend: api ----------------------------------------------------------
158
170
 
159
- async function handleApi(req, res, { adapter, pathname, search, base }, outBody, vault) {
171
+ async function handleApi(req, res, { adapter, pathname, search, base, destKey, destProtocol }, outBody, vault) {
160
172
  let upstream;
161
173
  try {
174
+ const headers = forwardHeaders(req.headers, base);
175
+ // If the destination carries its own key (imported from a configured API),
176
+ // forward WITH it instead of relying on the client's auth header.
177
+ if (destKey) {
178
+ if (destProtocol === 'anthropic') { headers['x-api-key'] = destKey; delete headers.authorization; }
179
+ else { headers.authorization = `Bearer ${destKey}`; }
180
+ }
162
181
  upstream = await fetch(base.replace(/\/$/, '') + pathname + search, {
163
182
  method: req.method,
164
- headers: forwardHeaders(req.headers, base),
183
+ headers,
165
184
  body: ['GET', 'HEAD'].includes(req.method) ? undefined : outBody,
166
185
  });
167
186
  } catch (e) {
@@ -280,7 +299,10 @@ export function createGateway(cfg = loadConfig()) {
280
299
  }
281
300
  if (dest && dest.type === 'api') {
282
301
  if (!dest.baseUrl) return sendJson(res, 502, { error: `destination "${dest.id}" has no baseUrl` });
283
- return handleApi(req, res, { ...r, pathname, search: url.search, base: dest.baseUrl }, outBody, vault);
302
+ if (isSelfUrl(dest.baseUrl, cfg)) {
303
+ return sendJson(res, 508, { error: { message: `destination "${dest.id}" points back at the gateway (${dest.baseUrl}) — refusing to forward (would loop).`, type: 'loop_detected' } });
304
+ }
305
+ return handleApi(req, res, { ...r, pathname, search: url.search, base: dest.baseUrl, destKey: dest.apiKey, destProtocol: dest.protocol }, outBody, vault);
284
306
  }
285
307
  return handleBridge(req, res, { ...r, pathname, agentOverride: dest?.agent }, body, vault, cfg);
286
308
  });