Skip to content

Commit 0fd25bc

Browse files
committed
Also escape label
1 parent 32c4aa0 commit 0fd25bc

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

QRCoder/PayloadGenerator.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,7 @@ private void ProcessCommonFields(StringBuilder sb)
20122012
}
20132013
string strippedSecret = Secret.Replace(" ", "");
20142014
string escapedIssuer = null;
2015+
string escapedLabel = null;
20152016
string label = null;
20162017

20172018
if (!String40Methods.IsNullOrWhiteSpace(Issuer))
@@ -2023,14 +2024,18 @@ private void ProcessCommonFields(StringBuilder sb)
20232024
escapedIssuer = Uri.EscapeDataString(Issuer);
20242025
}
20252026

2026-
if (!String40Methods.IsNullOrWhiteSpace(Label) && Label.Contains(":"))
2027+
if (!String40Methods.IsNullOrWhiteSpace(Label))
20272028
{
2028-
throw new Exception("Label must not have a ':'");
2029+
if (Label.Contains(":"))
2030+
{
2031+
throw new Exception("Label must not have a ':'");
2032+
}
2033+
escapedLabel = Uri.EscapeDataString(Label);
20292034
}
20302035

2031-
if (Label != null && escapedIssuer != null)
2036+
if (escapedLabel != null && escapedIssuer != null)
20322037
{
2033-
label = escapedIssuer + ":" + Label;
2038+
label = escapedIssuer + ":" + escapedLabel;
20342039
}
20352040
else if (escapedIssuer != null)
20362041
{

QRCoderTests/PayloadGeneratorTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,22 +2656,22 @@ public void one_time_password_generator_time_based_generates_with_standard_optio
26562656
Label = "[email protected]",
26572657
};
26582658

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

26622662

26632663
[Fact]
26642664
[Category("PayloadGenerator/OneTimePassword")]
2665-
public void one_time_password_generator_time_based_generates_with_standard_options_escapes_issuer()
2665+
public void one_time_password_generator_time_based_generates_with_standard_options_escapes_issuer_and_label()
26662666
{
26672667
var pg = new PayloadGenerator.OneTimePassword
26682668
{
26692669
Secret = "pwq6 5q55",
26702670
Issuer = "Google Google",
2671-
Label = "[email protected]",
2671+
Label = "test/test@google.com",
26722672
};
26732673

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

26772677

@@ -2688,23 +2688,23 @@ public void one_time_password_generator_hmac_based_generates_with_standard_optio
26882688
Counter = 500,
26892689
};
26902690

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

26942694
[Fact]
26952695
[Category("PayloadGenerator/OneTimePassword")]
2696-
public void one_time_password_generator_hmac_based_generates_with_standard_options_escapes_issuer()
2696+
public void one_time_password_generator_hmac_based_generates_with_standard_options_escapes_issuer_and_label()
26972697
{
26982698
var pg = new PayloadGenerator.OneTimePassword
26992699
{
27002700
Secret = "pwq6 5q55",
27012701
Issuer = "Google Google",
2702-
Label = "[email protected]",
2702+
Label = "test/test@google.com",
27032703
Type = PayloadGenerator.OneTimePassword.OneTimePasswordAuthType.HOTP,
27042704
Counter = 500,
27052705
};
27062706

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

27102710

0 commit comments

Comments
 (0)