Skip to content

Decode destination header for doc copy#1072

Merged
davisp merged 1 commit intoapache:masterfrom
cloudant:issue-977-decode-destheader-for-copydoc
Jan 26, 2018
Merged

Decode destination header for doc copy#1072
davisp merged 1 commit intoapache:masterfrom
cloudant:issue-977-decode-destheader-for-copydoc

Conversation

@jiangphcn
Copy link
Copy Markdown
Contributor

@jiangphcn jiangphcn commented Dec 18, 2017

Overview

The Fauxton URL request to CouchDB encodes the user input and sets the Destination header with the encoded value of the new ID when sending the COPY request. This PR is aimed to make CouchDB to decode Destination header, and creates the ID without escaped values.

jiangphs-mbp-2:geotest jiangph$ curl -u foo:bar http://127.0.0.1:15984/testdb1/point11 -X COPY -H 'Content-Type: application/json'  -H 'Destination: xxx%E5%95%8Axxx'
{"ok":true,"ok":true,"id":"xxx啊xxx","rev":"1-ffb6c2ae737918f8f911e1fce668333e"}
jiangphs-mbp-2:geotest jiangph$ curl -u foo:bar http://127.0.0.1:15984/testdb1/point11 -X COPY -H 'Content-Type: application/json'  -H 'Destination: this/is/an/example'
{"ok":true,"ok":true,"id":"this/is/an/example","rev":"1-ffb6c2ae737918f8f911e1fce668333e"}

Testing recommendations

make check skip_deps+=couch_epi apps=chttpd tests=all_test_
======================== EUnit ========================
chttpd db tests
Application crypto was left running!
  chttpd_db_test:84: should_return_ok_true_on_bulk_update...[0.089 s] ok
  chttpd_db_test:111: should_accept_live_as_an_alias_for_continuous...[0.043 s] ok
  chttpd_db_test:126: should_return_404_for_delete_att_on_notadoc...[0.004 s] ok
  chttpd_db_test:148: should_return_409_for_del_att_without_rev...[0.038 s] ok
  chttpd_db_test:166: should_return_200_for_del_att_with_rev...[0.069 s] ok
  chttpd_db_test:187: should_return_409_for_put_att_nonexistent_rev...[0.011 s] ok
  chttpd_db_test:282: should_return_correct_id_on_doc_copy...[0.083 s] ok
[os_mon] cpu supervisor port (cpu_sup): Erlang has closed
  [done in 3.898 s]
=======================================================
  All 7 tests passed.
==> rel (eunit)
==> couchdb (eunit)

Related Issues or Pull Requests

issue #977

Checklist

  • Code is written and works correctly;
  • Changes are covered by tests;
  • Documentation reflects the changes;

@jiangphcn jiangphcn force-pushed the issue-977-decode-destheader-for-copydoc branch from 7223dd9 to 0cb99b4 Compare December 19, 2017 09:15
@jiangphcn jiangphcn changed the title [WIP]Decode destination header for doc copy Decode destination header for doc copy Dec 19, 2017
@jiangphcn jiangphcn force-pushed the issue-977-decode-destheader-for-copydoc branch 2 times, most recently from 74752d0 to b8643cc Compare December 19, 2017 10:34
end,
{TargetDocId, TargetRevs} = couch_httpd_db:parse_copy_destination_header(Req),
{TargetDocId0, TargetRevs} = couch_httpd_db:parse_copy_destination_header(Req),
TargetDocId = list_to_binary(mochiweb_util:unquote(TargetDocId0)),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem here is that the doc id isn't always escaped (that change is fairly recent).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Bob. According to statement from Garren in issue #977

Which means the API works although it doesn't follow 100% the Destination header's spec which states the header should contain an URI, hence its value should be encoded if it includes characters outside the valid range.

If spec implies that destination header for couchdb should be encoded, we need to decode here. If there is no escaped character in docid for target doc, calling mochiweb_util:unquote/1 will not change the content of docid.

@rnewson
Copy link
Copy Markdown
Member

rnewson commented Dec 20, 2017

There's several places where we url encode and url decode, and we've changed where we do that over multiple versions of couchdb. I'm struggling to see if we can call unquote unilaterally here. Do we need to care about this across versions or is it sufficient to be internally consistent?

@jiangphcn jiangphcn force-pushed the issue-977-decode-destheader-for-copydoc branch from 77126ba to 85880cf Compare January 23, 2018 13:24
@jiangphcn
Copy link
Copy Markdown
Contributor Author

https://tools.ietf.org/html/rfc4918#section-10.3
https://tools.ietf.org/html/rfc3986

From above links, we know that

10.3.  Destination Header

   The Destination request header specifies the URI that identifies a
   destination resource for methods such as COPY and MOVE, which take
   two URIs as parameters.

      Destination = "Destination" ":" Simple-ref


   If the Destination value is an absolute-URI (Section 4.3 of
   [RFC3986]), it may name a different server (or different port or
   scheme).  If the source server cannot attempt a copy to the remote
   server, it MUST fail the request.  Note that copying and moving
   resources to remote servers is not fully defined in this
   specification (e.g., specific error conditions).
Abstract

   A Uniform Resource Identifier (URI) is a compact sequence of
   characters that identifies an abstract or physical resource.  This
   specification defines the generic URI syntax and a process for
   resolving URI references that might be in relative form, along with
   guidelines and security considerations for the use of URIs on the
   Internet.  The URI syntax defines a grammar that is a superset of all
   valid URIs, allowing an implementation to parse the common components
   of a URI reference without knowing the scheme-specific requirements
   of every possible identifier.  This specification does not define a
   generative grammar for URIs; that task is performed by the individual
   specifications of each URI scheme.

@jiangphcn
Copy link
Copy Markdown
Contributor Author

My understanding is that it is not restricted to encode or un-encode URI. For with url encoding, it is safe from security's point of view.

When discussing with Bob and Paul, Bob mentioned "we should encode the destination header value, and decode it wherever we might need to (if anywhere)".

Here is one place we need to decode URI.

@jiangphcn jiangphcn force-pushed the issue-977-decode-destheader-for-copydoc branch 2 times, most recently from 0dc6b37 to 9f60d6d Compare January 24, 2018 01:29
Copy link
Copy Markdown
Member

@davisp davisp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@jiangphcn jiangphcn force-pushed the issue-977-decode-destheader-for-copydoc branch from 9f60d6d to 2f5db85 Compare January 26, 2018 15:28
@jiangphcn jiangphcn force-pushed the issue-977-decode-destheader-for-copydoc branch from 2f5db85 to 51ac2f5 Compare January 26, 2018 16:08
@davisp davisp merged commit 52e7cbe into apache:master Jan 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants