From 70603d2cccea1485cfe3712de0cfc8b58908b853 Mon Sep 17 00:00:00 2001 From: Daniel Widgren Date: Sun, 15 Nov 2026 20:36:18 +0100 Subject: [PATCH 1/3] fix: answer 404 for a message id that is not a UUID The ETSI block of a message is found by a selector built from the id in the path. An id with a line break, or any other text that is not a UUID, no longer reaches that selector. --- src/controllers/ldf_www_controller.erl | 43 +++++++++++------ test/ldf_www_controller_tests.erl | 64 ++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 test/ldf_www_controller_tests.erl diff --git a/src/controllers/ldf_www_controller.erl b/src/controllers/ldf_www_controller.erl index e9aa645..8f364fc 100644 --- a/src/controllers/ldf_www_controller.erl +++ b/src/controllers/ldf_www_controller.erl @@ -78,19 +78,36 @@ submit_history(Req0) -> %% Convert a stored message to the requested ETSI XML and inject it, pretty %% printed, under its row. Any other format (e.g. "hide") clears the block. -message_etsi(#{bindings := #{~"messageid" := MessageId, ~"format" := Format}}) when - Format =:= ~"103707"; Format =:= ~"103120" --> - Block = - case ldf_db:get_message(MessageId) of - {ok, [#{payload := Payload} | _]} -> - render_etsi(Format, MessageId, safe_decode(Payload)); - _ -> - etsi_error(~"Message not found.") - end, - {datastar, [patch_xml(MessageId, Block)]}; -message_etsi(#{bindings := #{~"messageid" := MessageId}}) -> - {datastar, [patch_xml(MessageId, ~"")]}. +message_etsi(#{bindings := #{~"messageid" := MessageId, ~"format" := Format}}) -> + case is_message_id(MessageId) of + true -> {datastar, [patch_xml(MessageId, etsi_content(Format, MessageId))]}; + false -> {status, 404} + end. + +etsi_content(Format, MessageId) when Format =:= ~"103707"; Format =:= ~"103120" -> + case ldf_db:get_message(MessageId) of + {ok, [#{payload := Payload} | _]} -> + render_etsi(Format, MessageId, safe_decode(Payload)); + _ -> + etsi_error(~"Message not found.") + end; +etsi_content(_Format, _MessageId) -> + ~"". + +is_message_id(<>) -> + [] =:= + [ + Digit + || <> <= <>, + not is_hex_digit(Digit) + ]; +is_message_id(_) -> + false. + +is_hex_digit(Digit) -> + (Digit >= $0 andalso Digit =< $9) orelse + (Digit >= $a andalso Digit =< $f) orelse + (Digit >= $A andalso Digit =< $F). %% --- published by the message API on each intercepted message ------------- diff --git a/test/ldf_www_controller_tests.erl b/test/ldf_www_controller_tests.erl new file mode 100644 index 0000000..b9d27f4 --- /dev/null +++ b/test/ldf_www_controller_tests.erl @@ -0,0 +1,64 @@ +-module(ldf_www_controller_tests). + +-include_lib("eunit/include/eunit.hrl"). + +-define(MESSAGE_ID, ~"1e87fd7c-de4f-4e97-be27-2bdcdbad23b7"). + +hiding_a_message_clears_its_block_test() -> + {datastar, Events} = ldf_www_controller:message_etsi(request(?MESSAGE_ID, ~"hide")), + [Event] = sse_events(iolist_to_binary(Events)), + ?assertEqual([~"#xml-1e87fd7c-de4f-4e97-be27-2bdcdbad23b7"], data(~"selector", Event)), + ?assertEqual([~"inner"], data(~"mode", Event)). + +upper_case_message_id_is_accepted_test() -> + Id = ~"1E87FD7C-DE4F-4E97-BE27-2BDCDBAD23B7", + {datastar, Events} = ldf_www_controller:message_etsi(request(Id, ~"hide")), + [Event] = sse_events(iolist_to_binary(Events)), + ?assertEqual([~"#xml-1E87FD7C-DE4F-4E97-BE27-2BDCDBAD23B7"], data(~"selector", Event)). + +message_id_that_is_not_a_uuid_is_not_found_test_() -> + [ + ?_assertEqual({status, 404}, ldf_www_controller:message_etsi(request(Id, Format))) + || Id <- [ + ~"1e87fd7c-de4f-4e97-be27-2bdcdbad23b7\n", + ~"1e87fd7c\nde4f-4e97-be27-2bdcdbad23b7", + ~"1e87fd7c-de4f-4e97-be27-2bdcdbad23b7\r\nevent: x", + ~"1e87fd7c-de4f-4e97-be27-2bdcdbad23b7, body", + ~"1e87fd7c-de4f-4e97-be27-2bdcdbad23bg", + ~"1e87fd7cde4f4e97be272bdcdbad23b7", + ~"" + ], + Format <- [~"hide", ~"103707", ~"103120"] + ]. + +request(MessageId, Format) -> + #{bindings => #{~"messageid" => MessageId, ~"format" => Format}}. + +%% Reads a stream the way an EventSource client does: a line ends at CRLF, +%% CR or LF, an empty line ends an event, and a line without a colon is a +%% field with an empty value. +sse_events(Stream) -> + events(binary:split(Stream, [~"\r\n", ~"\r", ~"\n"], [global]), []). + +events([], _Unfinished) -> + []; +events([~"" | Lines], []) -> + events(Lines, []); +events([~"" | Lines], Fields) -> + [lists:reverse(Fields) | events(Lines, [])]; +events([Line | Lines], Fields) -> + events(Lines, [field(Line) | Fields]). + +field(Line) -> + case binary:split(Line, ~":") of + [Name, <<$\s, Value/binary>>] -> {Name, Value}; + [Name, Value] -> {Name, Value}; + [Name] -> {Name, ~""} + end. + +-spec data(binary(), [{binary(), binary()}]) -> [binary()]. +data(Kind, Event) -> + [ + Rest + || {~"data", Value} <- Event, [Found, Rest] <- [binary:split(Value, ~" ")], Found =:= Kind + ]. From 97183b5ba802e7039795f30aeb1f7e037e3dc724 Mon Sep 17 00:00:00 2001 From: Daniel Widgren Date: Sun, 15 Nov 2026 20:36:18 +0100 Subject: [PATCH 2/3] fix: pin datastar v0.1.0 A carriage return or CRLF in a rendered row now ends a line as a line feed does, so the row reaches the browser whole. --- rebar.config | 2 +- rebar.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rebar.config b/rebar.config index 77cba3b..98ebffa 100644 --- a/rebar.config +++ b/rebar.config @@ -16,7 +16,7 @@ {deps, [ {kura, {git, "https://github.com/Taure/kura.git", {tag, "v2.20.12"}}}, {kura_postgres, {git, "https://github.com/Taure/kura_postgres.git", {tag, "v1.1.3"}}}, - {datastar, {git, "https://github.com/Taure/datastar.git", {branch, "main"}}}, + {datastar, {git, "https://github.com/Taure/datastar.git", {tag, "v0.1.0"}}}, {datastar_nova, {git, "https://github.com/Taure/datastar_nova.git", {tag, "v0.1.0"}}}, nova ]}. diff --git a/rebar.lock b/rebar.lock index 86642b1..a744455 100644 --- a/rebar.lock +++ b/rebar.lock @@ -3,7 +3,7 @@ {<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.19.0">>},2}, {<<"datastar">>, {git,"https://github.com/Taure/datastar.git", - {ref,"fcc11f399294b305620a7c99ed633b898ce3889b"}}, + {ref,"b1f08c5a4998aab33389eef1788619110829bb5b"}}, 0}, {<<"datastar_nova">>, {git,"https://github.com/Taure/datastar_nova.git", From 630e7edabf645d3103021a7f2699123d68c69e95 Mon Sep 17 00:00:00 2001 From: Daniel Widgren Date: Sun, 15 Nov 2026 20:36:18 +0100 Subject: [PATCH 3/3] test: line breaks in a message text stay inside the row event --- test/ldf_www_controller_tests.erl | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/ldf_www_controller_tests.erl b/test/ldf_www_controller_tests.erl index b9d27f4..c63869d 100644 --- a/test/ldf_www_controller_tests.erl +++ b/test/ldf_www_controller_tests.erl @@ -1,6 +1,7 @@ -module(ldf_www_controller_tests). -include_lib("eunit/include/eunit.hrl"). +-include_lib("nova/include/nova_pubsub.hrl"). -define(MESSAGE_ID, ~"1e87fd7c-de4f-4e97-be27-2bdcdbad23b7"). @@ -31,6 +32,31 @@ message_id_that_is_not_a_uuid_is_not_found_test_() -> Format <- [~"hide", ~"103707", ~"103120"] ]. +line_breaks_in_a_message_text_stay_inside_the_row_event_test() -> + ok = nova_pubsub:start(), + ok = nova_pubsub:join(ldf_messages), + Message = #{~"id" => ?MESSAGE_ID, ~"payload" => ~"one\r\ntwo\rthree\nfour"}, + ldf_www_controller:publish_message(?MESSAGE_ID, iolist_to_binary(json:encode(Message))), + Stream = + receive + #nova_pubsub{channel = ldf_messages, payload = Events} -> iolist_to_binary(Events) + after 1000 -> error(nothing_published) + end, + nova_pubsub:leave(ldf_messages), + [Placeholder, Row] = sse_events(Stream), + ?assertEqual([~"data", ~"event"], field_names(Placeholder)), + ?assertEqual([~"#feed-empty"], data(~"selector", Placeholder)), + ?assertEqual([~"data", ~"event"], field_names(Row)), + ?assertEqual([~"#messages"], data(~"selector", Row)), + ?assertEqual([~"append"], data(~"mode", Row)), + Html = elements(Row), + ?assertMatch({0, _}, binary:match(Html, ~"
")), + ?assertMatch( + {_, _}, + binary:match(Html, ~"
one\ntwo\nthree\nfour
") + ), + ?assertEqual(~"class=\"etsi\">
", binary:part(Html, byte_size(Html), -25)). + request(MessageId, Format) -> #{bindings => #{~"messageid" => MessageId, ~"format" => Format}}. @@ -56,9 +82,17 @@ field(Line) -> [Name] -> {Name, ~""} end. +field_names(Event) -> + lists:usort([Name || {Name, _Value} <- Event]). + -spec data(binary(), [{binary(), binary()}]) -> [binary()]. data(Kind, Event) -> [ Rest || {~"data", Value} <- Event, [Found, Rest] <- [binary:split(Value, ~" ")], Found =:= Kind ]. + +-spec elements([{binary(), binary()}]) -> binary(). +elements(Event) -> + Data = iolist_to_binary([[Line, $\n] || Line <- data(~"elements", Event)]), + binary:part(Data, 0, byte_size(Data) - 1).