summaryrefslogtreecommitdiff
path: root/src/chttpd/test/chttpd_security_tests.erl
blob: b80238c7880c109a3263690e2e5c1961021d654f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
% 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_security_tests).

-include_lib("couch/include/couch_eunit.hrl").
-include_lib("couch/include/couch_db.hrl").

-define(USER, "chttpd_db_test_admin").
-define(PASS, "pass").
-define(AUTH, {basic_auth, {?USER, ?PASS}}).

-define(TEST_MEMBER, "test_member").
-define(TEST_MEMBER_PASS, "test_member_pass").
-define(TEST_MEMBER_AUTH, {basic_auth, {?TEST_MEMBER, ?TEST_MEMBER_PASS}}).

-define(TEST_ADMIN, "test_admin").
-define(TEST_ADMIN_PASS, "test_admin_pass").
-define(TEST_ADMIN_AUTH, {basic_auth, {?TEST_ADMIN, ?TEST_ADMIN_PASS}}).



-define(CONTENT_JSON, {"Content-Type", "application/json"}).
-define(FIXTURE_TXT, ?ABS_PATH(?FILE)).

setup() ->
    Hashed = couch_passwords:hash_admin_password(?PASS),
    Persist = false,
    ok = config:set("admins", ?USER, ?b2l(Hashed), Persist),
    UserDb = ?tempdb(),
    TmpDb = ?tempdb(),
    ok = config:set("chttpd_auth", "authentication_db", ?b2l(UserDb), Persist),

    Addr = config:get("chttpd", "bind_address", "127.0.0.1"),
    Port = mochiweb_socket_server:get(chttpd, port),
    BaseUrl = lists:concat(["http://", Addr, ":", Port, "/"]),
    Url = lists:concat([BaseUrl, ?b2l(TmpDb)]),
    UsersUrl = lists:concat([BaseUrl, ?b2l(UserDb)]),
    create_db(UsersUrl),
    create_db(Url),
    create_design_doc(Url),
    create_user(UsersUrl,?TEST_MEMBER,?TEST_MEMBER_PASS,[<<?TEST_MEMBER>>]),
    create_user(UsersUrl,?TEST_ADMIN,?TEST_ADMIN_PASS,[<<?TEST_ADMIN>>]),
    set_security(Url),
    [Url, UsersUrl].

teardown([Url,UsersUrl]) ->
    delete_db(Url),
    delete_db(UsersUrl),
    ok = config:delete("admins", ?USER, _Persist=false).

create_db(Url) ->
    {ok, Status, _, _} = test_request:put(Url, [?CONTENT_JSON, ?AUTH], "{}"),
    ?assert(Status =:= 201 orelse Status =:= 202).

create_design_doc(Url) ->
    {ok, Status, _, _} = test_request:put(lists:concat([Url, '/_design/test']), [?CONTENT_JSON, ?AUTH],
            "{\"id\":\"_design/test\"}"),
    ?assert(Status =:= 201 orelse Status =:= 202).

set_security(Url) ->
    SecurityUrl = lists:concat([Url, "/_security"]),
    SecurityProperties = [
        {<<"admins">>,{[{<<"roles">>,[<<?TEST_ADMIN>>]}]}},
        {<<"members">>,{[{<<"roles">>,[<<?TEST_MEMBER>>]}]}}
    ],

    Body = jiffy:encode({SecurityProperties}),
    {ok, Status, _, _} = test_request:put(SecurityUrl, [?CONTENT_JSON, ?AUTH], Body),
    ?assert(Status =:= 200).

delete_db(Url) ->
    {ok, 200, _, _} = test_request:delete(Url, [?AUTH]).

create_user(UsersUrl, Name, Password, Roles) ->

    Body = "{\"name\":\"" ++ Name ++
        "\",\"type\":\"user\",\"roles\":" ++ erlang:binary_to_list(jiffy:encode(Roles)) ++ ",\"password\":\"" ++ Password ++"\"}",

    Url = lists:concat([
        UsersUrl, "/org.couchdb.user:", Name]),
    {ok, 201, _, _} = test_request:put(Url, [?CONTENT_JSON, ?AUTH], Body).


all_test_() ->
    {
        "chttpd security tests",
        {
            setup,
            fun chttpd_test_util:start_couch/0, fun chttpd_test_util:stop_couch/1,
            {
                foreach,
                fun setup/0, fun teardown/1,
                [
                    fun should_allow_admin_db_compaction/1,
                    fun should_disallow_anonymous_db_compaction/1,
                    fun should_disallow_db_member_db_compaction/1,
                    fun should_allow_db_admin_db_compaction/1,
                    fun should_allow_admin_view_compaction/1,
                    fun should_disallow_anonymous_view_compaction/1,
                    fun should_allow_admin_db_view_cleanup/1,
                    fun should_disallow_anonymous_db_view_cleanup/1
                ]
            }
        }
    }.

should_allow_admin_db_compaction([Url,_UsersUrl]) ->
    ?_assertEqual(true,
        begin
            {ok, _, _, ResultBody} = test_request:post(Url ++ "/_compact",
                [?CONTENT_JSON, ?AUTH], ""),
            ResultJson = ?JSON_DECODE(ResultBody),
            {InnerJson} = ResultJson,
            couch_util:get_value(<<"ok">>, InnerJson, undefined)
        end).

should_disallow_anonymous_db_compaction([Url,_UsersUrl]) ->
    {ok, _, _, ResultBody} = test_request:post(Url ++ "/_compact",
        [?CONTENT_JSON], ""),
    ResultJson = ?JSON_DECODE(ResultBody),
    {InnerJson} = ResultJson,
    ErrType = couch_util:get_value(<<"error">>, InnerJson),
    ?_assertEqual(<<"unauthorized">>,ErrType).

should_disallow_db_member_db_compaction([Url,_UsersUrl]) ->
    {ok, _, _, ResultBody} = test_request:post(Url ++ "/_compact",
        [?CONTENT_JSON, ?TEST_MEMBER_AUTH], ""),
    ResultJson = ?JSON_DECODE(ResultBody),
    {InnerJson} = ResultJson,
    ErrType = couch_util:get_value(<<"error">>, InnerJson),
    ?_assertEqual(<<"unauthorized">>,ErrType).

should_allow_db_admin_db_compaction([Url,_UsersUrl]) ->
    ?_assertEqual(true,
        begin
            {ok, _, _, ResultBody} = test_request:post(Url ++ "/_compact",
                [?CONTENT_JSON, ?TEST_ADMIN_AUTH], ""),
            ResultJson = ?JSON_DECODE(ResultBody),
            {InnerJson} = ResultJson,
            couch_util:get_value(<<"ok">>, InnerJson, undefined)
        end).

should_allow_admin_view_compaction([Url,_UsersUrl]) ->
    ?_assertEqual(true,
        begin
            {ok, _, _, ResultBody} = test_request:post(Url ++ "/_compact/test",
                [?CONTENT_JSON, ?AUTH], ""),
            ResultJson = ?JSON_DECODE(ResultBody),
            {InnerJson} = ResultJson,
            couch_util:get_value(<<"ok">>, InnerJson, undefined)
        end).

should_disallow_anonymous_view_compaction([Url,_UsersUrl]) ->
    {ok, _, _, ResultBody} = test_request:post(Url ++ "/_compact/test",
        [?CONTENT_JSON], ""),
    ResultJson = ?JSON_DECODE(ResultBody),
    {InnerJson} = ResultJson,
    ErrType = couch_util:get_value(<<"error">>, InnerJson),
    ?_assertEqual(<<"unauthorized">>,ErrType).

should_allow_admin_db_view_cleanup([Url,_UsersUrl]) ->
    ?_assertEqual(true,
        begin
            {ok, _, _, ResultBody} = test_request:post(Url ++ "/_view_cleanup",
                [?CONTENT_JSON, ?AUTH], ""),
            ResultJson = ?JSON_DECODE(ResultBody),
            {InnerJson} = ResultJson,
            couch_util:get_value(<<"ok">>, InnerJson, undefined)
        end).

should_disallow_anonymous_db_view_cleanup([Url,_UsersUrl]) ->
    {ok, _, _, ResultBody} = test_request:post(Url ++ "/_view_cleanup",
        [?CONTENT_JSON], ""),
    ResultJson = ?JSON_DECODE(ResultBody),
    {InnerJson} = ResultJson,
    ErrType = couch_util:get_value(<<"error">>, InnerJson),
    ?_assertEqual(<<"unauthorized">>, ErrType).