Skip to content

Commit

Permalink
fix: url encoding/decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Nov 10, 2024
1 parent 38c6525 commit ea07171
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@filen/webdav",
"version": "0.2.56",
"version": "0.2.57",
"description": "Filen WebDAV",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class Copy {
return
}

const destination = decodeURI(url.pathname)
const destination = decodeURIComponent(url.pathname)

if (destination.startsWith("..") || destination.startsWith("./") || destination.startsWith("../")) {
await Responses.forbidden(res)
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/mkcol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Mkcol {
*/
public async handle(req: Request, res: Response): Promise<void> {
try {
const path = decodeURI(req.url.endsWith("/") ? req.url.slice(0, req.url.length - 1) : req.url)
const path = decodeURIComponent(req.url.endsWith("/") ? req.url.slice(0, req.url.length - 1) : req.url)
const sdk = this.server.getSDKForUser(req.username)

if (!sdk) {
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class Move {
return
}

const destination = decodeURI(url.pathname)
const destination = decodeURIComponent(url.pathname)

if (destination.startsWith("..") || destination.startsWith("./") || destination.startsWith("../")) {
await Responses.forbidden(res)
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class Put {
*/
public async handle(req: Request, res: Response): Promise<void> {
try {
const path = removeLastSlash(decodeURI(req.url))
const path = removeLastSlash(decodeURIComponent(req.url))
const parentPath = pathModule.posix.dirname(path)
const name = pathModule.posix.basename(path)
const thisResource = await this.server.pathToResource(req, path)
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export class WebDAVServer {
* @returns {ISemaphore}
*/
public getRWMutexForUser(path: string, username?: string): ISemaphore {
path = removeLastSlash(decodeURI(path))
path = removeLastSlash(decodeURIComponent(path))

if (!username) {
return new Semaphore(1)
Expand Down Expand Up @@ -302,7 +302,7 @@ export class WebDAVServer {
* @returns {Promise<Resource | null>}
*/
public async urlToResource(req: Request): Promise<Resource | null> {
const url = decodeURI(req.url)
const url = decodeURIComponent(req.url)
const path = url === "/" ? url : removeLastSlash(url)

if (this.getVirtualFilesForUser(req.username)[path]) {
Expand Down
15 changes: 12 additions & 3 deletions src/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export class Responses {
"xmlns:D": "DAV:"
},
"D:response": resources.map(resource => ({
"D:href": `${encodeURI(resource.url)}`,
"D:href": `${resource.url
.split("/")
.map(part => encodeURIComponent(part))
.join("/")}`,
["D:propstat"]: {
"D:prop": {
"D:getlastmodified": new Date(resource.mtimeMs).toUTCString(),
Expand Down Expand Up @@ -80,7 +83,10 @@ export class Responses {
"xmlns:D": "DAV:"
},
"D:response": {
"D:href": `${encodeURI(url)}`,
"D:href": `${url
.split("/")
.map(part => encodeURIComponent(part))
.join("/")}`,
["D:propstat"]: {
"D:prop": {},
"D:status": "HTTP/1.1 207 Multi-Status"
Expand Down Expand Up @@ -111,7 +117,10 @@ export class Responses {
"xmlns:D": "DAV:"
},
"D:response": {
"D:href": `${encodeURI(url)}`,
"D:href": `${url
.split("/")
.map(part => encodeURIComponent(part))
.join("/")}`,
["D:propstat"]: {
"D:prop": {},
"D:status": "HTTP/1.1 404 NOT FOUND"
Expand Down

0 comments on commit ea07171

Please sign in to comment.