2018-08-07 22:40:17 +00:00
|
|
|
const crypto = require('crypto');
|
2018-02-25 02:00:43 +00:00
|
|
|
const express = require('express');
|
2017-08-24 21:54:02 +00:00
|
|
|
const helmet = require('helmet');
|
|
|
|
const storage = require('../storage');
|
|
|
|
const config = require('../config');
|
2018-02-06 22:31:18 +00:00
|
|
|
const auth = require('../middleware/auth');
|
|
|
|
const language = require('../middleware/language');
|
2017-08-24 21:54:02 +00:00
|
|
|
const pages = require('./pages');
|
2018-08-07 22:40:17 +00:00
|
|
|
const fxa = require('./fxa');
|
|
|
|
const filelist = require('./filelist');
|
2018-02-06 22:31:18 +00:00
|
|
|
|
2017-08-29 18:19:21 +00:00
|
|
|
const IS_DEV = config.env === 'development';
|
2018-02-06 22:31:18 +00:00
|
|
|
const ID_REGEX = '([0-9a-fA-F]{10})';
|
2017-08-24 21:54:02 +00:00
|
|
|
|
|
|
|
module.exports = function(app) {
|
|
|
|
app.use(helmet());
|
|
|
|
app.use(
|
|
|
|
helmet.hsts({
|
|
|
|
maxAge: 31536000,
|
2017-08-29 18:19:21 +00:00
|
|
|
force: !IS_DEV
|
2017-08-24 21:54:02 +00:00
|
|
|
})
|
|
|
|
);
|
2018-08-07 22:40:17 +00:00
|
|
|
app.use(function(req, res, next) {
|
|
|
|
req.cspNonce = crypto.randomBytes(16).toString('hex');
|
|
|
|
next();
|
|
|
|
});
|
2017-11-30 21:41:09 +00:00
|
|
|
if (!IS_DEV) {
|
|
|
|
app.use(
|
|
|
|
helmet.contentSecurityPolicy({
|
|
|
|
directives: {
|
|
|
|
defaultSrc: ["'self'"],
|
|
|
|
connectSrc: [
|
|
|
|
"'self'",
|
2018-07-12 21:27:49 +00:00
|
|
|
'wss://*.dev.lcip.org',
|
|
|
|
'wss://*.mozaws.net',
|
|
|
|
'wss://send.firefox.com',
|
2017-11-30 21:41:09 +00:00
|
|
|
'https://sentry.prod.mozaws.net',
|
|
|
|
'https://www.google-analytics.com'
|
|
|
|
],
|
2018-08-07 22:40:17 +00:00
|
|
|
imgSrc: [
|
|
|
|
"'self'",
|
|
|
|
'https://www.google-analytics.com',
|
|
|
|
'https://*.dev.lcip.org',
|
|
|
|
'https://firefoxusercontent.com'
|
|
|
|
],
|
|
|
|
scriptSrc: [
|
|
|
|
"'self'",
|
|
|
|
function(req) {
|
|
|
|
return `'nonce-${req.cspNonce}'`;
|
|
|
|
}
|
|
|
|
],
|
2018-02-26 19:48:28 +00:00
|
|
|
styleSrc: ["'self'", 'https://code.cdn.mozilla.net'],
|
2017-11-30 21:41:09 +00:00
|
|
|
fontSrc: ["'self'", 'https://code.cdn.mozilla.net'],
|
|
|
|
formAction: ["'none'"],
|
|
|
|
frameAncestors: ["'none'"],
|
|
|
|
objectSrc: ["'none'"],
|
|
|
|
reportUri: '/__cspreport__'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2017-11-06 21:36:36 +00:00
|
|
|
app.use(function(req, res, next) {
|
|
|
|
res.set('Pragma', 'no-cache');
|
|
|
|
res.set('Cache-Control', 'no-cache');
|
|
|
|
next();
|
|
|
|
});
|
2018-02-25 02:00:43 +00:00
|
|
|
app.use(express.json());
|
2018-08-07 22:40:17 +00:00
|
|
|
app.get('/', language, pages.index);
|
2018-02-06 22:31:18 +00:00
|
|
|
app.get('/legal', language, pages.legal);
|
2017-08-24 21:54:02 +00:00
|
|
|
app.get('/jsconfig.js', require('./jsconfig'));
|
2018-02-06 22:31:18 +00:00
|
|
|
app.get(`/share/:id${ID_REGEX}`, language, pages.blank);
|
|
|
|
app.get(`/download/:id${ID_REGEX}`, language, pages.download);
|
|
|
|
app.get('/completed', language, pages.blank);
|
|
|
|
app.get('/unsupported/:reason', language, pages.unsupported);
|
2018-08-07 22:40:17 +00:00
|
|
|
app.get(`/api/download/:id${ID_REGEX}`, auth.hmac, require('./download'));
|
|
|
|
app.get(
|
|
|
|
`/api/download/blob/:id${ID_REGEX}`,
|
|
|
|
auth.hmac,
|
|
|
|
require('./download')
|
|
|
|
);
|
2018-02-06 22:31:18 +00:00
|
|
|
app.get(`/api/exists/:id${ID_REGEX}`, require('./exists'));
|
2018-08-07 22:40:17 +00:00
|
|
|
app.get(`/api/metadata/:id${ID_REGEX}`, auth.hmac, require('./metadata'));
|
|
|
|
app.get('/api/fxa/login', fxa.login);
|
|
|
|
app.get('/api/fxa/oauth', fxa.oauth);
|
|
|
|
app.get('/api/filelist', auth.fxa, filelist.get);
|
|
|
|
app.post('/api/filelist', auth.fxa, filelist.post);
|
|
|
|
app.post('/api/upload', auth.fxa, require('./upload'));
|
|
|
|
app.post(`/api/delete/:id${ID_REGEX}`, auth.owner, require('./delete'));
|
|
|
|
app.post(`/api/password/:id${ID_REGEX}`, auth.owner, require('./password'));
|
2018-08-31 17:59:26 +00:00
|
|
|
app.post(
|
|
|
|
`/api/params/:id${ID_REGEX}`,
|
|
|
|
auth.owner,
|
|
|
|
auth.fxa,
|
|
|
|
require('./params')
|
|
|
|
);
|
2018-08-07 22:40:17 +00:00
|
|
|
app.post(`/api/info/:id${ID_REGEX}`, auth.owner, require('./info'));
|
2017-08-24 21:54:02 +00:00
|
|
|
|
|
|
|
app.get('/__version__', function(req, res) {
|
|
|
|
res.sendFile(require.resolve('../../dist/version.json'));
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/__lbheartbeat__', function(req, res) {
|
|
|
|
res.sendStatus(200);
|
|
|
|
});
|
|
|
|
|
2017-08-25 17:03:49 +00:00
|
|
|
app.get('/__heartbeat__', async (req, res) => {
|
2017-08-24 21:54:02 +00:00
|
|
|
try {
|
|
|
|
await storage.ping();
|
|
|
|
res.sendStatus(200);
|
|
|
|
} catch (e) {
|
|
|
|
res.sendStatus(500);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|