diff --git a/.changeset/old-spies-knock.md b/.changeset/old-spies-knock.md new file mode 100644 index 0000000000..1adf88ce8a --- /dev/null +++ b/.changeset/old-spies-knock.md @@ -0,0 +1,5 @@ +--- +"@effect/platform": patch +--- + +ensure requests & responses have headers redacted when inspecting diff --git a/packages/platform-browser/src/internal/httpClient.ts b/packages/platform-browser/src/internal/httpClient.ts index d6f20e12e0..7413910769 100644 --- a/packages/platform-browser/src/internal/httpClient.ts +++ b/packages/platform-browser/src/internal/httpClient.ts @@ -314,25 +314,11 @@ class ClientResponseImpl extends IncomingMessageImpl implem } toJSON(): unknown { - let body: unknown - try { - body = Effect.runSync(this.json) - } catch (_) { - // - } - try { - body = body ?? Effect.runSync(this.text) - } catch (_) { - // - } - return { + return IncomingMessage.inspect(this, { _id: "@effect/platform/HttpClientResponse", request: this.request.toJSON(), - status: this.status, - headers: this.headers, - remoteAddress: this.remoteAddress.toJSON(), - body - } + status: this.status + }) } } diff --git a/packages/platform/src/HttpIncomingMessage.ts b/packages/platform/src/HttpIncomingMessage.ts index 0422d1ab3f..c59d3f8d1c 100644 --- a/packages/platform/src/HttpIncomingMessage.ts +++ b/packages/platform/src/HttpIncomingMessage.ts @@ -5,7 +5,7 @@ import * as Effect from "effect/Effect" import * as FiberRef from "effect/FiberRef" import { dual } from "effect/Function" import * as Global from "effect/GlobalValue" -import type { Inspectable } from "effect/Inspectable" +import * as Inspectable from "effect/Inspectable" import * as Option from "effect/Option" import type * as ParseResult from "effect/ParseResult" import * as Schema from "effect/Schema" @@ -31,7 +31,7 @@ export type TypeId = typeof TypeId * @since 1.0.0 * @category models */ -export interface HttpIncomingMessage extends Inspectable { +export interface HttpIncomingMessage extends Inspectable.Inspectable { readonly [TypeId]: TypeId readonly headers: Headers.Headers readonly remoteAddress: Option.Option @@ -116,7 +116,7 @@ export const inspect = (self: HttpIncomingMessage, that: object): object = } const obj: any = { ...that, - headers: self.headers, + headers: Inspectable.redact(self.headers), remoteAddress: self.remoteAddress.toJSON() } if (body !== undefined) { diff --git a/packages/platform/src/internal/httpClientRequest.ts b/packages/platform/src/internal/httpClientRequest.ts index 8d57094805..1cf89476c7 100644 --- a/packages/platform/src/internal/httpClientRequest.ts +++ b/packages/platform/src/internal/httpClientRequest.ts @@ -29,7 +29,7 @@ const Proto = { url: this.url, urlParams: this.urlParams, hash: this.hash, - headers: this.headers, + headers: Inspectable.redact(this.headers), body: this.body.toJSON() } }, diff --git a/packages/platform/src/internal/httpServerResponse.ts b/packages/platform/src/internal/httpServerResponse.ts index dc1a46601f..0d652f0b23 100644 --- a/packages/platform/src/internal/httpServerResponse.ts +++ b/packages/platform/src/internal/httpServerResponse.ts @@ -75,7 +75,7 @@ class ServerResponseImpl extends Effectable.StructuralClass { ) assert.deepStrictEqual(response, { id: 1, userId: 1, title: "delectus aut autem", completed: false }) }).pipe(Effect.provide(JsonPlaceholderLive))) + + it("ClientRequest redacts headers", () => { + const request = HttpClientRequest.get(new URL("https://example.com")).pipe( + HttpClientRequest.setHeaders({ + "authorization": "foobar" + }) + ) + + const fiberRefs = FiberRefs.unsafeMake( + new Map([ + [ + Headers.currentRedactedNames, + [[FiberId.none, ["Authorization"]] as const] + ] as const + ]) + ) + const r = Inspectable.withRedactableContext(fiberRefs, () => Inspectable.toStringUnknown(request)) + const redacted = JSON.parse(r) + + assert.deepStrictEqual(redacted, { + _id: "@effect/platform/HttpClientRequest", + method: "GET", + url: "https://example.com/", + urlParams: [], + hash: { _id: "Option", _tag: "None" }, + headers: { authorization: "" }, + body: { _id: "@effect/platform/HttpBody", _tag: "Empty" } + }) + }) })