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

URL Encoding bug #68

Closed
arigon opened this issue Dec 23, 2021 · 4 comments
Closed

URL Encoding bug #68

arigon opened this issue Dec 23, 2021 · 4 comments

Comments

@arigon
Copy link

arigon commented Dec 23, 2021

Hello

if the issuer contains a space, the URL encoder will mix up the URL.

key, err := totp.Generate(totp.GenerateOpts{
	Issuer:      "Test Issuer",
	AccountName: "my@email.com",
})

Actual result:
otpauth://totp/Test%20Issuer:my@email.com?algorithm=SHA1&digits=6&issuer=Test+Issuer&period=30&secret=QE2C7JXZB3TY3FBKL6PB7PZXP7UCRPOA

otp_issue

Expected result:
otpauth://totp/Test%20Issuer:my@email.com?algorithm=SHA1&digits=6&issuer=Test%20Issuer&period=30&secret=QE2C7JXZB3TY3FBKL6PB7PZXP7UCRPOA

otp_issue_expected

@pquerna
Copy link
Owner

pquerna commented Jan 3, 2022

So, we are just using standard golang url.Values{} and the Encode method. This uses a + as to encode a Space. This is a valid encoding -- alternatively, using %20 is also valid. maybe call this upstream golang/go#4013

Do you have a Client that isn't supporting the + encoding for spaces?

@arigon
Copy link
Author

arigon commented Jan 4, 2022

I saw this golang issue as well. The client I made the screenshots with is Google Authenticator. My workaround is to concat the URL and encode each parameter.

@arigon
Copy link
Author

arigon commented Jan 9, 2022

I will explain the differences of both encodings used by net.URL. Go net.URL has two different encoding functions: url.PathEscape() and url.QueryEscape(). PathEscape will replace a space with %20, QueryEscape will replace a space with +.
The Path of the OTP URL is escaped by PathEscape (space -> %20) and the parameters are escaped by QueryEscape (space -> +).

I'm building my own URL by concating the user defined values and only use PathEscape().

func buildOTPURL(issuer, email, algorithm, digits string, key *otp.Key) string {
	otpURL := "otpauth://totp/"
	otpURL += url.PathEscape(issuer)
	otpURL += ":"
	otpURL += url.PathEscape(email)
	otpURL += "?"
	otpURL += "algorithm=" + algorithm
	otpURL += "&digits=" + digits
	otpURL += "&issuer=" + url.PathEscape(issuer)
	otpURL += "&period=" + fmt.Sprint(key.Period())
	otpURL += "&secret=" + key.Secret()

	return otpURL
}

@pquerna
Copy link
Owner

pquerna commented Dec 14, 2022

Fixed by #78 and the v1.4 release https://github.com/pquerna/otp/releases/tag/v1.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants