Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.

Commit 91a704e

Browse files
authored
Hardened message handling by adding handling for StringConversionException and std::exception in message_callback of OCPP1.6 (#1018)
Signed-off-by: Piet Gömpel <[email protected]>
1 parent 63e25cd commit 91a704e

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

lib/ocpp/v16/charge_point_impl.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,10 @@ void ChargePointImpl::message_callback(const std::string& message) {
12881288
EVLOG_error << "runtime_error during reception of message: " << e.what();
12891289
this->message_dispatcher->dispatch_call_error(CallError(MessageId("-1"), "GenericError", e.what(), json({})));
12901290
return;
1291+
} catch (const std::exception& e) {
1292+
EVLOG_error << "Exception during reception of message: " << e.what();
1293+
this->message_dispatcher->dispatch_call_error(CallError(MessageId("-1"), "GenericError", e.what(), json({})));
1294+
return;
12911295
}
12921296

12931297
auto json_message = enhanced_message.message;
@@ -1366,8 +1370,8 @@ void ChargePointImpl::message_callback(const std::string& message) {
13661370
}
13671371
} catch (json::exception& e) {
13681372
EVLOG_error << "JSON exception during handling of message: " << e.what();
1369-
this->securityEventNotification(ocpp::security_events::INVALIDMESSAGES, std::optional<CiString<255>>(message),
1370-
true);
1373+
this->securityEventNotification(ocpp::security_events::INVALIDMESSAGES,
1374+
CiString<255>(message, StringTooLarge::Truncate), true);
13711375
if (enhanced_message.messageTypeId != MessageTypeId::CALL) {
13721376
return; // CALLERROR shall only follow on a CALL message
13731377
}
@@ -1377,13 +1381,29 @@ void ChargePointImpl::message_callback(const std::string& message) {
13771381
}
13781382
} catch (const EnumConversionException& e) {
13791383
EVLOG_error << "EnumConversionException during handling of message: " << e.what();
1380-
this->securityEventNotification(ocpp::security_events::INVALIDMESSAGES, std::optional<CiString<255>>(message),
1381-
true);
1384+
this->securityEventNotification(ocpp::security_events::INVALIDMESSAGES,
1385+
CiString<255>(message, StringTooLarge::Truncate), true);
1386+
if (enhanced_message.messageTypeId != MessageTypeId::CALL) {
1387+
return; // CALLERROR shall only follow on a CALL message
1388+
}
1389+
auto call_error = CallError(enhanced_message.uniqueId, "FormationViolation", e.what(), json({}, true));
1390+
this->message_dispatcher->dispatch_call_error(call_error);
1391+
} catch (const StringConversionException& e) {
1392+
EVLOG_error << "StringConversionException during handling of message: " << e.what();
1393+
this->securityEventNotification(ocpp::security_events::INVALIDMESSAGES,
1394+
CiString<255>(message, StringTooLarge::Truncate), true);
13821395
if (enhanced_message.messageTypeId != MessageTypeId::CALL) {
13831396
return; // CALLERROR shall only follow on a CALL message
13841397
}
13851398
auto call_error = CallError(enhanced_message.uniqueId, "FormationViolation", e.what(), json({}, true));
13861399
this->message_dispatcher->dispatch_call_error(call_error);
1400+
} catch (const std::exception& e) {
1401+
EVLOG_error << "Exception during handling of message: " << e.what();
1402+
if (enhanced_message.messageTypeId != MessageTypeId::CALL) {
1403+
return; // CALLERROR shall only follow on a CALL message
1404+
}
1405+
auto call_error = CallError(enhanced_message.uniqueId, "GenericError", e.what(), json({}, true));
1406+
this->message_dispatcher->dispatch_call_error(call_error);
13871407
}
13881408
}
13891409

0 commit comments

Comments
 (0)