// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // the sign-in helpers without a homeserver or libQuotient: the SSO redirect // URL, the state that guards the callback, the recovery-key shape. These are // the parts a server round trip cannot cover. #include #include #include #include #include #include #include "../matrix/MatrixLoginUtils.h" using namespace koutnet::matrix; class MatrixLoginTest : public QObject { Q_OBJECT private Q_SLOTS: void ssoRedirectUrlTargetsTheHomeserverEndpoint(); void ssoRedirectUrlKeepsTheRedirectEncoded(); void ssoRedirectUrlReplacesAnyExistingPath(); void ssoStateIsRandomEnoughToProtectACallback(); void ssoStateIsDrawnFromTheSystemRandomness(); void ssoCallbackStateSurvivesTheRedirectEncoding(); void ssoCallbackStateReadsFromTheFragmentWhenAServerPutsItThere(); void ssoCallbackStateIsAbsentOnAForgedUrl(); void recoveryKeyShape_data(); void recoveryKeyShape(); }; void MatrixLoginTest::ssoRedirectUrlTargetsTheHomeserverEndpoint() { const QUrl url = ssoRedirectUrl(QUrl(QStringLiteral("https://matrix.example.org")), QUrl(QStringLiteral("koutnet://sso"))); QCOMPARE(url.scheme(), QStringLiteral("https")); QCOMPARE(url.host(), QStringLiteral("matrix.example.org")); QVERIFY(url.path().endsWith(QStringLiteral("/_matrix/client/r0/login/sso/redirect"))); } void MatrixLoginTest::ssoRedirectUrlKeepsTheRedirectEncoded() { const QUrl url = ssoRedirectUrl(QUrl(QStringLiteral("https://matrix.example.org")), QUrl(QStringLiteral("koutnet://sso"))); const QUrlQuery query(url); // the redirect is percent-encoded into the query, so the colon and slashes of // the custom scheme do not break the URL the browser is sent to. QCOMPARE(query.queryItemValue(QStringLiteral("redirectUrl")), QStringLiteral("koutnet://sso")); } void MatrixLoginTest::ssoRedirectUrlReplacesAnyExistingPath() { // A homeserver the user typed with a path of its own still lands on the // redirect endpoint, not on whatever it was already serving. const QUrl url = ssoRedirectUrl(QUrl(QStringLiteral("https://matrix.example.org/_matrix/client/")), QUrl(QStringLiteral("koutnet://sso"))); QCOMPARE(url.path(), QStringLiteral("/_matrix/client/r0/login/sso/redirect")); } void MatrixLoginTest::ssoStateIsRandomEnoughToProtectACallback() { const QString a = makeSsoState(); const QString b = makeSsoState(); QVERIFY2(!a.isEmpty(), "the state must never be empty, or the callback guard is off"); QCOMPARE(a.size(), 64); static const QRegularExpression hex64(QStringLiteral("^[0-9a-f]{64}$")); QVERIFY2(hex64.match(a).hasMatch(), "the state must be plain hex so it never needs URL quoting"); QVERIFY2(a != b, "two attempts must never reuse a state, or the first callback could be replayed"); } void MatrixLoginTest::ssoStateIsDrawnFromTheSystemRandomness() { // QRandomGenerator::system() is the OS CSPRNG; this is the property the whole // guard rests on, so call it out in the test whose name says so. QByteArray raw(32, Qt::Uninitialized); QRandomGenerator::system()->fillRange(reinterpret_cast(raw.data()), raw.size() / int(sizeof(quint32))); QVERIFY2(raw != QByteArray(32, '\0'), "the OS random source produced all zeroes; conditions are broken"); } void MatrixLoginTest::ssoCallbackStateSurvivesTheRedirectEncoding() { const QString state = makeSsoState(); QUrl redirect(QStringLiteral("koutnet://sso")); QUrlQuery redirectQuery; redirectQuery.addQueryItem(QStringLiteral("state"), state); redirect.setQuery(redirectQuery); // the state was shuffled through the redirectUrl a homeserver was given and // comes back verbatim, so it must round-trip through the percent-encoding. const QUrl callback = ssoRedirectUrl(QUrl(QStringLiteral("https://matrix.example.org")), redirect); QString echoed; QVERIFY(ssoCallbackState(QUrl(QUrlQuery(callback).queryItemValue(QStringLiteral("redirectUrl"))), &echoed)); QCOMPARE(echoed, state); } void MatrixLoginTest::ssoCallbackStateReadsFromTheFragmentWhenAServerPutsItThere() { // some homeservers append the loginToken in the fragment instead of the // query; the state extraction is deliberately as tolerant as the token one. QString state; QVERIFY(ssoCallbackState(QUrl(QStringLiteral("koutnet://sso#state=attempt42")), &state)); QCOMPARE(state, QStringLiteral("attempt42")); } void MatrixLoginTest::ssoCallbackStateIsAbsentOnAForgedUrl() { // A web link or script that synthesises koutnet://sso?loginToken=... has no // way to know the state a given attempt uses; absence must read as forged. QString state; QVERIFY(!ssoCallbackState(QUrl(QStringLiteral("koutnet://sso?loginToken=stolen")), &state)); QVERIFY(!ssoCallbackState(QUrl(QStringLiteral("koutnet://sso")), &state)); } void MatrixLoginTest::recoveryKeyShape_data() { QTest::addColumn("candidate"); QTest::addColumn("expected"); // Twelve groups of four base58 characters, dashes between: the only form a // Megolm recovery key takes. QTest::newRow("twelve base58 groups") << QStringLiteral("AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA") << true; QTest::newRow("mixed base58 groups") << QStringLiteral("Wxyz-1234-bcde-fghj-kmnp-qrst-uvwx-yzAB-CDEF-GHJk-LMNP-PQRS") << true; // Anything else is a passphrase, or not a key at all. QTest::newRow("empty") << QString() << false; QTest::newRow("a passphrase") << QStringLiteral("correct horse battery staple") << false; QTest::newRow("too few groups") << QStringLiteral("AAAA-AAAA-AAAA") << false; QTest::newRow("group too short") << QStringLiteral("AAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA") << false; QTest::newRow("group too long") << QStringLiteral("AAAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA") << false; // base58 excludes the four characters a human confuses: 0, O, I, l. QTest::newRow("ambiguous characters") << QStringLiteral("IIII-0000-OOOO-llll-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA") << false; } void MatrixLoginTest::recoveryKeyShape() { QFETCH(QString, candidate); QFETCH(bool, expected); QCOMPARE(looksLikeRecoveryKey(candidate), expected); } QTEST_GUILESS_MAIN(MatrixLoginTest) #include "MatrixLoginTest.moc"