From eb30d14d3465d400103367be6f5f9d89fa16e105 Mon Sep 17 00:00:00 2001 From: Paul Joseph Davis Date: Thu, 20 Jan 2011 01:15:55 +0000 Subject: Fix bug that allows invalid UTF-8 after valid escapes. Merges r991073 from trunk to branches/1.0.x Fixes COUCHDB-875 git-svn-id: https://svn.apache.org/repos/asf/couchdb/branches/1.0.x@1061088 13f79535-47bb-0310-9956-ffa450edef68 --- src/mochiweb/mochijson2.erl | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/mochiweb/mochijson2.erl') diff --git a/src/mochiweb/mochijson2.erl b/src/mochiweb/mochijson2.erl index 111c37bd..1ed2b88f 100644 --- a/src/mochiweb/mochijson2.erl +++ b/src/mochiweb/mochijson2.erl @@ -405,8 +405,22 @@ tokenize_string(B, S=#decoder{offset=O}, Acc) -> Acc1 = lists:reverse(xmerl_ucs:to_utf8(C), Acc), tokenize_string(B, ?ADV_COL(S, 6), Acc1) end; - <<_:O/binary, C, _/binary>> -> - tokenize_string(B, ?INC_CHAR(S, C), [C | Acc]) + <<_:O/binary, C1, _/binary>> when C1 < 128 -> + tokenize_string(B, ?INC_CHAR(S, C1), [C1 | Acc]); + <<_:O/binary, C1, C2, _/binary>> when C1 >= 194, C1 =< 223, + C2 >= 128, C2 =< 191 -> + tokenize_string(B, ?ADV_COL(S, 2), [C2, C1 | Acc]); + <<_:O/binary, C1, C2, C3, _/binary>> when C1 >= 224, C1 =< 239, + C2 >= 128, C2 =< 191, + C3 >= 128, C3 =< 191 -> + tokenize_string(B, ?ADV_COL(S, 3), [C3, C2, C1 | Acc]); + <<_:O/binary, C1, C2, C3, C4, _/binary>> when C1 >= 240, C1 =< 244, + C2 >= 128, C2 =< 191, + C3 >= 128, C3 =< 191, + C4 >= 128, C4 =< 191 -> + tokenize_string(B, ?ADV_COL(S, 4), [C4, C3, C2, C1 | Acc]); + _ -> + throw(invalid_utf8) end. tokenize_number(B, S) -> @@ -653,7 +667,9 @@ test_input_validation() -> <>, <>, % we don't support code points > 10FFFF per RFC 3629 - <> + <>, + %% escape characters trigger a different code path + <> ], lists:foreach(fun(X) -> ok = try decode(X) catch invalid_utf8 -> ok end -- cgit v1.2.3