Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ share/server/main-coffee.js
share/server/main.js
share/server/main-ast-bypass.js
share/www
src/argparse/
src/bear/
src/certifi/
src/couch/priv/couch_js/**/config.h
Expand All @@ -55,6 +56,7 @@ src/couch/priv/couch_js/**/*.d
src/couch/priv/icu_driver/couch_icu_driver.d
src/cowlib/
src/mango/src/mango_cursor_text.nocompile
src/erlperf/
src/excoveralls/
src/fauxton/
src/folsom/
Expand Down
4 changes: 3 additions & 1 deletion rebar.config.script
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ DepDescs = [
{jiffy, "jiffy", {tag, "1.1.2"}},
{mochiweb, "mochiweb", {tag, "v3.3.0"}},
{meck, "meck", {tag, "v1.1.0"}},
{recon, "recon", {tag, "2.5.6"}}
{recon, "recon", {tag, "2.5.6"}},
{argparse, {url, "https://github.com/max-au/argparse"}, "1.2.4"},
{erlperf, {url, "https://github.com/max-au/erlperf"}, "2.3.0"}
].

WithProper = lists:keyfind(with_proper, 1, CouchConfig) == {with_proper, true}.
Expand Down
24 changes: 13 additions & 11 deletions src/couch/src/couch_query_servers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -477,19 +477,21 @@ builtin_cmp_last(A, B) ->
validate_doc_update(Db, DDoc, EditDoc, DiskDoc, Ctx, SecObj) ->
JsonEditDoc = couch_doc:to_json_obj(EditDoc, [revs]),
JsonDiskDoc = json_doc(DiskDoc),
Resp = ddoc_prompt(
Db,
DDoc,
[<<"validate_doc_update">>],
[JsonEditDoc, JsonDiskDoc, Ctx, SecObj]
),
if
Resp == 1 -> ok;
true -> couch_stats:increment_counter([couchdb, query_server, vdu_rejects], 1)
end,
Args = [JsonEditDoc, JsonDiskDoc, Ctx, SecObj],

Resp =
case ddoc_prompt(Db, DDoc, [<<"validate_doc_update">>], Args) of
Code when Code =:= 1; Code =:= ok; Code =:= true ->
ok;
Other ->
couch_stats:increment_counter([couchdb, query_server, vdu_rejects], 1),
Other
end,
case Resp of
RespCode when RespCode =:= 1; RespCode =:= ok; RespCode =:= true ->
ok ->
ok;
{[{<<"forbidden">>, Message}, {<<"failures">>, Failures}]} ->
throw({forbidden, Message, Failures});
{[{<<"forbidden">>, Message}]} ->
throw({forbidden, Message});
{[{<<"unauthorized">>, Message}]} ->
Expand Down
2 changes: 1 addition & 1 deletion src/couch_mrview/src/couch_mrview.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ validate_ddoc_fields(DDoc) ->
[{<<"rewrites">>, [string, array]}],
[{<<"shows">>, object}, {any, [object, string]}],
[{<<"updates">>, object}, {any, [object, string]}],
[{<<"validate_doc_update">>, string}],
[{<<"validate_doc_update">>, [string, object]}],
[{<<"views">>, object}, {<<"lib">>, object}],
[{<<"views">>, object}, {any, object}, {<<"map">>, MapFuncType}],
[{<<"views">>, object}, {any, object}, {<<"reduce">>, string}]
Expand Down
51 changes: 33 additions & 18 deletions src/mango/src/mango_doc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

get_field/2,
get_field/3,
get_field_with_stack/3,
get_field_from_stack/2,
rem_field/2,
set_field/3
]).
Expand Down Expand Up @@ -377,43 +379,56 @@ do_update_to_insert([{_, _} | Rest], Doc) ->
get_field(Props, Field) ->
get_field(Props, Field, no_validation).

get_field(Props, Field, Validator) when is_binary(Field) ->
{ok, Path} = mango_util:parse_field(Field),
get_field(Props, Path, Validator);
get_field(Props, [], no_validation) ->
Props;
get_field(Props, [], Validator) ->
case (catch Validator(Props)) of
get_field(Props, Field, no_validation) ->
{Value, _} = get_field_with_stack(Props, Field, []),
Value;
get_field(Props, Field, Validator) ->
{Value, _} = get_field_with_stack(Props, Field, []),
case (catch Validator(Value)) of
true ->
Props;
Value;
_ ->
invalid_value
end;
get_field({Props}, [Name | Rest], Validator) ->
end.

get_field_with_stack(Props, Field, Stack) when is_binary(Field) ->
{ok, Path} = mango_util:parse_field(Field),
get_field_with_stack(Props, Path, Stack);
get_field_with_stack(Props, [], Stack) ->
{Props, Stack};
get_field_with_stack({Props}, [Name | Rest], Stack) ->
case lists:keyfind(Name, 1, Props) of
{Name, Value} ->
get_field(Value, Rest, Validator);
get_field_with_stack(Value, Rest, [{Props} | Stack]);
false ->
not_found
{not_found, [{Props} | Stack]}
end;
get_field(Values, [Name | Rest], Validator) when is_list(Values) ->
get_field_with_stack(Values, [Name | Rest], Stack) when is_list(Values) ->
% Name might be an integer index into an array
try
Pos = binary_to_integer(Name),
case Pos >= 0 andalso Pos < length(Values) of
true ->
% +1 because Erlang uses 1 based list indices
Value = lists:nth(Pos + 1, Values),
get_field(Value, Rest, Validator);
get_field_with_stack(Value, Rest, Stack);
false ->
bad_path
{bad_path, Stack}
end
catch
error:badarg ->
bad_path
{bad_path, Stack}
end;
get_field(_, [_ | _], _) ->
bad_path.
get_field_with_stack(_, [_ | _], Stack) ->
{bad_path, Stack}.

get_field_from_stack([{[{<<"parent">>, N}]} | Rest], Stack) ->
case length(Stack) >= N of
true -> get_field(lists:nth(N, Stack), Rest);
false -> not_found
end;
get_field_from_stack(Path, Stack) ->
get_field(lists:last(Stack), Path).

rem_field(Props, Field) when is_binary(Field) ->
{ok, Path} = mango_util:parse_field(Field),
Expand Down
32 changes: 32 additions & 0 deletions src/mango/src/mango_native_proc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

-record(st, {
indexes = [],
validators = [],
timeout = 5000
}).

Expand Down Expand Up @@ -94,6 +95,37 @@ handle_call({prompt, [<<"nouveau_index_doc">>, Doc]}, _From, St) ->
Else
end,
{reply, Vals, St};
handle_call({prompt, [<<"ddoc">>, <<"new">>, DDocId, {DDoc}]}, _From, St) ->
NewSt =
case couch_util:get_value(<<"validate_doc_update">>, DDoc) of
undefined ->
St;
Selector0 ->
Selector = mango_selector:normalize(Selector0),
Validators = couch_util:set_value(DDocId, St#st.validators, Selector),
St#st{validators = Validators}
end,
{reply, true, NewSt};
handle_call({prompt, [<<"ddoc">>, DDocId, [<<"validate_doc_update">>], Args]}, _From, St) ->
case couch_util:get_value(DDocId, St#st.validators) of
undefined ->
Msg = [<<"validate_doc_update">>, DDocId],
{stop, {invalid_call, Msg}, {invalid_call, Msg}, St};
Selector ->
[NewDoc, OldDoc, _Ctx, _SecObj] = Args,
Struct = {[{<<"newDoc">>, NewDoc}, {<<"oldDoc">>, OldDoc}]},
Reply =
case mango_selector:match_failures(Selector, Struct) of
[] ->
true;
Failures ->
{[
{<<"forbidden">>, <<"forbidden">>},
{<<"failures">>, Failures}
]}
end,
{reply, Reply, St}
end;
handle_call(Msg, _From, St) ->
{stop, {invalid_call, Msg}, {invalid_call, Msg}, St}.

Expand Down
Loading