Skip to content

Commit d88338f

Browse files
authored
fix: tolerate trailing semicolons in parameter parsing (#618) (#804)
Headers like `Content-Disposition: attachment; filename="test.html";` with a trailing semicolon are technically valid and accepted by browsers. Previously this would fail with {error, badarg}. The fix checks if there's only whitespace after a semicolon and if so, returns the accumulated parameters instead of failing.
1 parent 8b0e6b7 commit d88338f

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/hackney_bstr.erl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,16 @@ params(Data, Fun) ->
220220
params(Data, Fun, Acc) ->
221221
whitespace(Data,
222222
fun (<< $;, Rest/binary >>) ->
223-
param(Rest,
224-
fun (Rest2, Attr, Value) ->
225-
params(Rest2, Fun, [{Attr, Value}|Acc])
223+
%% Check for trailing semicolon (no parameter after it)
224+
whitespace(Rest,
225+
fun (<<>>) ->
226+
%% Trailing semicolon with no parameter - just return accumulated params
227+
Fun(<<>>, lists:reverse(Acc));
228+
(Rest2) ->
229+
param(Rest2,
230+
fun (Rest3, Attr, Value) ->
231+
params(Rest3, Fun, [{Attr, Value}|Acc])
232+
end)
226233
end);
227234
(Rest) ->
228235
Fun(Rest, lists:reverse(Acc))

test/hackney_bstr_tests.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ params_test_() ->
8585
{<<";c=d">>, [<<>>,{<<"c">>,<<"d">>}]},
8686
{<<";a=b;c=d">>, [<<>>,{<<"a">>,<<"b">>},{<<"c">>,<<"d">>}]},
8787
{<<";ab">>, {error, badarg}},
88-
{<<";a=b;">>, {error, badarg}},
88+
%% Issue #618: trailing semicolons should be tolerated
89+
{<<";a=b;">>, [<<>>,{<<"a">>,<<"b">>}]},
90+
{<<";a=b;c=d;">>, [<<>>,{<<"a">>,<<"b">>},{<<"c">>,<<"d">>}]},
8991
{<<" ;a=b;c=d">>, [<<>>,{<<"a">>,<<"b">>},{<<"c">>,<<"d">>}]}
9092
],
9193
[{V, fun() -> R = hackney_bstr:params(V, F) end} || {V, R} <- Tests].

0 commit comments

Comments
 (0)