Merge pull request #1104 from transloadit/feature/transloadit-server-pattern

transloadit: Add COMPANION_PATTERN constant.
This commit is contained in:
Renée Kooi 2018-10-18 13:07:19 +02:00 committed by GitHub
commit 69008fd9fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 3 deletions

View file

@ -15,6 +15,8 @@ function defaultGetAssemblyOptions (file, options) {
}
const COMPANION = 'https://api2.transloadit.com/companion'
// Regex matching acceptable postMessage() origins for authentication feedback from companion.
const ALLOWED_COMPANION_PATTERN = /\.transloadit\.com$/
// Regex used to check if a Companion address is run by Transloadit.
const TL_COMPANION = /https?:\/\/api2(?:-\w+)?\.transloadit\.com\/companion/
const TL_UPPY_SERVER = /https?:\/\/api2(?:-\w+)?\.transloadit\.com\/uppy-server/
@ -116,10 +118,11 @@ module.exports = class Transloadit extends Plugin {
this.uppy.log(err)
throw err
}
if (file.remote && TL_COMPANION.test(file.remote.serverUrl)) {
let newHost = status.companion_url
const newHost = status.companion_url
.replace(/\/$/, '')
let path = file.remote.url
const path = file.remote.url
.replace(file.remote.serverUrl, '')
.replace(/^\//, '')
@ -695,3 +698,4 @@ module.exports = class Transloadit extends Plugin {
module.exports.COMPANION = COMPANION
module.exports.UPPY_SERVER = COMPANION
module.exports.COMPANION_PATTERN = ALLOWED_COMPANION_PATTERN

View file

@ -55,9 +55,12 @@ const Transloadit = require('@uppy/transloadit')
uppy.use(Dropbox, {
serverUrl: Transloadit.COMPANION
serverPattern: Transloadit.COMPANION_PATTERN
})
```
When using `Transloadit.COMPANION`, you should also configure [`serverPattern: Transloadit.COMPANION_PATTERN`](#Transloadit-COMPANION-PATTERN).
The value of this constant is `https://api2.transloadit.com/companion`. If you are using a custom [`service`](#service) option, you should also set a custom host option in your provider plugins, by taking a Transloadit API url and appending `/companion`:
```js
@ -66,6 +69,24 @@ uppy.use(Dropbox, {
})
```
### `Transloadit.COMPANION_PATTERN`
A RegExp pattern matching Transloadit's hosted companion endpoints. The pattern is used in remote provider `serverPattern` options, to ensure that third party authentication messages cannot be faked by an attacker's page, but can only originate from Transloadit's servers.
Use it whenever you use `serverUrl: Transloadit.COMPANION`, like so:
```js
const Dropbox = require('@uppy/dropbox')
const Transloadit = require('@uppy/transloadit')
uppy.use(Dropbox, {
serverUrl: Transloadit.COMPANION
serverPattern: Transloadit.COMPANION_PATTERN
})
```
The value of this constant covers _all_ Transloadit's Companion servers, so it does not need to be changed if you are using a custom [`service`](#service) option. However, if you are not using the Transloadit Companion servers at `*.transloadit.com`, make sure to set the `serverPattern` option to something that matches what you do use.
## Options
The `@uppy/transloadit` plugin has the following configurable options:

View file

@ -57,7 +57,11 @@ function initUppy () {
target: '#uppy-dashboard-container',
note: 'Images only, 12 files, up to 1 MB'
})
.use(Instagram, { target: Dashboard, serverUrl: 'https://api2.transloadit.com/companion', serverPattern: /\.transloadit\.com$/ })
.use(Instagram, {
target: Dashboard,
serverUrl: 'https://api2.transloadit.com/companion',
serverPattern: Transloadit.COMPANION_PATTERN
})
.use(Webcam, { target: Dashboard })
uppy