Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP/HTML: Resolution of double dots (..) in the URI (request, Location header etc.)

Tags:

http

uri

Are HTTP requests URIs allowed to contain ".." segments?

According to RFC 2616, section 5.1.2, they can refer to absolute URIs or absolute paths (the other options in that section are not relevant for this question).

The meaning of absolute URIs and absolute paths is described in RFC 3986, which also describes an algorithm to normalize paths (that includes remove single and double dot elements).

However, I can't find the exact specification whether an RFC conforming request URI can contain ".." segments - are they allowed in an absolute path/URI, and does the server have to normalize such URIs? Or is that up to the client?

Is there any difference for "Location:" response headers? According to the spec, they can only contain absolute URIs, but does that include ".." parts? Will the client have to normalize those too before requesting the referred resource?

To clarify, I know that URIs like ../foo are illegal in those situations, but what about http://example.com/../foo? Is that a valid absolute URI?

I'm currently redirecting clients to such URIs and would like to know if that is conforming to the specifications.

like image 903
lxgr Avatar asked Nov 07 '12 17:11

lxgr


2 Answers

If you want to "know if that is conforming to the specifications," why don't you simply refer to the relevant specification?

RFC 3986 Section 5.2 is very clear on how URI dot segments should be resolved:

This section describes an algorithm for converting a URI reference that might be relative to a given base URI into the parsed components of the reference's target. The components can then be recomposed, as described in Section 5.3, to form the target URI. This algorithm provides definitive results that can be used to test the output of other implementations. Applications may implement relative reference resolution by using some other algorithm, provided that the results match what would be given by this one.

If you are, for example, following Location: headers, it's usually prudent to normalize and resolve invalid relative paths (Location: headers are supposed to be absolute URIs). In these cases you should absolutely follow the instruction of RFC 3986 to resolve those paths against your base URI.

Should you pass around dot segments in your URIs all over the place? Probably not if you can help it because you're relying on other people to have implemented the specification correctly. But does passing URIs with dot segments violate the URI specification? No.

like image 153
rdlowrey Avatar answered Oct 26 '22 23:10

rdlowrey


Syntactically speaking, http://example.com/../foo is a valid URI.

How the server interprets that URI is a different matter. Servers have to be very careful about how then translate URIs to file paths, for obvious security reasons. Usually the server will either strip out .. segments, or do some kind of post-processing to make sure the file path is inside the document root.

like image 32
slashingweapon Avatar answered Oct 27 '22 01:10

slashingweapon