1
0
mirror of https://gitlab.com/timvisee/send.git synced 2024-11-14 07:02:30 +01:00
send/server/server.js

320 lines
7.1 KiB
JavaScript
Raw Normal View History

const express = require('express');
const exphbs = require('express-handlebars');
const busboy = require('connect-busboy');
const path = require('path');
const bodyParser = require('body-parser');
2017-06-20 00:51:48 +02:00
const helmet = require('helmet');
const conf = require('./config.js');
const storage = require('./storage.js');
2017-06-23 19:53:11 +02:00
const Raven = require('raven');
const crypto = require('crypto');
const fs = require('fs');
2017-08-02 23:13:53 +02:00
const version = require('../public/version.json');
2017-06-24 05:01:32 +02:00
if (conf.sentry_dsn) {
2017-06-23 19:53:11 +02:00
Raven.config(conf.sentry_dsn).install();
}
2017-06-08 22:45:28 +02:00
const mozlog = require('./log.js');
2017-07-11 21:34:49 +02:00
const log = mozlog('send.server');
2017-06-24 01:53:17 +02:00
const STATIC_PATH = path.join(__dirname, '../public');
const app = express();
function allLangs() {
return fs
.readdirSync(path.join(STATIC_PATH, 'locales'))
.map(function(f) {
return f.split('.')[0];
})
.join(',');
}
function prodLangs() {
return require('../package.json').availableLanguages.join(',');
}
const availableLanguages = conf.l10n_dev ? allLangs() : prodLangs();
if (conf.env === 'development') {
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const config = require('../webpack.config.js');
config.devtool = 'inline-source-map';
const compiler = webpack(config);
2017-08-10 19:02:13 +02:00
app.use(
webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath
})
);
}
2017-06-23 20:14:33 +02:00
app.engine(
'handlebars',
exphbs({
defaultLayout: 'main',
partialsDir: 'views/partials/',
helpers: {
availableLanguages,
2017-08-04 12:40:55 +02:00
l10nDev: conf.l10n_dev,
2017-08-11 23:02:44 +02:00
baseUrl: conf.base_url,
2017-08-04 12:40:55 +02:00
title: 'Firefox Send',
2017-08-12 00:59:14 +02:00
description:
'Encrypt and send files with a link that automatically expires to ensure your important documents dont stay online forever.'
}
2017-06-23 20:14:33 +02:00
})
);
app.set('view engine', 'handlebars');
2017-06-01 22:14:14 +02:00
2017-06-20 00:51:48 +02:00
app.use(helmet());
2017-07-22 02:01:26 +02:00
app.use(
helmet.hsts({
maxAge: 31536000,
force: conf.env === 'production'
})
);
2017-07-12 19:56:04 +02:00
app.use(
helmet.contentSecurityPolicy({
directives: {
2017-07-18 00:49:09 +02:00
defaultSrc: ["'self'"],
2017-07-12 19:56:04 +02:00
connectSrc: [
2017-07-18 00:49:09 +02:00
"'self'",
2017-07-12 19:56:04 +02:00
'https://sentry.prod.mozaws.net',
2017-07-25 07:08:43 +02:00
'https://www.google-analytics.com'
2017-07-12 19:56:04 +02:00
],
2017-07-25 21:08:37 +02:00
imgSrc: ["'self'", 'https://www.google-analytics.com'],
2017-07-25 07:08:43 +02:00
scriptSrc: ["'self'"],
2017-07-18 00:49:09 +02:00
styleSrc: ["'self'", 'https://code.cdn.mozilla.net'],
fontSrc: ["'self'", 'https://code.cdn.mozilla.net'],
formAction: ["'none'"],
frameAncestors: ["'none'"],
2017-07-25 07:08:43 +02:00
objectSrc: ["'none'"],
reportUri: '/__cspreport__'
2017-07-12 19:56:04 +02:00
}
})
);
2017-07-22 02:01:26 +02:00
app.use(
busboy({
limits: {
fileSize: conf.max_file_size
}
})
);
2017-06-01 22:14:14 +02:00
app.use(bodyParser.json());
2017-06-24 01:53:17 +02:00
app.use(express.static(STATIC_PATH));
2017-06-01 22:14:14 +02:00
app.get('/', (req, res) => {
res.render('index');
});
app.get('/unsupported/:reason', (req, res) => {
2017-07-27 20:54:36 +02:00
const outdated = req.params.reason === 'outdated';
res.render('unsupported', {
outdated,
fira: true
});
});
2017-07-24 19:24:17 +02:00
app.get('/legal', (req, res) => {
res.render('legal');
});
app.get('/jsconfig.js', (req, res) => {
res.set('Content-Type', 'application/javascript');
res.render('jsconfig', {
2017-08-02 23:13:53 +02:00
googleAnalyticsId: conf.analytics_id,
sentryId: conf.sentry_id,
version: version.version,
commit: version.commit,
2017-07-20 21:50:20 +02:00
maxFileSize: conf.max_file_size,
expireSeconds: conf.expire_seconds,
layout: false
});
});
app.get('/exists/:id', async (req, res) => {
2017-06-09 19:44:12 +02:00
const id = req.params.id;
2017-07-11 20:18:31 +02:00
if (!validateID(id)) {
res.sendStatus(404);
2017-07-11 20:18:31 +02:00
return;
}
try {
await storage.exists(id);
res.sendStatus(200);
} catch (e) {
res.sendStatus(404);
}
2017-06-08 22:45:28 +02:00
});
app.get('/download/:id', async (req, res) => {
2017-06-09 19:44:12 +02:00
const id = req.params.id;
2017-07-11 20:18:31 +02:00
if (!validateID(id)) {
res.status(404).render('notfound');
2017-07-11 20:18:31 +02:00
return;
}
try {
2017-08-06 03:06:43 +02:00
const efilename = await storage.filename(id);
const filename = decodeURIComponent(efilename);
const filenameJson = JSON.stringify({ filename });
const sizeInBytes = await storage.length(id);
2017-08-05 21:23:58 +02:00
const ttl = await storage.ttl(id);
res.render('download', {
2017-08-06 03:06:43 +02:00
filename,
filenameJson,
sizeInBytes,
2017-08-05 21:23:58 +02:00
ttl
2017-07-25 21:08:37 +02:00
});
} catch (e) {
res.status(404).render('notfound');
}
2017-06-01 22:14:14 +02:00
});
app.get('/assets/download/:id', async (req, res) => {
2017-06-09 19:44:12 +02:00
const id = req.params.id;
2017-06-06 23:24:51 +02:00
if (!validateID(id)) {
res.sendStatus(404);
2017-06-01 22:14:14 +02:00
return;
}
try {
const meta = await storage.metadata(id);
const contentLength = await storage.length(id);
res.writeHead(200, {
'Content-Disposition': `attachment; filename=${meta.filename}`,
2017-08-09 21:56:32 +02:00
'Content-Type': meta.mimeType,
'Content-Length': contentLength,
'X-File-Metadata': JSON.stringify(meta)
});
const file_stream = storage.get(id);
file_stream.on('end', async () => {
try {
2017-08-04 06:47:03 +02:00
await storage.forceDelete(id);
} catch (e) {
log.info('DeleteError:', id);
}
});
file_stream.pipe(res);
} catch (e) {
res.sendStatus(404);
}
2017-06-01 22:14:14 +02:00
});
app.post('/delete/:id', async (req, res) => {
2017-06-09 19:44:12 +02:00
const id = req.params.id;
2017-06-01 22:14:14 +02:00
2017-06-06 23:24:51 +02:00
if (!validateID(id)) {
res.sendStatus(404);
2017-06-01 22:14:14 +02:00
return;
}
2017-06-06 23:24:51 +02:00
2017-06-09 19:44:12 +02:00
const delete_token = req.body.delete_token;
2017-06-06 23:24:51 +02:00
if (!delete_token) {
res.sendStatus(404);
2017-07-11 20:18:31 +02:00
return;
2017-06-01 22:14:14 +02:00
}
try {
2017-08-03 08:01:13 +02:00
const err = await storage.delete(id, delete_token);
if (!err) {
res.sendStatus(200);
}
} catch (e) {
res.sendStatus(404);
}
2017-06-01 22:14:14 +02:00
});
app.post('/upload', (req, res, next) => {
const newId = crypto.randomBytes(5).toString('hex');
2017-07-11 21:47:40 +02:00
let meta;
2017-07-11 21:34:49 +02:00
2017-07-11 21:47:40 +02:00
try {
meta = JSON.parse(req.header('X-File-Metadata'));
} catch (e) {
2017-07-11 21:47:40 +02:00
res.sendStatus(400);
return;
}
2017-07-11 20:18:31 +02:00
2017-07-12 19:53:29 +02:00
if (
!meta.hasOwnProperty('id') ||
2017-07-13 23:56:28 +02:00
!meta.hasOwnProperty('filename') ||
!validateIV(meta.id)
2017-07-12 19:53:29 +02:00
) {
res.sendStatus(404);
2017-07-11 20:18:31 +02:00
return;
}
meta.delete = crypto.randomBytes(10).toString('hex');
req.pipe(req.busboy);
2017-06-20 23:33:28 +02:00
2017-08-09 21:56:32 +02:00
req.busboy.on(
'file',
async (fieldname, file, filename, encoding, mimeType) => {
try {
meta.mimeType = mimeType || 'application/octet-stream';
await storage.set(newId, file, filename, meta);
const protocol = conf.env === 'production' ? 'https' : req.protocol;
const url = `${protocol}://${req.get('host')}/download/${newId}/`;
res.json({
url,
delete: meta.delete,
id: newId
});
} catch (e) {
if (e.message === 'limit') {
return res.sendStatus(413);
}
res.sendStatus(500);
2017-07-20 21:50:20 +02:00
}
}
2017-08-09 21:56:32 +02:00
);
2017-07-18 19:52:32 +02:00
req.on('close', async err => {
try {
2017-08-04 06:47:03 +02:00
await storage.forceDelete(newId);
} catch (e) {
log.info('DeleteError:', newId);
}
2017-07-22 02:01:26 +02:00
});
2017-06-01 22:14:14 +02:00
});
2017-06-22 21:18:07 +02:00
app.get('/__lbheartbeat__', (req, res) => {
res.sendStatus(200);
});
app.get('/__heartbeat__', async (req, res) => {
try {
await storage.ping();
res.sendStatus(200);
} catch (e) {
res.sendStatus(500);
}
2017-06-23 20:14:33 +02:00
});
2017-06-23 20:29:45 +02:00
app.get('/__version__', (req, res) => {
2017-06-24 01:53:17 +02:00
res.sendFile(path.join(STATIC_PATH, 'version.json'));
2017-06-23 20:29:45 +02:00
});
2017-07-11 21:47:40 +02:00
const server = app.listen(conf.listen_port, () => {
2017-07-11 21:34:49 +02:00
log.info('startServer:', `Send app listening on port ${conf.listen_port}!`);
2017-06-06 23:24:51 +02:00
});
2017-06-01 22:14:14 +02:00
2017-06-09 19:44:12 +02:00
const validateID = route_id => {
2017-07-07 23:47:56 +02:00
return route_id.match(/^[0-9a-fA-F]{10}$/) !== null;
2017-07-11 20:18:31 +02:00
};
const validateIV = route_id => {
return route_id.match(/^[0-9a-fA-F]{24}$/) !== null;
2017-07-11 21:47:40 +02:00
};
module.exports = {
server: server,
storage: storage
2017-07-12 19:53:29 +02:00
};