mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-04 03:35:36 +08:00
fix(http-api): mask every client path substitution when param wire keys are unknown
This commit is contained in:
@@ -46,6 +46,7 @@ import type * as HttpApiGroup from "./HttpApiGroup.ts"
|
||||
import type * as HttpApiMiddleware from "./HttpApiMiddleware.ts"
|
||||
import * as HttpApiSchema from "./HttpApiSchema.ts"
|
||||
import * as HttpApiPath from "./internal/path.ts"
|
||||
import { getRedactedKeys } from "./internal/redactedKeys.ts"
|
||||
|
||||
/**
|
||||
* The type-safe client shape generated from HTTP API groups, with non-top-level
|
||||
@@ -514,7 +515,9 @@ export const makeClient = <ApiId extends string, Groups extends HttpApiGroup.Con
|
||||
|
||||
const middlewareKeys = Array.from(onEndpointOptions.middleware, (tag) => `${tag.key}/Client`)
|
||||
const redactedNames = HttpApi.getEndpointRedactedNames(api, group, endpoint)
|
||||
const redactedParams = HttpApi.getEndpointRedactedParams(api, group, endpoint)
|
||||
const redactedParams: ReadonlyArray<string> | "*" = getRedactedKeys(endpoint.params) === undefined
|
||||
? "*"
|
||||
: HttpApi.getEndpointRedactedParams(api, group, endpoint)
|
||||
|
||||
const endpointFn = Effect.fnUntraced(function*(
|
||||
request: {
|
||||
@@ -535,7 +538,7 @@ export const makeClient = <ApiId extends string, Groups extends HttpApiGroup.Con
|
||||
const params = (yield* encodeParams(request.params)) as Record<string, string>
|
||||
const sent = makeUrl(params)
|
||||
httpRequest = HttpClientRequest.setUrl(httpRequest, sent)
|
||||
if (redactedParams.length > 0) {
|
||||
if (redactedParams === "*" || redactedParams.length > 0) {
|
||||
redactedPath = { sent: fullPath(sent), redacted: fullPath(makeUrl(params, redactedParams)) }
|
||||
}
|
||||
}
|
||||
@@ -987,17 +990,19 @@ export const urlBuilder = <Api extends HttpApi.Constraint>(api: Api, options?: {
|
||||
|
||||
const compilePath = (path: string, schema: Schema.Top | undefined) => {
|
||||
if (schema === undefined || !path.includes(":")) {
|
||||
return (_: any, _redacted?: ReadonlyArray<string>) => path
|
||||
return (_: any, _redacted?: ReadonlyArray<string> | "*") => path
|
||||
}
|
||||
const paramNames = HttpApiPath.getParamNames(schema)
|
||||
return (params: Record<string, string | undefined>, redacted?: ReadonlyArray<string>) =>
|
||||
return (params: Record<string, string | undefined>, redacted?: ReadonlyArray<string> | "*") =>
|
||||
HttpApiPath.replaceParams(path, paramNames, (key, optional) => {
|
||||
const value = params[key]
|
||||
if (value === "." || value === "..") {
|
||||
// dot segments survive encoding and would be resolved by URL normalization
|
||||
throw new Error(`Invalid path parameter: ${key}`)
|
||||
}
|
||||
if (value !== undefined) return redacted?.includes(key) ? "REDACTED" : encodeURIComponent(value)
|
||||
if (value !== undefined) {
|
||||
return redacted === "*" || redacted?.includes(key) ? "REDACTED" : encodeURIComponent(value)
|
||||
}
|
||||
if (optional) return undefined
|
||||
throw new Error(`Missing path parameter: ${key}`)
|
||||
})
|
||||
|
||||
@@ -1352,6 +1352,9 @@ describe("HttpApiClient", () => {
|
||||
params: Schema.Struct({ token: Schema.RedactedFromValue(Schema.String), id: Schema.String }).pipe(
|
||||
Schema.encodeKeys({ token: "tok" })
|
||||
)
|
||||
}),
|
||||
HttpApiEndpoint.get("recordParam", "/record/:tok$1", {
|
||||
params: Schema.Record(Schema.String, Schema.RedactedFromValue(Schema.String))
|
||||
})
|
||||
)
|
||||
)
|
||||
@@ -1369,6 +1372,21 @@ describe("HttpApiClient", () => {
|
||||
strictEqual(capture.span.attributes.get("url.path"), "/api/v1/p/REDACTED/REDACTED")
|
||||
strictEqual(capture.sent.url, "http://test/api/v1/p/s3cret/visible")
|
||||
|
||||
yield* capture.trace(client.test.recordParam({ params: { tok: Redacted.make("s3cret") } }))
|
||||
strictEqual(capture.span.attributes.get("url.full"), "http://test/api/v1/record/REDACTED$1")
|
||||
strictEqual(capture.span.attributes.get("url.path"), "/api/v1/record/REDACTED$1")
|
||||
strictEqual(capture.sent.url, "http://test/api/v1/record/s3cret$1")
|
||||
|
||||
const errorClient = yield* HttpApiClient.makeWith(Api, {
|
||||
baseUrl: "http://test/api/v1",
|
||||
httpClient: clientFromResponse(() => new Response("boom", { status: 500 }))
|
||||
})
|
||||
const recordError = yield* Effect.flip(
|
||||
errorClient.test.recordParam({ params: { tok: Redacted.make("s3cret") } })
|
||||
)
|
||||
assert.include(recordError.message, "GET http://test/api/v1/record/REDACTED$1")
|
||||
assert.notInclude(JSON.stringify(recordError), "s3cret")
|
||||
|
||||
const rewrittenClient = yield* HttpApiClient.makeWith(Api, {
|
||||
baseUrl: "http://test/api/v1",
|
||||
httpClient: capture.httpClient.pipe(
|
||||
|
||||
Reference in New Issue
Block a user