Skip to content

Commit

Permalink
tweak api, add linting
Browse files Browse the repository at this point in the history
  • Loading branch information
mixmix committed Dec 8, 2023
1 parent e919467 commit 927656e
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 82 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ explictly public:
```js
const explicitPublicMsg = {
content: { type: 'profile' },
options: { allowPublic: true }
allowPublic: true
}

server.publish(explicitPublicMsg, (err, msg) => {
Expand Down
72 changes: 38 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/* eslint-disable brace-style */
const get = require('lodash.get')

const isString = (t) => (typeof t === 'string')
const NotBothError = () => new Error(
'recps-guard: should not have recps && allowPublic, check your code'
)
const NotAllowedTypeError = (type) => new Error(
`recps-guard: public messages of type "${type}" not allowed`
)

module.exports = {
name: 'recpsGuard',
Expand All @@ -9,61 +17,57 @@ module.exports = {
},
init (ssb, config) {
const allowedTypes = getAllowedTypes(ssb, config)
const isAllowedType = (type) => allowedTypes.has(type)

const publishHook = (publish, args) => {
function publishHook (publish, args) {
const [input, cb] = args

if (get(input, ['options', 'allowPublic']) === true) {
// allowPublic and has recps, disallowed
if (hasRecps(input.content)) {
return cb(new Error('recps-guard: should not have recps && allowPublic, check your code'))
}
const isExplictAllow = (
input.allowPublic === true ||
get(input, ['options', 'allowPublic']) === true // legacy support
)

if (isExplictAllow) {
const content = input.content

// allowPublic and no recps, allowed
return publish(input.content, cb)
} else {
// without allowPublic, content isn't nested with db1 publish
if (hasRecps(content)) cb(NotBothError())
else publish(content, cb)
}
else {
const content = input

// no allowPublic and has recps/can publish without recps, allowed
if (
isString(content) ||
const isAllowed = (
isString(content) || // already encrypted
hasRecps(content) ||
allowedTypes.has(content.type)
) return publish(content, cb)
isAllowedType(content.type)
)

// no allowPublic and no recps, disallowed
return cb(new Error(`recps-guard: public messages of type "${content.type}" not allowed`))
if (isAllowed) publish(content, cb)
else cb(NotAllowedTypeError(content.type))
}
}

const createHook = (create, args) => {
function createHook (create, args) {
const [input, cb] = args

if (input.allowPublic === true) {
// allowPublic and has recps, disallowed
if (hasRecps(input.content)) {
return cb(new Error('recps-guard: should not have recps && allowPublic, check your code'))
}
if (hasRecps(input.content)) return cb(NotBothError())

// allowPublic and no recps, allowed
return create(input, cb)
} else {
// without allowPublic, content isn't nested with db1 publish
}
else {
const content = input.content

// no allowPublic and has recps/can publish without recps, allowed
if (
input.encryptionFormat ||
isString(content) ||
const isAllowed = (
isString(content) || // already encrypted
input.encryptionFormat || // signed up for encryption
hasRecps(content) ||
allowedTypes.has(content.type)
) return create(input, cb)
isAllowedType(content.type)
)

// no allowPublic and no recps, disallowed
return cb(new Error(`recps-guard: public messages of type "${content.type}" not allowed`))
if (isAllowed) create(input, cb)
else cb(NotAllowedTypeError(content.type))
}

}

if (ssb.publish) {
Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "guards against unencrypted messages being accidentally published!",
"main": "index.js",
"scripts": {
"test": "tape test/**/*.test.js | tap-spec"
"test": "npm run test:js && npm run lint",
"test:js": "tape test/**/*.test.js | tap-arc",
"lint": "standard --fix"
},
"repository": {
"type": "git",
Expand All @@ -21,6 +23,9 @@
"url": "https://github.com/ssbc/ssb-recps-guard/issues"
},
"homepage": "https://github.com/ssbc/ssb-recps-guard#readme",
"dependencies": {
"lodash.get": "^4.4.2"
},
"devDependencies": {
"scuttle-testbot": "^2.2.0",
"ssb-box": "^1.0.1",
Expand All @@ -30,10 +35,8 @@
"ssb-db2": "^8.1.0",
"ssb-private1": "^1.0.1",
"ssb-tribes": "^4.0.0",
"tap-spec": "^5.0.0",
"standard": "^17.1.0",
"tap-arc": "^1.2.2",
"tape": "^5.7.2"
},
"dependencies": {
"lodash.get": "^4.4.2"
}
}
89 changes: 52 additions & 37 deletions test/core-tests.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,54 @@
module.exports = (server, t, cb) => {
const msg = { type: 'profile' }
server.publish(msg, (err, data) => {
t.match(err.message, /recps-guard: public messages of type "profile" not allowed/, 'public blocked')

const msg = { type: 'profile', recps: [server.id] }
server.publish(msg, (err, data) => {
t.error(err, 'msgs with recps allowed')
t.equal(typeof data.value.content, 'string', '(msg content encrypted)')

const msg = Buffer.from('cats are cool').toString('base64') + '.box7'
server.publish(msg, (err, data) => {
t.error(err, 'pre-encrypted content published fine')
t.equal(typeof data.value.content, 'string', '(msg content encrypted)')

const content = { type: 'profile', name: 'mix' }
server.publish({ content, options: { allowPublic: true } }, (err, data) => {
if (err) return cb(err)
t.error(err, 'msgs { content, options: { allowPublic: true } allowed')
t.deepEqual(data.value.content, content, '(msg content unencrypted, allowPublic pruned)')

const weird = {
content: { type: 'profile', recps: [server.id] },
options: { allowPublic: true }
}
server.publish(weird, (err, data) => {
t.match(
err.message,
/recps-guard: should not have recps && allowPublic, check your code/,
'disallow recps AND allowPublic'
)

cb(null)
})
})
})
const { promisify: p } = require('util')

module.exports = async (server, t, cb) => {
let description, content, input

description = 'public blocked'
content = { type: 'profile' }
await p(server.publish)(content)
.then(() => t.fail(description))
.catch(err => {
t.match(err.message, /recps-guard: public messages of type "profile" not allowed/, description)
})
})

description = 'msgs with recps allowed'
content = { type: 'profile', recps: [server.id] }
await p(server.publish)(content)
.then(data => t.equal(typeof data.value.content, 'string', description))
.catch(err => t.error(err, description))

description = 'pre-encrypted content published fine'
content = Buffer.from('cats are cool').toString('base64') + '.box7'
await p(server.publish)(content)
.then(data => t.equal(typeof data.value.content, 'string', description))
.catch(err => t.fail(err, description))

description = 'msgs { content, allowPublic: true } allowed'
content = { type: 'profile', name: 'mix' }
input = { content, allowPublic: true }
await p(server.publish)(input)
.then(data => t.deepEqual(data.value.content, content, description))
.catch(err => t.fail(err, description))

description = 'legacy: msgs { content, options: { allowPublic: true } }'
content = { type: 'profile', name: 'mix' }
input = { content, options: { allowPublic: true } }
await p(server.publish)(input)
.then(data => t.deepEqual(data.value.content, content, description))
.catch(err => t.fail(err, description))

description = 'disallow recps AND allowPublic'
input = {
content: { type: 'profile', recps: [server.id] },
allowPublic: true
}
await p(server.publish)(input)
.then(() => t.fail(description))
.catch(err => t.match(
err.message,
/recps-guard: should not have recps && allowPublic, check your code/,
'disallow recps AND allowPublic'
))

cb(null)
}
8 changes: 4 additions & 4 deletions test/db2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const test = require('tape')
const Server = require('./test-bot')

test('db2', async t => {
const server = Server({db1: false})
const server = Server({ db1: false })
t.deepEqual(server.recpsGuard.allowedTypes(), [], 'recps.allowedTypes')

let content = { type: 'profile' }
await p(server.db.create)({content})
await p(server.db.create)({ content })
.then(msg => t.error(msg, "shouldn't get msg on err"))
.catch((err) => {
t.match(err.message, /recps-guard: public messages of type "profile" not allowed/, 'public blocked')
Expand All @@ -19,7 +19,7 @@ test('db2', async t => {
})

content = { type: 'profile', recps: [server.id] }
await p(server.db.create)({content})
await p(server.db.create)({ content })
.then(data => {
t.equal(typeof data.value.content, 'string', '(msg content encrypted)')
})
Expand Down Expand Up @@ -55,7 +55,7 @@ test('db2', async t => {
})

test('can create a group', async t => {
const server = Server({db1: false})
const server = Server({ db1: false })

const group = await p(server.tribes.create)({})
t.equal(typeof group.groupId, 'string', 'created group with groupId')
Expand Down
2 changes: 1 addition & 1 deletion test/install-order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test('installed in right order', t => {
test('installed in wrong order', { skip: true }, t => {
t.plan(2) // goodHook + throw

var server
let server
t.throws(
() => {
server = Server // eslint-disable-line
Expand Down

0 comments on commit 927656e

Please sign in to comment.