forked from EduCraft/curriculum-project-hub
fix: carry provider capability in dedicated header
This commit is contained in:
Generated
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@paradigm/hub",
|
"name": "@paradigm/hub",
|
||||||
"version": "0.0.18",
|
"version": "0.0.19",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@paradigm/hub",
|
"name": "@paradigm/hub",
|
||||||
"version": "0.0.18",
|
"version": "0.0.19",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@anthropic-ai/claude-agent-sdk": "^0.3.202",
|
"@anthropic-ai/claude-agent-sdk": "^0.3.202",
|
||||||
"@fastify/cookie": "^11.0.2",
|
"@fastify/cookie": "^11.0.2",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@paradigm/hub",
|
"name": "@paradigm/hub",
|
||||||
"version": "0.0.18",
|
"version": "0.0.19",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ const PROVIDER_ENV_KEYS = new Set([
|
|||||||
"ANTHROPIC_BASE_URL",
|
"ANTHROPIC_BASE_URL",
|
||||||
"ANTHROPIC_AUTH_TOKEN",
|
"ANTHROPIC_AUTH_TOKEN",
|
||||||
"ANTHROPIC_API_KEY",
|
"ANTHROPIC_API_KEY",
|
||||||
|
"ANTHROPIC_CUSTOM_HEADERS",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const SAFE_HOST_ENV_KEYS = [
|
const SAFE_HOST_ENV_KEYS = [
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ const REPLACED_REQUEST_HEADERS = new Set([
|
|||||||
"content-length",
|
"content-length",
|
||||||
"host",
|
"host",
|
||||||
"x-api-key",
|
"x-api-key",
|
||||||
|
"x-cph-run-capability",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
export interface ProviderUpstreamCredential {
|
export interface ProviderUpstreamCredential {
|
||||||
@@ -44,6 +45,7 @@ export interface ProviderProxyDiagnostic {
|
|||||||
readonly authShape?: {
|
readonly authShape?: {
|
||||||
readonly bearerLength: number;
|
readonly bearerLength: number;
|
||||||
readonly apiKeyLength: number;
|
readonly apiKeyLength: number;
|
||||||
|
readonly customCapabilityLength: number;
|
||||||
readonly expectedLength: number;
|
readonly expectedLength: number;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -87,6 +89,7 @@ export async function openProviderProxyLease(
|
|||||||
ANTHROPIC_BASE_URL: `http://127.0.0.1:${address.port}`,
|
ANTHROPIC_BASE_URL: `http://127.0.0.1:${address.port}`,
|
||||||
ANTHROPIC_AUTH_TOKEN: capability,
|
ANTHROPIC_AUTH_TOKEN: capability,
|
||||||
ANTHROPIC_API_KEY: capability,
|
ANTHROPIC_API_KEY: capability,
|
||||||
|
ANTHROPIC_CUSTOM_HEADERS: `X-CPH-Run-Capability: ${capability}`,
|
||||||
},
|
},
|
||||||
sensitiveValues: [capability],
|
sensitiveValues: [capability],
|
||||||
async close(): Promise<void> {
|
async close(): Promise<void> {
|
||||||
@@ -107,7 +110,12 @@ async function forwardProviderRequest(
|
|||||||
upstream: ProviderUpstreamCredential,
|
upstream: ProviderUpstreamCredential,
|
||||||
options: ProviderProxyOptions,
|
options: ProviderProxyOptions,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (!authorized(request.headers.authorization, header(request.headers["x-api-key"]), capability)) {
|
if (!authorized(
|
||||||
|
request.headers.authorization,
|
||||||
|
header(request.headers["x-api-key"]),
|
||||||
|
header(request.headers["x-cph-run-capability"]),
|
||||||
|
capability,
|
||||||
|
)) {
|
||||||
options.onDiagnostic?.({
|
options.onDiagnostic?.({
|
||||||
code: "provider_proxy_unauthorized",
|
code: "provider_proxy_unauthorized",
|
||||||
category: "authorization",
|
category: "authorization",
|
||||||
@@ -116,6 +124,7 @@ async function forwardProviderRequest(
|
|||||||
? request.headers.authorization.length - "Bearer ".length
|
? request.headers.authorization.length - "Bearer ".length
|
||||||
: 0,
|
: 0,
|
||||||
apiKeyLength: header(request.headers["x-api-key"])?.length ?? 0,
|
apiKeyLength: header(request.headers["x-api-key"])?.length ?? 0,
|
||||||
|
customCapabilityLength: header(request.headers["x-cph-run-capability"])?.length ?? 0,
|
||||||
expectedLength: capability.length,
|
expectedLength: capability.length,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -183,11 +192,16 @@ async function forwardProviderRequest(
|
|||||||
function authorized(
|
function authorized(
|
||||||
authorization: string | undefined,
|
authorization: string | undefined,
|
||||||
apiKey: string | undefined,
|
apiKey: string | undefined,
|
||||||
|
customCapability: string | undefined,
|
||||||
capability: string,
|
capability: string,
|
||||||
): boolean {
|
): boolean {
|
||||||
const value = authorization?.startsWith("Bearer ")
|
const bearer = authorization?.startsWith("Bearer ")
|
||||||
? authorization.slice("Bearer ".length)
|
? authorization.slice("Bearer ".length)
|
||||||
: apiKey;
|
: undefined;
|
||||||
|
return [bearer, apiKey, customCapability].some((value) => matchesCapability(value, capability));
|
||||||
|
}
|
||||||
|
|
||||||
|
function matchesCapability(value: string | undefined, capability: string): boolean {
|
||||||
if (value === undefined) return false;
|
if (value === undefined) return false;
|
||||||
const supplied = Buffer.from(value);
|
const supplied = Buffer.from(value);
|
||||||
const expected = Buffer.from(capability);
|
const expected = Buffer.from(capability);
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ describe("run-scoped provider proxy", () => {
|
|||||||
ANTHROPIC_BASE_URL: expect.stringMatching(/^http:\/\/127\.0\.0\.1:\d+$/),
|
ANTHROPIC_BASE_URL: expect.stringMatching(/^http:\/\/127\.0\.0\.1:\d+$/),
|
||||||
ANTHROPIC_AUTH_TOKEN: expect.any(String),
|
ANTHROPIC_AUTH_TOKEN: expect.any(String),
|
||||||
ANTHROPIC_API_KEY: expect.any(String),
|
ANTHROPIC_API_KEY: expect.any(String),
|
||||||
|
ANTHROPIC_CUSTOM_HEADERS: expect.stringMatching(/^X-CPH-Run-Capability: /),
|
||||||
});
|
});
|
||||||
expect(lease.sdkEnv.ANTHROPIC_AUTH_TOKEN).toBe(lease.sdkEnv.ANTHROPIC_API_KEY);
|
expect(lease.sdkEnv.ANTHROPIC_AUTH_TOKEN).toBe(lease.sdkEnv.ANTHROPIC_API_KEY);
|
||||||
|
|
||||||
@@ -108,7 +109,7 @@ describe("run-scoped provider proxy", () => {
|
|||||||
expect(diagnostics).toEqual([{
|
expect(diagnostics).toEqual([{
|
||||||
code: "provider_proxy_unauthorized",
|
code: "provider_proxy_unauthorized",
|
||||||
category: "authorization",
|
category: "authorization",
|
||||||
authShape: { bearerLength: 0, apiKeyLength: 0, expectedLength: 43 },
|
authShape: { bearerLength: 0, apiKeyLength: 0, customCapabilityLength: 0, expectedLength: 43 },
|
||||||
}]);
|
}]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user