Skip to content

Commit be02d86

Browse files
committed
Do not include any secret padding in the otpauth URI
The IETF draft-linuxgemini-otpauth-uri-02 recommends to not include the padding in Section 3.3.1. cf. https://www.ietf.org/archive/id/draft-linuxgemini-otpauth-uri-02.html#section-3.3.1 (fixes issue #12540) Signed-off-by: Sven Strickroth <email@cs-ware.de>
1 parent 44daca9 commit be02d86

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

src/core/Base32.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ QByteArray Base32::removePadding(const QByteArray& encodedData)
267267
return newEncodedData;
268268
}
269269

270-
QByteArray Base32::sanitizeInput(const QByteArray& encodedData)
270+
QByteArray Base32::sanitizeInput(const QByteArray& encodedData, bool withPadding /* = true */)
271271
{
272272
if (encodedData.size() <= 0) {
273273
return encodedData;
@@ -294,5 +294,8 @@ QByteArray Base32::sanitizeInput(const QByteArray& encodedData)
294294
}
295295
newEncodedData.resize(i);
296296

297+
if (!withPadding)
298+
return removePadding(newEncodedData);
299+
297300
return addPadding(newEncodedData);
298301
}

src/core/Base32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Base32
3535
Q_REQUIRED_RESULT static QByteArray encode(const QByteArray&);
3636
Q_REQUIRED_RESULT static QByteArray addPadding(const QByteArray&);
3737
Q_REQUIRED_RESULT static QByteArray removePadding(const QByteArray&);
38-
Q_REQUIRED_RESULT static QByteArray sanitizeInput(const QByteArray&);
38+
Q_REQUIRED_RESULT static QByteArray sanitizeInput(const QByteArray&, bool withPadding = true);
3939
};
4040

4141
#endif // BASE32_H

src/core/Totp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ QString Totp::writeSettings(const QSharedPointer<Totp::Settings>& settings,
180180
auto urlstring = QString("otpauth://totp/%1:%2?secret=%3&period=%4&digits=%5&issuer=%1")
181181
.arg(title.isEmpty() ? "KeePassXC" : QString(QUrl::toPercentEncoding(title)),
182182
username.isEmpty() ? "none" : QString(QUrl::toPercentEncoding(username)),
183-
QString(QUrl::toPercentEncoding(Base32::sanitizeInput(settings->key.toLatin1()))),
183+
QString(Base32::sanitizeInput(settings->key.toLatin1(), false)),
184184
QString::number(settings->step),
185185
QString::number(settings->digits));
186186

tests/TestTotp.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ void TestTotp::testParseSecret()
107107
QVERIFY(settings.isNull());
108108
}
109109

110+
void TestTotp::testTotpWriteSettings()
111+
{
112+
auto settings1 = Totp::createSettings("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", Totp::DEFAULT_DIGITS, Totp::DEFAULT_STEP);
113+
QCOMPARE(
114+
Totp::writeSettings(settings1, "ACME Co", "john", true),
115+
"otpauth://totp/ACME%20Co:john?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&period=30&digits=6&issuer=ACME%20Co");
116+
117+
auto settings2 = Totp::createSettings("63BEDWCQZKTQWPESARIERL5DTTQFCJTK", 3, 25);
118+
QCOMPARE(
119+
Totp::writeSettings(settings2, "ACME Co", "", true),
120+
"otpauth://totp/ACME%20Co:none?secret=63BEDWCQZKTQWPESARIERL5DTTQFCJTK&period=25&digits=3&issuer=ACME%20Co");
121+
122+
auto settings3 = Totp::createSettings("HXDMVJECJJWSRBY", Totp::DEFAULT_DIGITS, Totp::DEFAULT_STEP);
123+
QCOMPARE(
124+
Totp::writeSettings(settings3, "", "john", true),
125+
"otpauth://totp/KeePassXC:john?secret=HXDMVJECJJWSRBY&period=30&digits=6&issuer=KeePassXC");
126+
}
127+
110128
void TestTotp::testTotpCode()
111129
{
112130
// Test vectors from RFC 6238

tests/TestTotp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class TestTotp : public QObject
2828
private slots:
2929
void initTestCase();
3030
void testParseSecret();
31+
void testTotpWriteSettings();
3132
void testTotpCode();
3233
void testSteamTotp();
3334
void testEntryHistory();

0 commit comments

Comments
 (0)