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

Ensure that an OTP's issuer and label values are escaped correctly #391

Merged
merged 3 commits into from
Apr 7, 2024
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
17 changes: 11 additions & 6 deletions QRCoder/PayloadGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,7 @@ private void ProcessCommonFields(StringBuilder sb)
}
string strippedSecret = Secret.Replace(" ", "");
string escapedIssuer = null;
string escapedLabel = null;
string label = null;

if (!String40Methods.IsNullOrWhiteSpace(Issuer))
Expand All @@ -2023,18 +2024,22 @@ private void ProcessCommonFields(StringBuilder sb)
escapedIssuer = Uri.EscapeDataString(Issuer);
}

if (!String40Methods.IsNullOrWhiteSpace(Label) && Label.Contains(":"))
if (!String40Methods.IsNullOrWhiteSpace(Label))
{
throw new Exception("Label must not have a ':'");
if (Label.Contains(":"))
{
throw new Exception("Label must not have a ':'");
}
escapedLabel = Uri.EscapeDataString(Label);
}

if (Label != null && Issuer != null)
if (escapedLabel != null && escapedIssuer != null)
{
label = Issuer + ":" + Label;
label = escapedIssuer + ":" + escapedLabel;
}
else if (Issuer != null)
else if (escapedIssuer != null)
{
label = Issuer;
label = escapedIssuer;
}

if (label != null)
Expand Down
35 changes: 33 additions & 2 deletions QRCoderTests/PayloadGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2656,7 +2656,22 @@ public void one_time_password_generator_time_based_generates_with_standard_optio
Label = "test@google.com",
};

pg.ToString().ShouldBe("otpauth://totp/Google:test@google.com?secret=pwq65q55&issuer=Google");
pg.ToString().ShouldBe("otpauth://totp/Google:test%40google.com?secret=pwq65q55&issuer=Google");
}


[Fact]
[Category("PayloadGenerator/OneTimePassword")]
public void one_time_password_generator_time_based_generates_with_standard_options_escapes_issuer_and_label()
{
var pg = new PayloadGenerator.OneTimePassword
{
Secret = "pwq6 5q55",
Issuer = "Google Google",
Label = "test/test@google.com",
};

pg.ToString().ShouldBe("otpauth://totp/Google%20Google:test%2Ftest%40google.com?secret=pwq65q55&issuer=Google%20Google");
}


Expand All @@ -2673,7 +2688,23 @@ public void one_time_password_generator_hmac_based_generates_with_standard_optio
Counter = 500,
};

pg.ToString().ShouldBe("otpauth://hotp/Google:test@google.com?secret=pwq65q55&issuer=Google&counter=500");
pg.ToString().ShouldBe("otpauth://hotp/Google:test%40google.com?secret=pwq65q55&issuer=Google&counter=500");
}

[Fact]
[Category("PayloadGenerator/OneTimePassword")]
public void one_time_password_generator_hmac_based_generates_with_standard_options_escapes_issuer_and_label()
{
var pg = new PayloadGenerator.OneTimePassword
{
Secret = "pwq6 5q55",
Issuer = "Google Google",
Label = "test/test@google.com",
Type = PayloadGenerator.OneTimePassword.OneTimePasswordAuthType.HOTP,
Counter = 500,
};

pg.ToString().ShouldBe("otpauth://hotp/Google%20Google:test%2Ftest%40google.com?secret=pwq65q55&issuer=Google%20Google&counter=500");
}


Expand Down