You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2024/07/05 11:20:03 UTC

(couchdb) 01/01: Validate target doc id for COPY method

This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch validate-docid-copy-method
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit c2d44c44ef361fc06e562654b5517f60701a2400
Author: Robert Newson <Ro...@ibm.com>
AuthorDate: Fri Jul 5 12:16:42 2024 +0100

    Validate target doc id for COPY method
    
    closes #5117
---
 src/chttpd/src/chttpd_db.erl       |  1 +
 src/couch/src/couch_httpd_db.erl   |  1 +
 test/elixir/test/copy_doc_test.exs | 10 ++++++++++
 3 files changed, 12 insertions(+)

diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl
index 8cc7a8619..062e0bf24 100644
--- a/src/chttpd/src/chttpd_db.erl
+++ b/src/chttpd/src/chttpd_db.erl
@@ -1200,6 +1200,7 @@ db_doc_req(#httpd{method = 'COPY', user_ctx = Ctx} = Req, Db, SourceDocId) ->
         end,
     {TargetDocId0, TargetRevs} = couch_httpd_db:parse_copy_destination_header(Req),
     TargetDocId = list_to_binary(chttpd:unquote(TargetDocId0)),
+    couch_db:validate_docid(Db, TargetDocId),
     % open old doc
     Doc = couch_doc_open(Db, SourceDocId, SourceRev, []),
     % save new doc
diff --git a/src/couch/src/couch_httpd_db.erl b/src/couch/src/couch_httpd_db.erl
index e045510cc..5c566ed78 100644
--- a/src/couch/src/couch_httpd_db.erl
+++ b/src/couch/src/couch_httpd_db.erl
@@ -685,6 +685,7 @@ db_doc_req(#httpd{method = 'COPY'} = Req, Db, SourceDocId) ->
         end,
     {TargetDocId0, TargetRevs} = parse_copy_destination_header(Req),
     TargetDocId = list_to_binary(mochiweb_util:unquote(TargetDocId0)),
+    couch_db:validate_docid(Db, TargetDocId),
     % open old doc
     Doc = couch_doc_open(Db, SourceDocId, SourceRev, []),
     % save new doc
diff --git a/test/elixir/test/copy_doc_test.exs b/test/elixir/test/copy_doc_test.exs
index 4641ff6ea..f1f554242 100644
--- a/test/elixir/test/copy_doc_test.exs
+++ b/test/elixir/test/copy_doc_test.exs
@@ -56,6 +56,16 @@ defmodule CopyDocTest do
     assert resp.status_code == 400
     assert resp.body["reason"] == "Destination URL must be relative."
 
+    resp =
+      Couch.request(
+        :copy,
+        "/#{db_name}/doc_to_be_copied2",
+        headers: [Destination: "_invalid_doc_id?rev=#{rev}"]
+      )
+
+    assert resp.status_code == 400
+    assert resp.body["reason"] == "Only reserved document ids may start with underscore."
+
     resp =
       Couch.request(
         :copy,