Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wild-suns-bearer-space.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Fix `HttpApiSecurity` bearer/http credential decoding
3 changes: 2 additions & 1 deletion packages/effect/src/unstable/httpapi/HttpApiBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ export const securityDecode = <Security extends HttpApiSecurity.HttpApiSecurity>
case "Http": {
return Effect.map(
HttpServerRequest,
(request) => Redacted.make((request.headers.authorization ?? "").slice(self.schemeLength)) as any
// schemeLength + space
(request) => Redacted.make((request.headers.authorization ?? "").slice(self.schemeLength + 1)) as any
)
}
case "ApiKey": {
Expand Down
38 changes: 38 additions & 0 deletions packages/effect/test/unstable/httpapi/HttpApiSecurity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, it } from "@effect/vitest"
import { strictEqual } from "@effect/vitest/utils"
import { Effect, Redacted } from "effect"
import { HttpClientRequest, HttpServerRequest } from "effect/unstable/http"
import { HttpApiBuilder, HttpApiSecurity } from "effect/unstable/httpapi"

describe("HttpApiSecurity", () => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also have a test that validates that credentials from a different scheme than the one specified are rejected.

See the tests in Julius' PR #2315

describe("securityDecode", () => {
it.effect("decodes a bearer token without a leading space", () =>
Effect.gen(function*() {
const token = "abc123"
const { headers } = HttpClientRequest.get("http://localhost/").pipe(
HttpClientRequest.bearerToken(token)
)
const credential = yield* HttpApiBuilder.securityDecode(HttpApiSecurity.bearer).pipe(
Effect.provideService(
HttpServerRequest.HttpServerRequest,
HttpServerRequest.fromWeb(new Request("http://localhost/", { headers }))
),
Effect.provideService(HttpServerRequest.ParsedSearchParams, {})
)

strictEqual(Redacted.value(credential), token)
}))

it.effect("decodes a custom http scheme without a leading space", () =>
Effect.gen(function*() {
const credential = yield* HttpApiBuilder.securityDecode(HttpApiSecurity.http({ scheme: "Token" })).pipe(
Effect.provideService(
HttpServerRequest.HttpServerRequest,
HttpServerRequest.fromWeb(new Request("http://localhost/", { headers: { authorization: "Token abc123" } }))
),
Effect.provideService(HttpServerRequest.ParsedSearchParams, {})
)
strictEqual(Redacted.value(credential), "abc123")
}))
})
})
Loading