diff options
author | cel <cel@lOUVT+Phkvai9a/cCS/RKo+S9hnPAQdVixms/7ldpPA=.ed25519> | 2020-03-28 15:58:43 -0400 |
---|---|---|
committer | cel <cel@lOUVT+Phkvai9a/cCS/RKo+S9hnPAQdVixms/7ldpPA=.ed25519> | 2020-04-10 16:11:50 -0400 |
commit | 9ad733614f9de494ff952b9c9f72b834bfb8252b (patch) | |
tree | 470fdc2383d9606d8e1075bbb4d73d8a3b0affce /lib | |
parent | 5a719f74ab1963f17275e28ae0aa9779b4b1ec03 (diff) | |
download | patchfoo-9ad733614f9de494ff952b9c9f72b834bfb8252b.tar.gz patchfoo-9ad733614f9de494ff952b9c9f72b834bfb8252b.zip |
Restrict access based on Referer
Diffstat (limited to 'lib')
-rw-r--r-- | lib/app.js | 38 | ||||
-rw-r--r-- | lib/serve.js | 22 |
2 files changed, 59 insertions, 1 deletions
@@ -25,6 +25,7 @@ var fs = require('fs') var mkdirp = require('mkdirp') var Base64URL = require('base64-url') var ssbKeys = require('ssb-keys') +var Url = require('url') var zeros = new Buffer(24); zeros.fill(0) @@ -81,6 +82,27 @@ function App(sbot, config) { } }).filter(Boolean) + this.trustedReferers = u.toArray(conf.trustedReferers || ['http://localhost:/', 'http://127.0.0.1:/', 'http://[::1]:/', 'http://' + this.hostname + '/']) + this.trustedReferersParsed = this.trustedReferers.indexOf('*') > -1 ? [{ + subdomains: true, + host: '', + port: '*', + }] : this.trustedReferers.map(function (pattern) { + var m = /^([a-z0-9\-+]+:)?\/\/+(\.)?(?:\[([0-9a-f:]*)\]|([^:/]*))(:([0-9]*?|\*))?(\/.*?)?$/.exec(pattern) + if (!m) return void console.trace('Unable to parse URL pattern "'+pattern+'"') + var port = !m[5] ? 80 : + !m[6] ? Number(self.port) : + '*' === m[6] ? '*' : Number(m[6]) + if (port !== '*' && isNaN(port)) return void console.trace('Unable to parse port in URL pattern "'+pattern+'". Default port: "'+self.port+'"') + return { + protocol: m[1], + subdomains: !!m[2], + hostname: m[3] || m[4], + port: port, + path: m[7] + } + }).filter(Boolean) + var base = conf.base || '/' this.opts = { base: base, @@ -152,6 +174,22 @@ App.prototype.isAllowedHostHeader = function (hostname) { return false } +App.prototype.getRefererPath = function (referer) { + if (!referer) return + var url = Url.parse(referer) + var port = Number(url.port || 80) + for (var i = 0; i < this.trustedReferersParsed.length; i++) { + var allow = this.trustedReferersParsed[i] + if ((!allow.protocol || allow.protocol === url.protocol) + && (allow.port === '*' || allow.port === port) + && (allow.hostname === '' || allow.hostname === url.hostname || + (allow.subdomains && host.endsWith('.'+allow.hostname))) + && (!allow.path || url.pathname.startsWith(allow.path)) + ) return allow.path ? url.pathname.substr(allow.path.length) : url.pathname + } + return null +} + App.prototype.go = function () { var self = this var server = http.createServer(function (req, res) { diff --git a/lib/serve.js b/lib/serve.js index fdf24f7..414cef5 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -122,6 +122,19 @@ Serve.prototype.go = function () { Boolean(conf.replyMentionFeeds) if (this.req.method === 'POST' || this.req.method === 'PUT') { + var referer = this.req.headers.referer + var refererPath = this.app.getRefererPath(referer) + if (!refererPath) { + if (!referer) console.error('Missing referer') + else console.error('Referer not allowed: "' + referer + '"') + this.res.writeHead(403) + return this.res.end('Forbidden') + } + if (this.isUnsafePath(refererPath)) { + console.error('Unsafe referer path not allowed: "' + refererPath + '"') + this.res.writeHead(403) + return this.res.end('Forbidden') + } if (/^multipart\/form-data/.test(this.req.headers['content-type'])) { var data = {} var erred @@ -213,6 +226,14 @@ Serve.prototype.go = function () { } } +Serve.prototype.isUnsafePath = function (path) { + return typeof path !== 'string' + || /^&|^%26/.test(path) + || path.indexOf('../') !== -1 + || path.startsWith('/web/') + || path.startsWith('/npm-readme/') +} + Serve.prototype.saveDraft = function (content, cb) { var self = this var data = self.data @@ -2426,7 +2447,6 @@ Serve.prototype.wrapPage = function (title, searchQ) { var done = multicb({pluck: 1, spread: true}) done()(null, h('html', h('head', h('meta', {charset: 'utf-8'}), - h('meta', {name: 'referrer', content: 'no-referrer'}), h('title', title), h('meta', {name: 'viewport', content: 'width=device-width,initial-scale=1'}), h('link', {rel: 'icon', href: render.toUrl('/static/hermie.ico'), type: 'image/x-icon'}), |