You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2024/08/25 23:56:17 UTC
(couchdb) branch shutdown-invalid-path created (now b4f396b40)
This is an automated email from the ASF dual-hosted git repository.
jaydoane pushed a change to branch shutdown-invalid-path
in repository https://gitbox.apache.org/repos/asf/couchdb.git
at b4f396b40 Abort response when path doesn't start with slash
This branch includes the following new commits:
new b4f396b40 Abort response when path doesn't start with slash
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(couchdb) 01/01: Abort response when path doesn't start with slash
Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
jaydoane pushed a commit to branch shutdown-invalid-path
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit b4f396b40112629fc272fa2f0892f332abce81ee
Author: Jay Doane <ja...@apache.org>
AuthorDate: Sun Aug 25 16:56:04 2024 -0700
Abort response when path doesn't start with slash
Currently a path that doesn't begin with a `/` causes a no match error
and crash. This can fill up logs with unwanted junk, and be generally
annoying.
```
2024-08-23 17:54:26.603 08/23 17:54:26.603 somehost db13 - CRASH REPORT Process (<0.19262.7388>) with 0 neighbors crashed with reason: no match of right hand value {"..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\windows\\win.ini",[],[]} at chttpd:handle_request_int/1(line:239) <= mochiweb_http:headers/6(line:140) <= proc_lib:init_p_do_apply/3(line:240); initial_call: {mochiweb_acceptor,init,['Argument__1','Argument__2',...]}, ancestors: [chttpd,chttpd_sup,<0.1261.0>], message [...]
```
This adds a check for path validity, and exits with a shutdown if the
path is invalid.
---
src/chttpd/src/chttpd.erl | 9 +++-
src/chttpd/test/eunit/chttpd_invalid_path_test.erl | 49 ++++++++++++++++++++++
2 files changed, 57 insertions(+), 1 deletion(-)
diff --git a/src/chttpd/src/chttpd.erl b/src/chttpd/src/chttpd.erl
index 0c1380862..5ee6fa824 100644
--- a/src/chttpd/src/chttpd.erl
+++ b/src/chttpd/src/chttpd.erl
@@ -236,7 +236,14 @@ handle_request_int(MochiReq) ->
% for the path, use the raw path with the query string and fragment
% removed, but URL quoting left intact
RawUri = MochiReq:get(raw_path),
- {"/" ++ Path, _, _} = mochiweb_util:urlsplit_path(RawUri),
+ {Path0, _, _} = mochiweb_util:urlsplit_path(RawUri),
+ Path = case Path0 of
+ "/" ++ ValidPath ->
+ ValidPath;
+ InvalidPath ->
+ couch_log:info("~p shutdown from invalid path ~p", [?MODULE, InvalidPath]),
+ exit({shutdown, invalid_path})
+ end,
% get requested path
RequestedPath =
diff --git a/src/chttpd/test/eunit/chttpd_invalid_path_test.erl b/src/chttpd/test/eunit/chttpd_invalid_path_test.erl
new file mode 100644
index 000000000..145451856
--- /dev/null
+++ b/src/chttpd/test/eunit/chttpd_invalid_path_test.erl
@@ -0,0 +1,49 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+% http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(chttpd_invalid_path_test).
+
+-include_lib("couch/include/couch_eunit.hrl").
+
+setup_fe() ->
+ meck:expect(couch_log, info, 2, ok),
+ Addr = config:get("chttpd", "bind_address", "127.0.0.1"),
+ Port = mochiweb_socket_server:get(chttpd, port),
+ {Addr, Port}.
+
+teardown_fe(_) ->
+ meck:unload().
+
+invalid_path_test_() ->
+ {
+ "chttpd invalid path test",
+ {
+ setup,
+ fun chttpd_test_util:start_couch/0,
+ fun chttpd_test_util:stop_couch/1,
+ {
+ foreach,
+ fun setup_fe/0,
+ fun teardown_fe/1,
+ [
+ ?TDEF_FE(shutdown_on_invalid_path)
+ ]
+ }
+ }
+ }.
+
+shutdown_on_invalid_path({Addr, Port}) ->
+ {ok, Socket} = gen_tcp:connect(Addr, Port, [binary, {active,true}]),
+ ok = gen_tcp:send(Socket, "GET badpath HTTP/1.0\n\n"),
+ ok = gen_tcp:close(Socket),
+ ok = meck:wait(couch_log, info, '_', _TimeoutMillisec = 1000),
+ ?assert(meck:called(couch_log, info, ['_', [chttpd, "badpath"]])).