skalpel 2.0.19 → 2.0.21

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.
@@ -18,7 +18,9 @@ var init_dispatcher = __esm({
18
18
  keepAliveTimeout: 1e4,
19
19
  keepAliveMaxTimeout: 6e4,
20
20
  connections: 100,
21
- pipelining: 1
21
+ pipelining: 1,
22
+ allowH2: false
23
+ // Force HTTP/1.1 to prevent GCP LB WebSocket downgrade
22
24
  });
23
25
  }
24
26
  });
@@ -638,6 +640,12 @@ __export(handler_exports, {
638
640
  isSkalpelBackendFailure: () => isSkalpelBackendFailure,
639
641
  shouldRouteToSkalpel: () => shouldRouteToSkalpel
640
642
  });
643
+ function validateCodexAuth(oauthToken, inboundAuth) {
644
+ if (!oauthToken && !inboundAuth) {
645
+ return { valid: false, error: "no_credentials" };
646
+ }
647
+ return { valid: true };
648
+ }
641
649
  function collectBody(req) {
642
650
  return new Promise((resolve, reject) => {
643
651
  const chunks = [];
@@ -902,6 +910,18 @@ async function handleWebSocketBridge(clientWs, req, config, source, logger) {
902
910
  const oauthHeader = req.headers["authorization"] ?? "";
903
911
  oauthToken = oauthHeader.toLowerCase().startsWith("bearer ") ? oauthHeader.slice(7).trim() : "";
904
912
  }
913
+ const authResult = validateCodexAuth(freshOauthToken, oauthToken);
914
+ if (!authResult.valid) {
915
+ clientWs.send(JSON.stringify({
916
+ type: "error",
917
+ error: {
918
+ code: "no_credentials",
919
+ message: "Codex requires OAuth login. Run: codex login"
920
+ }
921
+ }));
922
+ clientWs.close(4e3, "no credentials");
923
+ return;
924
+ }
905
925
  const backend = new BackendWsClient({
906
926
  url: backendUrl,
907
927
  apiKey: config.apiKey,
@@ -1009,6 +1029,18 @@ function parseSseEvents(buffer) {
1009
1029
  return { events, rest };
1010
1030
  }
1011
1031
  async function fallbackToHttp(clientWs, config, source, logger, requestBody, inboundAuth) {
1032
+ const preflightAuth = validateCodexAuth(getFreshAccessToken(), inboundAuth);
1033
+ if (!preflightAuth.valid) {
1034
+ clientWs.send(JSON.stringify({
1035
+ type: "error",
1036
+ error: {
1037
+ code: "no_credentials",
1038
+ message: "Codex requires OAuth login. Run: codex login"
1039
+ }
1040
+ }));
1041
+ clientWs.close(4e3, "no credentials");
1042
+ return;
1043
+ }
1012
1044
  try {
1013
1045
  const freshToken = getFreshAccessToken();
1014
1046
  const authHeader = freshToken !== null ? `Bearer ${freshToken}` : inboundAuth;
@@ -1038,17 +1070,37 @@ async function fallbackToHttp(clientWs, config, source, logger, requestBody, inb
1038
1070
  },
1039
1071
  body: requestBody
1040
1072
  });
1041
- if (!resp.ok || resp.body === null) {
1042
- clientWs.send(
1043
- JSON.stringify({
1044
- type: "error",
1045
- error: { code: resp.status, message: `http fallback status ${resp.status}` }
1046
- })
1047
- );
1073
+ if (!resp.ok) {
1074
+ let errorBody = "";
1048
1075
  try {
1049
- clientWs.close(1011, "http fallback failed");
1076
+ const bodyPromise = resp.text();
1077
+ const timeoutPromise = new Promise(
1078
+ (_, reject) => setTimeout(() => reject(new Error("body read timeout")), 5e3)
1079
+ );
1080
+ errorBody = await Promise.race([bodyPromise, timeoutPromise]);
1081
+ } catch (e) {
1082
+ errorBody = `status ${resp.status}`;
1083
+ }
1084
+ let errorMessage = `Backend error: ${resp.status}`;
1085
+ try {
1086
+ const parsed = JSON.parse(errorBody);
1087
+ errorMessage = parsed?.error?.message || parsed?.detail || errorMessage;
1050
1088
  } catch {
1089
+ if (errorBody.length < 200) errorMessage = errorBody;
1051
1090
  }
1091
+ clientWs.send(JSON.stringify({
1092
+ type: "error",
1093
+ error: { code: resp.status, message: errorMessage }
1094
+ }));
1095
+ clientWs.close(1011, "backend error");
1096
+ return;
1097
+ }
1098
+ if (resp.body === null) {
1099
+ clientWs.send(JSON.stringify({
1100
+ type: "error",
1101
+ error: { code: resp.status, message: "empty response body" }
1102
+ }));
1103
+ clientWs.close(1011, "empty body");
1052
1104
  return;
1053
1105
  }
1054
1106
  const reader = resp.body.getReader();