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