forked from ssbc/patchwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-server.js
187 lines (167 loc) · 5.42 KB
/
http-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var pull = require('pull-stream')
var toPull = require('stream-to-pull-stream')
var cat = require('pull-cat')
var ident = require('pull-identify-filetype')
var mime = require('mime-types')
var URL = require('url')
var path = require('path')
var fs = require('fs')
var refs = require('ssb-ref')
var Stack = require('stack')
var ip = require('ip')
var AppCSP = function (req, config) {
// hostname for the websocket connection:
// if it's a remote request, always use the configured hostname
// if it's local, choose from localhost or the configured hostname, based on which the client is using (as revealed by the host header)
var host
if (!ip.isLoopback(req.socket.remoteAddress))
host = config.getHostname()
else {
var requestHostname = req.headers.host.split(':')[0] // extract hostname (remove ':port')
host = (requestHostname == 'localhost' || requestHostname == config.getHostname())
? requestHostname
: (config.getHostname() || 'localhost')
}
return "default-src 'self'; "+
"connect-src 'self' ws://"+host+":7778 wss://"+host+":7778; "+
"img-src 'self' data:; "+
"object-src 'none'; "+
"frame-src 'none'; "+
"style-src 'self' 'unsafe-inline'; "+
"sandbox allow-modals allow-same-origin allow-scripts allow-top-navigation allow-popups"
}
var BlobCSP = function () { return "default-src none; sandbox" }
function respond (res, status, message) {
res.writeHead(status)
res.end(message)
}
function respondSource (res, source, wrap) {
if(wrap) {
res.writeHead(200, {'Content-Type': 'text/html'})
pull(
cat([
pull.once('<html><body><script>'),
source,
pull.once('</script></body></html>')
]),
toPull.sink(res)
)
}
else {
pull(
source,
ident(function (type) {
if (type) res.writeHead(200, {'Content-Type': mime.lookup(type)})
}),
toPull.sink(res)
)
}
}
var Log = exports.Log = function (sbot) {
return function (req, res, next) {
sbot.emit('log:info', ['HTTP', null, req.method + ' ' + req.url])
next()
}
}
var DeviceAccessControl = exports.DeviceAccessControl = function (config) {
return function (req, res, next) {
if (config.allowRemoteAccess())
return next() // remote & local access allowed
if (ip.isLoopback(req.socket.remoteAddress))
return next() // local access allowed
respond(res, 403, 'Remote access forbidden') // remote access disallowed
}
}
var PasswordAccessControl = exports.DeviceAccessControl = function (config) {
return function (req, res, next) {
if (!config.requiresPassword(config))
return next() // no password required
// check the password
var authMatch = /^Basic (.*)$/i.exec(req.headers.authorization)
if (authMatch) {
var password = (new Buffer(authMatch[1], 'base64').toString()).split(':')[1]
if (password && config.checkPassword(password))
return next() // password checks out
}
// deny
res.setHeader('WWW-Authenticate', 'Basic realm=Authorization Required')
respond(res, 401, 'Unauthorized')
}
}
var ServeApp = exports.ServeApp = function (sbot, opts, config) {
if (!opts || !opts.uiPath)
throw "opts.uiPath is required"
return function (req, res, next) {
var parsed = URL.parse(req.url, true)
var pathname = parsed.pathname
if (pathname == '/')
pathname = 'main.html'
var filepath = path.join(opts.uiPath, pathname)
fs.stat(filepath, function (err, stat) {
if(err) return next()
if(!stat.isFile()) return respond(res, 403, 'May only load files')
if (pathname == 'main.html')
res.setHeader('Content-Security-Policy', AppCSP(req, config)) // only give the open perms to main.html
else
res.setHeader('Content-Security-Policy', BlobCSP())
respondSource(
res,
toPull.source(fs.createReadStream(filepath)),
false
)
})
}
}
var ServeBlobs = exports.ServeBlobs = function (sbot) {
return function (req, res, next) {
var parsed = URL.parse(req.url, true)
var hash = decodeURIComponent(parsed.pathname.slice(1))
sbot.blobs.want(hash, function(err, has) {
if (!has) return respond(res, 404, 'File not found')
// optional name override
if (parsed.query.name)
res.setHeader('Content-Disposition', 'inline; filename='+encodeURIComponent(parsed.query.name))
// serve
res.setHeader('Content-Security-Policy', BlobCSP())
respondSource(res, sbot.blobs.get(hash), false)
})
}
}
var ServeFiles = exports.ServeFiles = function () {
return function (req, res, next) {
var parsed = URL.parse(req.url, true)
fs.stat(parsed.pathname, function (err, stat) {
if(err) return respond(res, 404, 'File not found')
if(!stat.isFile()) return respond(res, 403, 'May only load files')
res.setHeader('Content-Security-Policy', BlobCSP())
respondSource(
res,
toPull.source(fs.createReadStream(parsed.pathname)),
false
)
})
}
}
exports.BlobStack = function (sbot, opts) {
return Stack(
Log(sbot),
DeviceAccessControl(),
ServeBlobs(sbot)
)
}
exports.FileStack = function (opts) {
return Stack(
Log(sbot),
DeviceAccessControl(),
ServeFiles()
)
}
exports.AppStack = function (sbot, opts, config) {
return Stack(
Log(sbot),
PasswordAccessControl(config),
DeviceAccessControl(config),
ServeApp(sbot, opts, config),
ServeBlobs(sbot)
)
}