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