Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lib/resource-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,21 @@ class ResourceMapper {

// Parses a URL into hostname and pathname
_parseUrl (url) {
let parsed
// URL specified as string
if (typeof url === 'string') {
return URL.parse(url)
}
parsed = URL.parse(url)
// URL specified as Express request object
if (!url.pathname && url.path) {
} else if (!url.pathname && url.path) {
const { hostname, path } = url
return { hostname, pathname: path.replace(/[?#].*/, '') }
}
parsed = { hostname, pathname: path.replace(/[?#].*/, '') }
// URL specified as object
return url
} else {
parsed = url
}
// reject Url containning %encoded /
if (parsed.pathname.includes('%2F')) throw new Error('Url cannot contain %2F (%encoded /)')
return parsed
}

// Gets the expected content type based on the extension of the path
Expand Down
12 changes: 10 additions & 2 deletions test/unit/resource-mapper-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ describe('ResourceMapper', () => {

itMapsUrl(mapper, 'a URL of a new file with encoded characters',
{
url: 'http://localhost/space%2Ffoo%20bar%20bar.html',
url: 'http://localhost/space/foo%20bar%20bar.html',
contentType: 'text/html',
createIfNotExists: true
},
Expand All @@ -235,6 +235,14 @@ describe('ResourceMapper', () => {
contentType: 'text/html'
})

itMapsUrl(mapper, 'a URL of a new file with encoded characters',
{
url: 'http://localhost/space%2Ffoo%20bar%20bar.html',
contentType: 'text/html',
createIfNotExists: true
},
new Error('Url cannot contain %2F (%encoded /)'))

itMapsUrl(mapper, 'a URL of an existing .acl file',
{
url: 'http://localhost/space/.acl'
Expand Down Expand Up @@ -413,7 +421,7 @@ describe('ResourceMapper', () => {
{
url: 'http://localhost/space%2F..%2Fbar'
},
new Error('Disallowed /.. segment in URL'))
new Error('Url cannot contain %2F (%encoded /)'))

// File to URL mapping

Expand Down