Skip to content

Commit

Permalink
feat(backend): secure the api with helmet
Browse files Browse the repository at this point in the history
  • Loading branch information
Wassim CHEGHAM committed Apr 19, 2018
1 parent 35104bb commit 8fb742d
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 10 deletions.
24 changes: 15 additions & 9 deletions functions/index.js
@@ -1,16 +1,14 @@
const functions = require('firebase-functions');
const express = require('express');
const helmet = require('helmet');
const cookieParser = require('cookie-parser')();
const cors = require('cors')({ origin: true });
const app = express();

const algoliaIndex = require('./algolia');

const FAKE_TOKEN = 'this-is-a-fake-token';
const BEARER = 'SearchToken';

const validateAuthorizedToken = (req, res, next) => {
console.log('Check if request is authorized with the correct Agent');
console.log('Check if request is authorized with the correct header');

if ((!req.headers.authorization || !req.headers.authorization.startsWith(`${BEARER} `)) && !req.cookies.__session) {
console.error('No valid token was passed as a Bearer token in the Authorization header.');
Expand All @@ -19,21 +17,29 @@ const validateAuthorizedToken = (req, res, next) => {
}

if (req.headers.authorization && req.headers.authorization.startsWith(`${BEARER} `)) {
console.log('Found "Authorization" header');
const idToken = req.headers.authorization.split(`${BEARER} `)[1];
console.log(`Found "Authorization" header with bearer: ${idToken}`);

// accept any token (for the sake of this demo app)
if (idToken === FAKE_TOKEN) {
return next();
}
return next();
}

res.status(403).send('Unauthorized');
return;
};

const filterMethods = (req, res, next) => {
if (['post', 'delete'].includes(req.method.toLowerCase())) {
next();
} else {
res.status(405).send('Method Not Allowed');
}
};

app.use(helmet());
app.use(cors);
app.use(cookieParser);
app.use(filterMethods);
app.use(validateAuthorizedToken);
app.post('/1/apps', (req, res) => {
console.log(`Adding: ${JSON.stringify(req.body)}`);
Expand All @@ -47,7 +53,7 @@ app.post('/1/apps', (req, res) => {
})
.catch(error => {
console.error(error);

res.status(500).send({
error
});
Expand Down
3 changes: 2 additions & 1 deletion functions/package.json
Expand Up @@ -8,7 +8,8 @@
"dotenv": "^5.0.1",
"express": "^4.16.3",
"firebase-admin": "^5.12.0",
"firebase-functions": "^1.0.1"
"firebase-functions": "^1.0.1",
"helmet": "^3.12.0"
},
"private": true
}
91 changes: 91 additions & 0 deletions functions/yarn.lock
Expand Up @@ -548,6 +548,10 @@ camelcase@^2.0.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-2.1.1.tgz#7c1d16d679a1bbe59ca02cacecfb011e201f5a1f"

camelize@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/camelize/-/camelize-1.0.0.tgz#164a5483e630fa4321e5af07020e531831b2609b"

capture-stack-trace@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/capture-stack-trace/-/capture-stack-trace-1.0.0.tgz#4a6fa07399c26bba47f0b2496b4d0fb408c5550d"
Expand Down Expand Up @@ -640,6 +644,10 @@ content-disposition@0.5.2:
version "0.5.2"
resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.2.tgz#0cf68bb9ddf5f2be7961c3a85178cb85dba78cb4"

content-security-policy-builder@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/content-security-policy-builder/-/content-security-policy-builder-2.0.0.tgz#8749a1d542fcbe82237281ea9f716ce68b394dd2"

content-type@~1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.4.tgz#e138cc75e040c727b1966fe5e5f8c9aee256fe3b"
Expand Down Expand Up @@ -700,6 +708,10 @@ dashdash@^1.12.0:
dependencies:
assert-plus "^1.0.0"

dasherize@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/dasherize/-/dasherize-2.0.0.tgz#6d809c9cd0cf7bb8952d80fc84fa13d47ddb1308"

debug@2.6.9, debug@^2.2.0, debug@^2.3.3, debug@^2.6.8:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down Expand Up @@ -789,10 +801,18 @@ dir-glob@^2.0.0:
arrify "^1.0.1"
path-type "^3.0.0"

dns-prefetch-control@0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/dns-prefetch-control/-/dns-prefetch-control-0.1.0.tgz#60ddb457774e178f1f9415f0cabb0e85b0b300b2"

dom-walk@^0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/dom-walk/-/dom-walk-0.1.1.tgz#672226dc74c8f799ad35307df936aba11acd6018"

dont-sniff-mimetype@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/dont-sniff-mimetype/-/dont-sniff-mimetype-1.0.0.tgz#5932890dc9f4e2f19e5eb02a20026e5e5efc8f58"

dot-prop@^4.1.0, dot-prop@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/dot-prop/-/dot-prop-4.2.0.tgz#1f19e0c2e1aa0e32797c49799f2837ac6af69c57"
Expand Down Expand Up @@ -910,6 +930,10 @@ expand-brackets@^2.1.4:
snapdragon "^0.8.1"
to-regex "^3.0.1"

expect-ct@0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/expect-ct/-/expect-ct-0.1.0.tgz#52735678de18530890d8d7b95f0ac63640958094"

express@^4.16.2, express@^4.16.3:
version "4.16.3"
resolved "https://registry.yarnpkg.com/express/-/express-4.16.3.tgz#6af8a502350db3246ecc4becf6b5a34d22f7ed53"
Expand Down Expand Up @@ -1097,6 +1121,10 @@ fragment-cache@^0.2.1:
dependencies:
map-cache "^0.2.2"

frameguard@3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/frameguard/-/frameguard-3.0.0.tgz#7bcad469ee7b96e91d12ceb3959c78235a9272e9"

fresh@0.5.2:
version "0.5.2"
resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7"
Expand Down Expand Up @@ -1361,6 +1389,37 @@ hawk@~6.0.2:
hoek "4.x.x"
sntp "2.x.x"

helmet-csp@2.7.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/helmet-csp/-/helmet-csp-2.7.0.tgz#7934094617d1feb7bb2dc43bb7d9e8830f774716"
dependencies:
camelize "1.0.0"
content-security-policy-builder "2.0.0"
dasherize "2.0.0"
lodash.reduce "4.6.0"
platform "1.3.5"

helmet@^3.12.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/helmet/-/helmet-3.12.0.tgz#2098e35cf4e51c64c2f1d38670b7d382a377d92c"
dependencies:
dns-prefetch-control "0.1.0"
dont-sniff-mimetype "1.0.0"
expect-ct "0.1.0"
frameguard "3.0.0"
helmet-csp "2.7.0"
hide-powered-by "1.0.0"
hpkp "2.0.0"
hsts "2.1.0"
ienoopen "1.0.0"
nocache "2.0.0"
referrer-policy "1.1.0"
x-xss-protection "1.1.0"

hide-powered-by@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/hide-powered-by/-/hide-powered-by-1.0.0.tgz#4a85ad65881f62857fc70af7174a1184dccce32b"

hoek@2.x.x:
version "2.16.3"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-2.16.3.tgz#20bb7403d3cea398e91dc4710a8ff1b8274a25ed"
Expand All @@ -1369,6 +1428,14 @@ hoek@4.x.x:
version "4.2.1"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.2.1.tgz#9634502aa12c445dd5a7c5734b572bb8738aacbb"

hpkp@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/hpkp/-/hpkp-2.0.0.tgz#10e142264e76215a5d30c44ec43de64dee6d1672"

hsts@2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/hsts/-/hsts-2.1.0.tgz#cbd6c918a2385fee1dd5680bfb2b3a194c0121cc"

http-errors@1.6.2:
version "1.6.2"
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.6.2.tgz#0a002cc85707192a7e7946ceedc11155f60ec736"
Expand Down Expand Up @@ -1403,6 +1470,10 @@ iconv-lite@0.4.19:
version "0.4.19"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b"

ienoopen@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/ienoopen/-/ienoopen-1.0.0.tgz#346a428f474aac8f50cf3784ea2d0f16f62bda6b"

ignore@^3.3.5:
version "3.3.7"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.7.tgz#612289bfb3c220e186a58118618d5be8c1bab021"
Expand Down Expand Up @@ -1720,6 +1791,10 @@ lodash.once@^4.0.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"

lodash.reduce@4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.reduce/-/lodash.reduce-4.6.0.tgz#f1ab6b839299ad48f784abbf476596f03b914d3b"

lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.2, lodash@^4.6.1:
version "4.17.5"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"
Expand Down Expand Up @@ -1889,6 +1964,10 @@ negotiator@0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.1.tgz#2b327184e8992101177b28563fb5e7102acd0ca9"

nocache@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/nocache/-/nocache-2.0.0.tgz#202b48021a0c4cbde2df80de15a17443c8b43980"

node-forge@0.7.4:
version "0.7.4"
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.7.4.tgz#8e6e9f563a1e32213aa7508cded22aa791dbf986"
Expand Down Expand Up @@ -2035,6 +2114,10 @@ pify@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176"

platform@1.3.5:
version "1.3.5"
resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.5.tgz#fb6958c696e07e2918d2eeda0f0bc9448d733444"

posix-character-classes@^0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"
Expand Down Expand Up @@ -2249,6 +2332,10 @@ reduce@^1.0.1:
dependencies:
object-keys "~1.0.0"

referrer-policy@1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/referrer-policy/-/referrer-policy-1.1.0.tgz#35774eb735bf50fb6c078e83334b472350207d79"

regex-not@^1.0.0, regex-not@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/regex-not/-/regex-not-1.0.2.tgz#1f4ece27e00b0b65e0247a6810e6a85d83a5752c"
Expand Down Expand Up @@ -2788,6 +2875,10 @@ write-file-atomic@^2.0.0:
imurmurhash "^0.1.4"
signal-exit "^3.0.2"

x-xss-protection@1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/x-xss-protection/-/x-xss-protection-1.1.0.tgz#4f1898c332deb1e7f2be1280efb3e2c53d69c1a7"

xdg-basedir@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-3.0.0.tgz#496b2cc109eca8dbacfe2dc72b603c17c5870ad4"
Expand Down

0 comments on commit 8fb742d

Please sign in to comment.