Skip to content

Commit

Permalink
fix(node): better source and move sanity checks
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Apr 11, 2023
1 parent d92db64 commit 1e6dab6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
12 changes: 12 additions & 0 deletions __tests__/files/file.spec.ts
Expand Up @@ -137,6 +137,18 @@ describe('File data change', () => {
expect(file.mtime?.getDate()).toBe(new Date().getDate())
})

test('Moving a file to an invalid destination throws', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
mtime: new Date(Date.UTC(2023, 0, 1, 0, 0, 0)),
})
expect(() => {
file.move('ftp://cloud.domain.com/remote.php/dav/files/emma/Pictures/picture-old.jpg')
}).toThrowError('Invalid source format, only http(s) is supported')
})

test('Moving a file to a different folder with root', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
Expand Down
8 changes: 6 additions & 2 deletions __tests__/files/node.spec.ts
Expand Up @@ -80,8 +80,12 @@ describe('Sanity checks', () => {
source: '/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma'
})).toThrowError('Invalid source')

})).toThrowError('Invalid source format, source must be a valid URL')
expect(() => new File({
source: 'ftp://remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma'
})).toThrowError('Invalid source format, only http(s) is supported')
})

test('Invalid mtime', () => {
Expand Down
12 changes: 5 additions & 7 deletions lib/files/node.ts
Expand Up @@ -93,13 +93,10 @@ export abstract class Node {
return dirname(this.source.slice(firstMatch + this.root.length) || '/')
}

// Try to parse the URL
try {
const url = new URL(this.source)
return dirname(url.pathname)
} catch (e) {}

return dirname(this.source)
// This should always be a valid URL
// as this is tested in the constructor
const url = new URL(this.source)
return dirname(url.pathname)
}

/**
Expand Down Expand Up @@ -216,6 +213,7 @@ export abstract class Node {
* e.g. https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg
*/
move(destination: string) {
validateData({ ...this._data, source: destination }, this._knownDavService)
this._data.source = destination
this._data.mtime = new Date()
}
Expand Down
8 changes: 7 additions & 1 deletion lib/files/nodeData.ts
Expand Up @@ -80,8 +80,14 @@ export const validateData = (data: NodeData, davService: RegExp) => {
throw new Error('Missing mandatory source')
}

try {
new URL(data.source)
} catch (e) {
throw new Error('Invalid source format, source must be a valid URL')
}

if (!data.source.startsWith('http')) {
throw new Error('Invalid source format')
throw new Error('Invalid source format, only http(s) is supported')
}

if ('mtime' in data && !(data.mtime instanceof Date)) {
Expand Down

0 comments on commit 1e6dab6

Please sign in to comment.