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

Fixes #7283: make BasicCredentials constructor safe #7284

Merged
merged 3 commits into from Sep 23, 2023

Conversation

grouzen
Copy link
Contributor

@grouzen grouzen commented Sep 19, 2023

This PR provides a fix for #7283 by replacing the existing BasicCredentials.apply constructor with BasicCredentials.fromString and using it in BasicCredentials.unapply to make pattern matching safe too.

@mergify mergify bot added series/0.23 PRs targeting 0.23.x module:core labels Sep 19, 2023
case ix => apply(userPass.substring(0, ix), userPass.substring(ix + 1), charset)
@deprecated("Use fromString instead", "0.23.24")
def apply(token: String): BasicCredentials =
fromString(token).get
Copy link
Member

Choose a reason for hiding this comment

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

If I'm not wrong, previously there was an IllegalArgumentException, and now it will be NoSuchElementException. Although the difference is subtle, shouldn't we preserve the behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed this one.

userPass.indexOf(':') match {
case -1 => apply(userPass, "", charset)
case ix => apply(userPass.substring(0, ix), userPass.substring(ix + 1), charset)
private def fromString0(token: String): Try[BasicCredentials] =
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you have any suggestions for a better name?

Copy link
Member

Choose a reason for hiding this comment

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

Personally I use Impl but it's just a private method, I don't really care :)

Suggested change
private def fromString0(token: String): Try[BasicCredentials] =
private def fromStringImpl(token: String): Try[BasicCredentials] =

Copy link
Member

@armanbilge armanbilge left a comment

Choose a reason for hiding this comment

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

Thanks for iterating!

userPass.indexOf(':') match {
case -1 => apply(userPass, "", charset)
case ix => apply(userPass.substring(0, ix), userPass.substring(ix + 1), charset)
private def fromString0(token: String): Try[BasicCredentials] =
Copy link
Member

Choose a reason for hiding this comment

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

Personally I use Impl but it's just a private method, I don't really care :)

Suggested change
private def fromString0(token: String): Try[BasicCredentials] =
private def fromStringImpl(token: String): Try[BasicCredentials] =

@armanbilge armanbilge merged commit b5f05dc into http4s:series/0.23 Sep 23, 2023
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module:core series/0.23 PRs targeting 0.23.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants