Skip to content

Commit

Permalink
Make uploadUrls recommended (transloadit#3182)
Browse files Browse the repository at this point in the history
* Make uploadUrls recommended

- warn on startup if uploadUrls is not specified as not specifying it is a security risk
- improve docs to make it more clear why uploadUrls should be specified (say that uploadUrls is required even though it is not, due to backward compatibility)
- no longer require_tld (it gives a false security) - fixes transloadit#2831

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>

* remove `example: []`

Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
mifi and aduh95 committed Sep 30, 2021
1 parent f61a845 commit d08d377
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/companion.js
Expand Up @@ -249,4 +249,8 @@ const validateConfig = (companionOptions) => {
}
})
}

if (companionOptions.uploadUrls == null || companionOptions.uploadUrls.length === 0) {
logger.warn('Running without uploadUrls specified is a security risk if running in production', 'startup.uploadUrls')
}
}
2 changes: 1 addition & 1 deletion src/server/Uploader.js
Expand Up @@ -201,7 +201,7 @@ class Uploader {
return false
}

const validatorOpts = { require_protocol: true, require_tld: !options.companionOptions.debug }
const validatorOpts = { require_protocol: true, require_tld: false }
return [options.endpoint, options.uploadUrl].every((url) => {
if (url && !validator.isURL(url, validatorOpts)) {
this._errRespMessage = 'invalid destination url'
Expand Down
30 changes: 29 additions & 1 deletion test/__tests__/uploader.js
Expand Up @@ -7,10 +7,38 @@ const Uploader = require('../../src/server/Uploader')
const socketClient = require('../mocksocket')
const standalone = require('../../src/standalone')

const { companionOptions } = standalone()

describe('uploader with tus protocol', () => {
test('uploader respects uploadUrls', async () => {
const opts = {
endpoint: 'http://localhost/files',
companionOptions: { ...companionOptions, uploadUrls: [/^http:\/\/url.myendpoint.com\//] },
}

expect(new Uploader(opts).hasError()).toBe(true)
})

test('uploader respects uploadUrls, valid', async () => {
const opts = {
endpoint: 'http://url.myendpoint.com/files',
companionOptions: { ...companionOptions, uploadUrls: [/^http:\/\/url.myendpoint.com\//] },
}

expect(new Uploader(opts).hasError()).toBe(false)
})

test('uploader respects uploadUrls, localhost', async () => {
const opts = {
endpoint: 'http://localhost:1337/',
companionOptions: { ...companionOptions, uploadUrls: [/^http:\/\/localhost:1337\//] },
}

expect(new Uploader(opts).hasError()).toBe(false)
})

test('upload functions with tus protocol', () => {
const fileContent = Buffer.from('Some file content')
const { companionOptions } = standalone()
const opts = {
companionOptions,
endpoint: 'http://url.myendpoint.com/files',
Expand Down

0 comments on commit d08d377

Please sign in to comment.