Page MenuHomePhorge

D343.1785977372.diff
No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None

D343.1785977372.diff

diff --git a/src/crypto/crypto.cpp b/src/crypto/crypto.cpp
--- a/src/crypto/crypto.cpp
+++ b/src/crypto/crypto.cpp
@@ -353,14 +353,18 @@
MaybeString Crypto::decrypt(nlohmann::json eventJson)
{
- auto content = eventJson.at("content");
- auto algo = content.contains("algorithm") ? content.at("algorithm").template get<std::string>() : std::string();
- if (algo == olmAlgo) {
- return m_d->decryptOlm(std::move(content));
- } else if (algo == megOlmAlgo) {
- return m_d->decryptMegOlm(eventJson);
+ try {
+ auto content = eventJson.at("content");
+ auto algo = content.contains("algorithm") ? content.at("algorithm").template get<std::string>() : std::string();
+ if (algo == olmAlgo) {
+ return m_d->decryptOlm(std::move(content));
+ } else if (algo == megOlmAlgo) {
+ return m_d->decryptMegOlm(eventJson);
+ }
+ return NotBut("Algorithm " + algo + " not supported");
+ } catch (const std::exception &e) {
+ return NotBut("Malformed event");
}
- return NotBut("Algorithm " + algo + " not supported");
}
bool Crypto::createInboundGroupSession(KeyOfGroupSession k, std::string sessionKey, std::string ed25519Key)
diff --git a/src/tests/crypto-test.cpp b/src/tests/crypto-test.cpp
--- a/src/tests/crypto-test.cpp
+++ b/src/tests/crypto-test.cpp
@@ -813,3 +813,33 @@
REQUIRE(k == KeyOfGroupSession{"!someroom:example.com", "some-session-id"});
}
}
+
+TEST_CASE("Crypto::decrypt() should not throw when event is malformed")
+{
+ Crypto a(RandomTag{}, genRandomData(Crypto::constructRandomSize()));
+ SECTION("event with no content") {
+ auto res = a.decrypt(json::object());
+ REQUIRE(!res.has_value());
+ REQUIRE(res.reason() == "Malformed event");
+ }
+
+ SECTION("decrypt malformed olm") {
+ auto res = a.decrypt(json{
+ {"content", {
+ {"algorithm", "m.olm.v1.curve25519-aes-sha2"},
+ }},
+ });
+ REQUIRE(!res.has_value());
+ REQUIRE(res.reason() == "Malformed event");
+ }
+
+ SECTION("decrypt malformed megolm") {
+ auto res = a.decrypt(json{
+ {"content", {
+ {"algorithm", "m.megolm.v1.aes-sha2"},
+ }},
+ });
+ REQUIRE(!res.has_value());
+ REQUIRE(res.reason() == "Malformed event");
+ }
+}

File Metadata

Mime Type
text/plain
Expires
Wed, Aug 5, 5:49 PM (23 h, 13 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1721711
Default Alt Text
D343.1785977372.diff (2 KB)

Event Timeline