Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DISCUSS] CouchDB Request Size Limits #1200

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions src/chttpd/src/chttpd_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,7 @@ db_attachment_req(#httpd{method=Method, user_ctx=Ctx}=Req, Db, DocId, FileNamePa
DocEdited = Doc#doc{
atts = NewAtt ++ [A || A <- Atts, couch_att:fetch(name, A) /= FileName]
},
couch_att:validate_attachment_count(length(DocEdited#doc.atts)),
W = chttpd:qs_value(Req, "w", integer_to_list(mem3:quorum(Db))),
case fabric:update_doc(Db, DocEdited, [{user_ctx,Ctx}, {w,W}]) of
{ok, UpdatedRev} ->
Expand Down
22 changes: 21 additions & 1 deletion src/couch/src/couch_att.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@

-export([
max_attachment_size/0,
validate_attachment_size/3
validate_attachment_size/3,
validate_attachment_count/1
]).

-compile(nowarn_deprecated_type).
Expand Down Expand Up @@ -718,6 +719,25 @@ max_attachment_size() ->
end.


max_attachment_count() ->
case config:get("couchdb", "max_attachments_per_document", "infinity") of
"infinity" ->
infinity;
MaxAttSize ->
list_to_integer(MaxAttSize)
end.


validate_attachment_count(AttCount) ->
case max_attachment_count() of
infinity -> ok;
MaxAttCount when AttCount =< MaxAttCount -> ok;
_TooManyAttachments ->
throw({request_entity_too_large,
<<"hit max_attachments_per_document">>})
end.


validate_attachment_size(AttName, AttSize, MaxAttSize)
when is_integer(AttSize), AttSize > MaxAttSize ->
throw({request_entity_too_large, {attachment, AttName}});
Expand Down
4 changes: 4 additions & 0 deletions src/couch/src/couch_doc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ from_json_obj_validate(EJson, DbName) ->
Doc = from_json_obj(EJson, DbName),
case couch_ejson_size:encoded_size(Doc#doc.body) =< MaxSize of
true ->
validate_attachment_count(Doc#doc.atts),
validate_attachment_sizes(Doc#doc.atts),
Doc;
false ->
Expand All @@ -153,6 +154,9 @@ validate_attachment_sizes(Atts) ->
end, Atts).


validate_attachment_count(Atts) ->
couch_att:validate_attachment_count(length(Atts)).

from_json_obj({Props}) ->
from_json_obj({Props}, undefined).

Expand Down