From 0d27d3e0a6815575fca422f946e258bf20452502 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sat, 3 Mar 2018 15:00:42 +0100 Subject: [PATCH] feat: allow configuration of maximum number of attachments per doc --- src/chttpd/src/chttpd_db.erl | 1 + src/couch/src/couch_att.erl | 22 +++++++++++++++++++++- src/couch/src/couch_doc.erl | 4 ++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl index 2c3ec635679..cae1c11a14f 100644 --- a/src/chttpd/src/chttpd_db.erl +++ b/src/chttpd/src/chttpd_db.erl @@ -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} -> diff --git a/src/couch/src/couch_att.erl b/src/couch/src/couch_att.erl index d699b6a8fc6..fa45b932dd4 100644 --- a/src/couch/src/couch_att.erl +++ b/src/couch/src/couch_att.erl @@ -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). @@ -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}}); diff --git a/src/couch/src/couch_doc.erl b/src/couch/src/couch_doc.erl index f960ec5c2d9..a39ff1752f0 100644 --- a/src/couch/src/couch_doc.erl +++ b/src/couch/src/couch_doc.erl @@ -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 -> @@ -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).