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
31 changes: 27 additions & 4 deletions lib/resource-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ class ResourceMapper {

// Determine the URL by chopping off everything after the dollar sign
const pathname = this._removeDollarExtension(path)
const url = `${this.resolveUrl(hostname)}${
pathname.split('/').map((component) => encodeURIComponent(component)).join('/')
}`
const url = `${this.resolveUrl(hostname)}${this._encodePath(pathname)}`
return { url, contentType: this._getContentTypeFromExtension(path) }
}

Expand All @@ -95,7 +93,7 @@ class ResourceMapper {
contentType = contentType ? contentType.replace(/\s*;.*/, '') : ''
// Parse the URL and find the base file path
const { pathname, hostname } = this._parseUrl(url)
const filePath = this.resolveFilePath(hostname, decodeURIComponent(pathname))
const filePath = this.resolveFilePath(hostname, this._decodePath(pathname))
if (filePath.indexOf('/..') >= 0) {
throw new Error('Disallowed /.. segment in URL')
}
Expand Down Expand Up @@ -149,6 +147,31 @@ class ResourceMapper {
return { path, contentType: contentType || this._defaultContentType }
}

// encode/decode path except slash (/), %encodedSlash (%2F|%2f), and/or ntimes%encodedSlash (%2525...2F|%2525...2f)
// see https://github.com/solid/node-solid-server/issues/1666
_exceptSlash () { return /(\/|%(?:25)*(?:2[Ff]))/g }

_encodePath (pathname) {
return pathname.split(this._exceptSlash())
.map((el, i) => i % 2 === 0 ? encodeURIComponent(el) : el)
.join('')
/* pathArray.forEach((el, i) => {
if (i % 2 === 0) pathArray[i] = encodeURIComponent(el)
Copy link
Contributor

Choose a reason for hiding this comment

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

Might want to normalize path separators to lowercase or uppercase, such that they are mapped to the same filename. (see CommunitySolidServer/CommunitySolidServer#1273)

}) */
// return pathArray.join('')
}

_decodePath (pathname) {
return pathname.split(this._exceptSlash())
.map((el, i) => i % 2 === 0 ? decodeURIComponent(el) : el)
.join('')
/* const pathArray = pathname.split(this._exceptSlash())
pathArray.forEach((el, i) => {
if (i % 2 === 0) pathArray[i] = decodeURIComponent(el)
})
return pathArray.join('') */
}

// Parses a URL into hostname and pathname
_parseUrl (url) {
// URL specified as string
Expand Down
Loading