Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: deserializing malformed basic auth token results in java.lang.IllegalArgumentException raised outside of F #7283

Closed
grouzen opened this issue Sep 19, 2023 · 4 comments
Assignees
Labels
bug Determined to be a bug in http4s good first issue A tractable issue for those looking to make an initial contribution

Comments

@grouzen
Copy link
Contributor

grouzen commented Sep 19, 2023

The exception is being raised when calling BasicCredentials.unapply method in pattern matching statement at https://github.com/http4s/http4s/blob/series/0.23/server/shared/src/main/scala/org/http4s/server/middleware/authentication/BasicAuth.scala#L69

It causes the situation when you need to catch an exception using try/catch instead of leveraging cats-effect machinery.

java.lang.IllegalArgumentException: Input byte array has incorrect ending byte at 132
	at java.base/java.util.Base64$Decoder.decode0(Base64.java:774)
	at java.base/java.util.Base64$Decoder.decode(Base64.java:538)
	at java.base/java.util.Base64$Decoder.decode(Base64.java:561)
	at org.http4s.BasicCredentials$.apply(Credentials.scala:106)
	at org.http4s.BasicCredentials$.unapply(Credentials.scala:118)
	at org.http4s.server.middleware.authentication.BasicAuth$.validatePassword(BasicAuth.scala:69)
	at org.http4s.server.middleware.authentication.BasicAuth$.$anonfun$challenge$1(BasicAuth.scala:57)

I think it should be considered a bug.

@armanbilge armanbilge added the bug Determined to be a bug in http4s label Sep 19, 2023
@armanbilge
Copy link
Member

Thanks. I guess the problem is here, this method is definitely not total if it's expecting a Base64 string.

def apply(token: String): BasicCredentials = {
val bytes = Base64.getDecoder.decode(token)

@armanbilge armanbilge added the good first issue A tractable issue for those looking to make an initial contribution label Sep 19, 2023
@grouzen
Copy link
Contributor Author

grouzen commented Sep 19, 2023

Yes, indeed. I would like to fix it. Do you have any idea of how to do this properly?
I believe we can't change the return type of the existing apply method as it leads to binary incompatibility.

@armanbilge
Copy link
Member

Awesome, thanks! I guess we can deprecate the apply method and replace it with a fromString that either turns Either or Option. I think we use a similar API style in other places.

@grouzen
Copy link
Contributor Author

grouzen commented Sep 19, 2023

Great! I'll fix it then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Determined to be a bug in http4s good first issue A tractable issue for those looking to make an initial contribution
Projects
None yet
Development

No branches or pull requests

2 participants