zio-http icon indicating copy to clipboard operation
zio-http copied to clipboard

fix: Authorization Header 400 Bad Request

Open melmathari opened this issue 9 months ago • 7 comments

Fixed: Wrong Error Code for Missing Authorization Header

Before, if the Authorization header was missing, the app returned a 400 Bad Request.
That just means something went wrong, but it didn’t clearly say the user wasn't logged in.

Now, the app checks if the Authorization header is missing or invalid.
If it is, it returns 401 Unauthorized, which correctly tells the user they need to log in.

Improvements

  • [x] If the Authorization header is missing, the response is now 401 Unauthorized
  • [x] If multiple headers are missing, including Authorization, the response is still 401 Unauthorized
  • [x] If the Authorization header is present but invalid (decoding error), the response is 401 Unauthorized

Compliance

This fix follows RFC 7235, which says:

The 401 (Unauthorized) status code indicates that the request has not been applied because it lackz valid authentication credentials for the resourrce.

🧪 Tests

To confirm nothing else broke, I ran:

sbt "zioHttpJVM / Test / testOnly endpoint"

All tests passed ✅

It only changes how Authorization header issues are handled. Everything else stays the same.

/claim #3235

melmathari avatar May 25 '25 23:05 melmathari

Deploy Preview for zio-http failed. Why did it fail? →

Name Link
Latest commit 5952e5e5fb54417d77fdef0f14fc7aaa46710798
Latest deploy log https://app.netlify.com/projects/zio-http/deploys/6835b44109d1510008136fdf

netlify[bot] avatar May 25 '25 23:05 netlify[bot]

image

melmathari avatar May 25 '25 23:05 melmathari

Netlify deploy failed?, and the docs site wasn’t touched in this PR. Can we skip Netlify deploys for PRs? @jdegoes

melmathari avatar May 26 '25 00:05 melmathari

@jdegoes No idea why we're having this test fail, i've addressed the re-ordering of http.method..

[error] --- /home/runner/work/zio-http/zio-http/zio-http/jvm/src/test/scala/zio/http/endpoint/AuthorizationHeaderSpec.scala [error] +++ [error] @@ -3,9 +3,9 @@ [error] import zio._ [error] import zio.test._ [error]
[error] +import zio.http.Method._ [error] import zio.http._ [error] import zio.http.codec._ [error] -import zio.http.Method._ [error]
[error] object AuthorizationHeaderSpec extends ZIOSpecDefault { [error] override def spec = [error] (zioHttpJVM / Test / scalafix) scalafix.sbt.ScalafixFailed: TestError [error] Total time: 34 s, completed May 26, 2025, 12:50:37 AM

Error: Process completed with exit code 1.

`package zio.http.endpoint

import zio._ import zio.test._

import zio.http.Method._ import zio.http._ import zio.http.codec._

object AuthorizationHeaderSpec extends ZIOSpecDefault { override def spec = suite("AuthorizationHeaderSpec")( test("should respond with 401 Unauthorized when required authorization header is missing") { val endpoint = Endpoint(Method.GET / "test") .header(HeaderCodec.authorization) .out[Unit] val route = endpoint.implement(_ => ZIO.unit) val request = Request(method = Method.GET, url = url"/test") for { response <- route.toRoutes.runZIO(request) } yield assertTrue(Status.Unauthorized == response.status) }, ) } `

melmathari avatar May 26 '25 07:05 melmathari

@kyri-petrou Appreciate if you can review this, i'm not sure I have to do anything with the failed website build?

melmathari avatar May 27 '25 10:05 melmathari

@kyri-petrou is anything needed from my end? Thanks! Looking forward to contributing more to ZIO.

melmathari avatar May 27 '25 22:05 melmathari

Hey guys, can I do anything else on this PR to avoid failing redirect rules?

melmathari avatar May 31 '25 19:05 melmathari