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

Handle relative paths according to RFC3986 #2984

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions request.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ function filterOutReservedFunctions (reserved, options) {
return object
}

// RFC3986 Sec 5.2.4 Handle /./ and /../
function removeDotSegments (uri) {
var inBuf = uri.split('/')
var outBuf = []
for (var i in inBuf) {
var segment = inBuf[i]
if (segment === '..' && outBuf.length > 1) {
outBuf.pop()
}
if (segment !== '.' && segment !== '..') {
outBuf.push(segment)
}
}
return outBuf.join('/')
}

// Return a simpler request object to allow serialization
function requestToJSON () {
var self = this
Expand Down Expand Up @@ -350,6 +366,8 @@ Request.prototype.init = function (options) {
self.path = '/'
}

self.path = removeDotSegments(self.path)

// Auth must happen last in case signing is dependent on other headers
if (options.aws) {
self.aws(options.aws)
Expand Down
8 changes: 8 additions & 0 deletions tests/test-baseUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ function addTests () {
addTest(s.url + '/api/', '', '/api/')
addTest(s.url + '/api', '/', '/api/')
addTest(s.url + '/api/', '/', '/api/')
addTest(s.url + '/api/', '/../', '/')
addTest(s.url + '/api/', '../', '/')
addTest(s.url + '/api/', '/./', '/api/')
addTest(s.url + '/api/', './', '/api/')
addTest(s.url + '/api', 'resource', '/api/resource')
addTest(s.url + '/api/', 'resource', '/api/resource')
addTest(s.url + '/api', '/resource', '/api/resource')
Expand All @@ -45,6 +49,10 @@ function addTests () {
addTest(s.url + '/api/', 'resource/', '/api/resource/')
addTest(s.url + '/api', '/resource/', '/api/resource/')
addTest(s.url + '/api/', '/resource/', '/api/resource/')
addTest(s.url + '/api/', '/../resource/', '/resource/')
addTest(s.url + '/api/', '/../resource', '/resource')
addTest(s.url + '/api/', '/./resource/', '/api/resource/')
addTest(s.url + '/api/', '/./resource', '/api/resource')
}

tape('setup', function (t) {
Expand Down