git ssb

0+

Dominic / ssb-peer-invites



Tree: faa577025470b36627e359489a115c27c490a730

Files: faa577025470b36627e359489a115c27c490a730 / test / invalid.js

5382 bytesRaw
1var tape = require('tape')
2var ssbKeys = require('ssb-keys')
3var v = require('ssb-validate')
4var i = require('../valid')
5var u = require('../util')
6
7var invite_key = require('../cap')
8
9var hash = u.hash
10
11var alice = ssbKeys.generate(null, hash('ALICE'))
12var bob = ssbKeys.generate(null, hash('BOB'))
13
14
15function throws(t, test, code) {
16 if(!code) throw new Error('error code must be provided')
17 try {
18 test()
19 t.fail('expected:'+test+' to throw code:'+code)
20 } catch(err) {
21 console.error(err.stack)
22 t.ok(err.code, 'errors must have an error code')
23 t.equal(err.code, code)
24 }
25}
26
27//any bit in invite{invite,host,reveal} is flipped
28tape('invalid - wrong invitee', function (t) {
29
30 //construct a message where host does not match
31 var seed = hash('seed2')
32 var keys = ssbKeys.generate(null, seed)
33 var invalid = ssbKeys.signObj(keys, invite_key, {
34 type: 'user-invite',
35 invite: ssbKeys.generate(null, hash('seed3')),
36 host: alice.id
37 })
38
39 var msg = v.create(null, alice, null, invalid, new Date('2018-03-26T06:14:18.377Z'))
40
41 throws(t, function () {
42 i.verifyInvitePublic(msg)
43 }, 'user-invites:invite-signature-failed')
44
45 throws(t, function () {
46 i.verifyInvitePrivate(msg)
47 }, 'user-invites:invite-signature-failed')
48
49 t.end()
50})
51
52//any bit in invite{invite,host,reveal} is flipped
53tape('invalid - wrong invitee', function (t) {
54
55 //construct a message where host does not match
56 var seed = hash('seed2')
57 var keys = ssbKeys.generate(null, seed)
58 var wrong_seed = hash('wrong_seed')
59 var invalid = ssbKeys.signObj(keys, invite_key, {
60 type: 'user-invite',
61 invite: keys.id, //correct key
62 reveal: u.box({hidden: true}, u.hash(u.hash(wrong_seed))),
63 host: alice.id
64 })
65 var invite_msg = v.create(null, alice, null, invalid, new Date('2018-03-26T06:14:18.377Z'))
66
67 t.ok(i.verifyInvitePublic(invite_msg))
68
69 throws(t, function () {
70 i.verifyInvitePrivate(invite_msg, seed)
71 }, 'user-invites:decrypt-reveal-failed')
72
73 //say if the invitee creates a accept message anyway.
74
75 throws(t, function () {
76 i.createAccept(invite_msg, wrong_seed, bob.id)
77 }, 'user-invites:seed-must-match-invite')
78
79
80 throws(t, function () {
81 i.createAccept(invite_msg, seed, bob.id)
82 }, 'user-invites:decrypt-reveal-failed')
83
84 var accept = ssbKeys.signObj(ssbKeys.generate(null, seed), invite_key, {
85 type: 'user-invite/accept',
86 receipt: '%'+ssbKeys.hash(JSON.stringify(invite_msg, null, 2)),
87 id: bob.id,
88 key: u.hash(u.hash(seed)) //what the reveal key should be.
89 })
90
91 var accept_msg =
92 v.create(null, bob, null, accept, new Date('2018-03-26T06:14:18.377Z'))
93
94 throws(t, function () {
95 i.verifyAccept(accept_msg, invite_msg)
96 }, 'user-invites:decrypt-accept-reveal-failed')
97
98 var accept2 = ssbKeys.signObj(ssbKeys.generate(null, seed), invite_key, {
99 type: 'user-invite/accept',
100 receipt: '%'+ssbKeys.hash(JSON.stringify(invite_msg, null, 2)),
101 id: bob.id,
102 key: u.hash('not the key') //what the reveal key should be.
103 })
104
105 throws(t, function () {
106 i.verifyAccept(accept_msg, invite_msg)
107 }, 'user-invites:decrypt-accept-reveal-failed')
108
109 t.end()
110})
111
112tape('wrong invite', function (t) {
113 var seed = hash('seed1')
114
115 var invite1 = v.create(null, alice, null, i.createInvite(seed, alice.id, {name: 'bob'}, {text: 'welcome to ssb!'}), new Date('2018-03-14T06:14:18.377Z'))
116
117 t.deepEqual({
118 reveal: {name: 'bob'},
119 private: {text: 'welcome to ssb!'}
120 }, i.verifyInvitePrivate(invite1, seed))
121
122
123 var accept_content = i.createAccept(invite1, seed, bob.id)
124 var accept = v.create(null, bob, null, accept_content, new Date('2018-03-14T06:32:18.377Z'))
125
126 var seed2 = hash('seed2')
127 var invite2 = v.create(null, alice, null, i.createInvite(seed2, alice.id, {name: 'bob'}, {text: 'welcome to ssb!'}), new Date('2018-03-14T06:14:18.377Z'))
128
129 //just test we do not verify the incorrect invite
130 throws(t, function () {
131 i.verifyAccept(accept, invite2)
132 }, 'user-invites:accept-wrong-invite')
133
134 t.end()
135
136})
137
138tape('wrong invite', function (t) {
139 var seed = hash('seed1')
140
141 var invite = v.create(null, alice, null, i.createInvite(seed, alice.id), new Date('2018-03-14T06:14:18.377Z'))
142 var seed2 = hash('seed2')
143 var accept_content = ssbKeys.signObj(ssbKeys.generate(null, seed2), invite_key, {
144 type: 'user-invite/accept',
145 receipt: '%'+ssbKeys.hash(JSON.stringify(invite, null, 2)),
146 id: bob.id,
147 })
148 var accept2 = v.create(null, bob, null, accept_content, new Date('2018-03-14T06:32:18.377Z'))
149
150
151 //just test we do not verify the incorrect invite
152 throws(t, function () {
153 i.verifyAccept(accept2, invite)
154 }, 'user-invites:accept-invite-signature-failed')
155
156 t.end()
157})
158
159
160tape('wrong invite', function (t) {
161 var seed = hash('seed1')
162
163 var invite = v.create(null, alice, null, i.createInvite(seed, alice.id, 'REVEAL'), new Date('2018-03-14T06:14:18.377Z'))
164 var accept_content = ssbKeys.signObj(ssbKeys.generate(null, seed), invite_key, {
165 type: 'user-invite/accept',
166 receipt: '%'+ssbKeys.hash(JSON.stringify(invite, null, 2)),
167 id: bob.id,
168 //key is missing!
169 })
170 var accept2 = v.create(null, bob, null, accept_content, new Date('2018-03-14T06:32:18.377Z'))
171
172
173 //just test we do not verify the incorrect invite
174 throws(t, function () {
175 i.verifyAccept(accept2, invite)
176 }, 'user-invites:accept-must-reveal-key')
177
178 t.end()
179})
180
181
182

Built with git-ssb-web