git ssb

9+

cel / ssb-viewer



Commit ad80496c9aaec127be8cd80533c253901b618812

Decrypt private blobs

cel committed on 1/25/2019, 10:59:04 PM
Parent: 7291151076ec1a461b2e5839d1136ab17ed833ac

Files changed

index.jschanged
package.jsonchanged
index.jsView
@@ -11,8 +11,9 @@
1111 var lru = require('lrucache')
1212 var webresolve = require('ssb-web-resolver')
1313 var serveEmoji = require('emoji-server')()
1414 var refs = require('ssb-ref')
15 +var BoxStream = require('pull-box-stream')
1516 var h = require('hyperscript')
1617 var {
1718 MdRenderer,
1819 renderEmoji,
@@ -28,8 +29,10 @@
2829 var appHash = hash([fs.readFileSync(__filename)])
2930
3031 var urlIdRegex = /^(?:\/(([%&@]|%25|%26|%40)(?:[A-Za-z0-9\/+]|%2[Ff]|%2[Bb]){43}(?:=|%3[Dd])\.(?:sha256|ed25519))(?:\.([^?]*))?|(\/.*?))(?:\?(.*))?$/
3132
33 +var zeros = new Buffer(24); zeros.fill(0)
34 +
3235 function hash(arr) {
3336 return arr.reduce(function (hash, item) {
3437 return hash.update(String(item))
3538 }, crypto.createHash('sha256')).digest('base64')
@@ -98,9 +101,9 @@
98101 }
99102 switch (m[2]) {
100103 case '%': return serveId(req, res, m[1], m[3], m[5])
101104 case '@': return serveFeed(req, res, m[1], m[3], m[5])
102- case '&': return serveBlob(req, res, sbot, m[1])
105 + case '&': return serveBlob(req, res, sbot, m[1], m[5])
103106 }
104107
105108 if (m[4] === '/') return serveHome(req, res, m[5])
106109 return respond(res, 404, 'Not found')
@@ -460,22 +463,36 @@
460463 cb(null, msg)
461464 }
462465 }
463466
464-function serveBlob(req, res, sbot, id) {
465- if (req.headers['if-none-match'] === id) return respond(res, 304)
467 +function serveBlob(req, res, sbot, id, query) {
468 + var q = query && qs.parse(query)
469 + var unbox = q && typeof q.unbox === 'string' && q.unbox.replace(/\s/g, '+')
470 + var etag = id + (unbox || '')
471 +
472 + if (req.headers['if-none-match'] === etag) return respond(res, 304)
466473 sbot.blobs.has(id, function (err, has) {
467474 if (err) {
468475 if (/^invalid/.test(err.message)) return respond(res, 400, err.message)
469476 else return respond(res, 500, err.message || err)
470477 }
471478 if (!has) return respond(res, 404, 'Not found')
479 +
480 + var unboxKey
481 + if (unbox) {
482 + try { unboxKey = new Buffer(unbox, 'base64') }
483 + catch(e) { return respond(res, 400, err.message) }
484 + if (unboxKey.length !== 32) return respond(res, 400, 'Bad blob key')
485 + }
486 +
472487 res.writeHead(200, {
473488 'Cache-Control': 'public, max-age=315360000',
474- 'etag': id
489 + 'etag': etag
475490 })
491 +
476492 pull(
477493 sbot.blobs.get(id),
494 + unboxKey ? BoxStream.createUnboxStream(unboxKey, zeros) : null,
478495 toPull(res, function (err) {
479496 if (err) console.error('[viewer]', err)
480497 })
481498 )
package.jsonView
@@ -10,8 +10,9 @@
1010 "emoji-server": "^1.0.0",
1111 "human-time": "^0.0.1",
1212 "hyperscript": "2.0.2",
1313 "lrucache": "^1.0.2",
14 + "pull-box-stream": "^1.0.13",
1415 "pull-cat": "^1.1.11",
1516 "pull-paramap": "^1.2.1",
1617 "pull-stream": "^3.5.0",
1718 "ssb-acme-validator": "http://localhost:8989/blobs/get/&MgpztmIbg8wShqDXBcKt0w78qwcpNe8qb4n3fsveve8=.sha256",

Built with git-ssb-web