Skip to content

Commit cd7ed8b

Browse files
committed
fix: check strict mode before proxy in all handlers
1 parent 71b1c95 commit cd7ed8b

17 files changed

Lines changed: 626 additions & 254 deletions

src/bedrock-converse.ts

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,34 @@ export async function handleConverse(
600600
return;
601601

602602
if (!fixture) {
603+
const effectiveStrict = resolveStrictMode(defaults.strict, req.headers);
604+
if (effectiveStrict) {
605+
const strictStatus = 503;
606+
const strictMessage = "Strict mode: no fixture matched";
607+
logger.error(`STRICT: No fixture matched for ${req.method ?? "POST"} ${urlPath}`);
608+
journal.add({
609+
method: req.method ?? "POST",
610+
path: urlPath,
611+
headers: flattenHeaders(req.headers),
612+
body: completionReq,
613+
response: {
614+
status: strictStatus,
615+
fixture: null,
616+
...strictOverrideField(defaults.strict, req.headers),
617+
},
618+
});
619+
writeErrorResponse(
620+
res,
621+
strictStatus,
622+
JSON.stringify({
623+
error: {
624+
message: strictMessage,
625+
type: "invalid_request_error",
626+
},
627+
}),
628+
);
629+
return;
630+
}
603631
if (defaults.record) {
604632
const outcome = await proxyAndRecord(
605633
req,
@@ -623,31 +651,22 @@ export async function handleConverse(
623651
return;
624652
}
625653
}
626-
const effectiveStrict = resolveStrictMode(defaults.strict, req.headers);
627-
const strictStatus = effectiveStrict ? 503 : 404;
628-
const strictMessage = effectiveStrict
629-
? "Strict mode: no fixture matched"
630-
: "No fixture matched";
631-
if (effectiveStrict) {
632-
logger.error(`STRICT: No fixture matched for ${req.method ?? "POST"} ${urlPath}`);
633-
}
634654
journal.add({
635655
method: req.method ?? "POST",
636656
path: urlPath,
637657
headers: flattenHeaders(req.headers),
638658
body: completionReq,
639659
response: {
640-
status: strictStatus,
660+
status: 404,
641661
fixture: null,
642-
...strictOverrideField(defaults.strict, req.headers),
643662
},
644663
});
645664
writeErrorResponse(
646665
res,
647-
strictStatus,
666+
404,
648667
JSON.stringify({
649668
error: {
650-
message: strictMessage,
669+
message: "No fixture matched",
651670
type: "invalid_request_error",
652671
},
653672
}),
@@ -871,6 +890,34 @@ export async function handleConverseStream(
871890
return;
872891

873892
if (!fixture) {
893+
const effectiveStrict = resolveStrictMode(defaults.strict, req.headers);
894+
if (effectiveStrict) {
895+
const strictStatus = 503;
896+
const strictMessage = "Strict mode: no fixture matched";
897+
logger.error(`STRICT: No fixture matched for ${req.method ?? "POST"} ${urlPath}`);
898+
journal.add({
899+
method: req.method ?? "POST",
900+
path: urlPath,
901+
headers: flattenHeaders(req.headers),
902+
body: completionReq,
903+
response: {
904+
status: strictStatus,
905+
fixture: null,
906+
...strictOverrideField(defaults.strict, req.headers),
907+
},
908+
});
909+
writeErrorResponse(
910+
res,
911+
strictStatus,
912+
JSON.stringify({
913+
error: {
914+
message: strictMessage,
915+
type: "invalid_request_error",
916+
},
917+
}),
918+
);
919+
return;
920+
}
874921
if (defaults.record) {
875922
const outcome = await proxyAndRecord(
876923
req,
@@ -894,31 +941,22 @@ export async function handleConverseStream(
894941
return;
895942
}
896943
}
897-
const effectiveStrict = resolveStrictMode(defaults.strict, req.headers);
898-
const strictStatus = effectiveStrict ? 503 : 404;
899-
const strictMessage = effectiveStrict
900-
? "Strict mode: no fixture matched"
901-
: "No fixture matched";
902-
if (effectiveStrict) {
903-
logger.error(`STRICT: No fixture matched for ${req.method ?? "POST"} ${urlPath}`);
904-
}
905944
journal.add({
906945
method: req.method ?? "POST",
907946
path: urlPath,
908947
headers: flattenHeaders(req.headers),
909948
body: completionReq,
910949
response: {
911-
status: strictStatus,
950+
status: 404,
912951
fixture: null,
913-
...strictOverrideField(defaults.strict, req.headers),
914952
},
915953
});
916954
writeErrorResponse(
917955
res,
918-
strictStatus,
956+
404,
919957
JSON.stringify({
920958
error: {
921-
message: strictMessage,
959+
message: "No fixture matched",
922960
type: "invalid_request_error",
923961
},
924962
}),

src/bedrock.ts

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,34 @@ export async function handleBedrock(
428428
return;
429429

430430
if (!fixture) {
431+
const effectiveStrict = resolveStrictMode(defaults.strict, req.headers);
432+
if (effectiveStrict) {
433+
const strictStatus = 503;
434+
const strictMessage = "Strict mode: no fixture matched";
435+
logger.error(`STRICT: No fixture matched for ${req.method ?? "POST"} ${urlPath}`);
436+
journal.add({
437+
method: req.method ?? "POST",
438+
path: urlPath,
439+
headers: flattenHeaders(req.headers),
440+
body: completionReq,
441+
response: {
442+
status: strictStatus,
443+
fixture: null,
444+
...strictOverrideField(defaults.strict, req.headers),
445+
},
446+
});
447+
writeErrorResponse(
448+
res,
449+
strictStatus,
450+
JSON.stringify({
451+
error: {
452+
message: strictMessage,
453+
type: "invalid_request_error",
454+
},
455+
}),
456+
);
457+
return;
458+
}
431459
if (defaults.record) {
432460
const outcome = await proxyAndRecord(
433461
req,
@@ -451,31 +479,22 @@ export async function handleBedrock(
451479
return;
452480
}
453481
}
454-
const effectiveStrict = resolveStrictMode(defaults.strict, req.headers);
455-
const strictStatus = effectiveStrict ? 503 : 404;
456-
const strictMessage = effectiveStrict
457-
? "Strict mode: no fixture matched"
458-
: "No fixture matched";
459-
if (effectiveStrict) {
460-
logger.error(`STRICT: No fixture matched for ${req.method ?? "POST"} ${urlPath}`);
461-
}
462482
journal.add({
463483
method: req.method ?? "POST",
464484
path: urlPath,
465485
headers: flattenHeaders(req.headers),
466486
body: completionReq,
467487
response: {
468-
status: strictStatus,
488+
status: 404,
469489
fixture: null,
470-
...strictOverrideField(defaults.strict, req.headers),
471490
},
472491
});
473492
writeErrorResponse(
474493
res,
475-
strictStatus,
494+
404,
476495
JSON.stringify({
477496
error: {
478-
message: strictMessage,
497+
message: "No fixture matched",
479498
type: "invalid_request_error",
480499
},
481500
}),
@@ -1049,6 +1068,34 @@ export async function handleBedrockStream(
10491068
return;
10501069

10511070
if (!fixture) {
1071+
const effectiveStrict = resolveStrictMode(defaults.strict, req.headers);
1072+
if (effectiveStrict) {
1073+
const strictStatus = 503;
1074+
const strictMessage = "Strict mode: no fixture matched";
1075+
logger.error(`STRICT: No fixture matched for ${req.method ?? "POST"} ${urlPath}`);
1076+
journal.add({
1077+
method: req.method ?? "POST",
1078+
path: urlPath,
1079+
headers: flattenHeaders(req.headers),
1080+
body: completionReq,
1081+
response: {
1082+
status: strictStatus,
1083+
fixture: null,
1084+
...strictOverrideField(defaults.strict, req.headers),
1085+
},
1086+
});
1087+
writeErrorResponse(
1088+
res,
1089+
strictStatus,
1090+
JSON.stringify({
1091+
error: {
1092+
message: strictMessage,
1093+
type: "invalid_request_error",
1094+
},
1095+
}),
1096+
);
1097+
return;
1098+
}
10521099
if (defaults.record) {
10531100
const outcome = await proxyAndRecord(
10541101
req,
@@ -1072,31 +1119,22 @@ export async function handleBedrockStream(
10721119
return;
10731120
}
10741121
}
1075-
const effectiveStrict = resolveStrictMode(defaults.strict, req.headers);
1076-
const strictStatus = effectiveStrict ? 503 : 404;
1077-
const strictMessage = effectiveStrict
1078-
? "Strict mode: no fixture matched"
1079-
: "No fixture matched";
1080-
if (effectiveStrict) {
1081-
logger.error(`STRICT: No fixture matched for ${req.method ?? "POST"} ${urlPath}`);
1082-
}
10831122
journal.add({
10841123
method: req.method ?? "POST",
10851124
path: urlPath,
10861125
headers: flattenHeaders(req.headers),
10871126
body: completionReq,
10881127
response: {
1089-
status: strictStatus,
1128+
status: 404,
10901129
fixture: null,
1091-
...strictOverrideField(defaults.strict, req.headers),
10921130
},
10931131
});
10941132
writeErrorResponse(
10951133
res,
1096-
strictStatus,
1134+
404,
10971135
JSON.stringify({
10981136
error: {
1099-
message: strictMessage,
1137+
message: "No fixture matched",
11001138
type: "invalid_request_error",
11011139
},
11021140
}),

src/cohere.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,36 @@ export async function handleCohere(
864864
return;
865865

866866
if (!fixture) {
867+
const effectiveStrict = resolveStrictMode(defaults.strict, req.headers);
868+
if (effectiveStrict) {
869+
const strictStatus = 503;
870+
const strictMessage = "Strict mode: no fixture matched";
871+
logger.error(
872+
`STRICT: No fixture matched for ${req.method ?? "POST"} ${req.url ?? "/v2/chat"}`,
873+
);
874+
journal.add({
875+
method: req.method ?? "POST",
876+
path: req.url ?? "/v2/chat",
877+
headers: flattenHeaders(req.headers),
878+
body: completionReq,
879+
response: {
880+
status: strictStatus,
881+
fixture: null,
882+
...strictOverrideField(defaults.strict, req.headers),
883+
},
884+
});
885+
writeErrorResponse(
886+
res,
887+
strictStatus,
888+
JSON.stringify({
889+
error: {
890+
message: strictMessage,
891+
type: "invalid_request_error",
892+
},
893+
}),
894+
);
895+
return;
896+
}
867897
if (defaults.record) {
868898
const outcome = await proxyAndRecord(
869899
req,
@@ -887,33 +917,22 @@ export async function handleCohere(
887917
return;
888918
}
889919
}
890-
const effectiveStrict = resolveStrictMode(defaults.strict, req.headers);
891-
const strictStatus = effectiveStrict ? 503 : 404;
892-
const strictMessage = effectiveStrict
893-
? "Strict mode: no fixture matched"
894-
: "No fixture matched";
895-
if (effectiveStrict) {
896-
logger.error(
897-
`STRICT: No fixture matched for ${req.method ?? "POST"} ${req.url ?? "/v2/chat"}`,
898-
);
899-
}
900920
journal.add({
901921
method: req.method ?? "POST",
902922
path: req.url ?? "/v2/chat",
903923
headers: flattenHeaders(req.headers),
904924
body: completionReq,
905925
response: {
906-
status: strictStatus,
926+
status: 404,
907927
fixture: null,
908-
...strictOverrideField(defaults.strict, req.headers),
909928
},
910929
});
911930
writeErrorResponse(
912931
res,
913-
strictStatus,
932+
404,
914933
JSON.stringify({
915934
error: {
916-
message: strictMessage,
935+
message: "No fixture matched",
917936
type: "invalid_request_error",
918937
},
919938
}),

0 commit comments

Comments
 (0)