-
Notifications
You must be signed in to change notification settings - Fork 8
/
error.js
377 lines (313 loc) · 10.5 KB
/
error.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
var tape = require('tape')
var ssbKeys = require('ssb-keys')
var crypto = require('crypto')
var path = require('path')
var fs = require('fs')
function hash (seed) {
return crypto.createHash('sha256').update(seed).digest()
}
function id (msg) {
return '%'+crypto.createHash('sha256').update(JSON.stringify(msg, null, 2), 'binary').digest('base64')+'.sha256'
}
var keys = ssbKeys.generate('ed25519', hash('validation-test-seed1'))
var keys2 = ssbKeys.generate('ed25519', hash('validation-test-seed2'))
//generate randomish but deterministic test data.
//this is not intended for security use.
//use a proper key stream (etc) instead.
function pseudorandom (seed, length) {
var a = []
for(var l = 0; l < length; l += 32)
a.push(hash(''+seed+l))
return Buffer.concat(a).slice(0, length)
}
var v = require('..')
var data = []
function test (hmac_key) {
var state = v.initial()
tape('simple', function (t) {
for(var i = 0; i < 10; i++) {
var msg = v.create(state.feeds[keys.id], keys, hmac_key, {type: 'test', i: i}, +new Date('2017-04-11 8:08 UTC')+i)
//append sets the state for this author,
//as well as appends the message to the queue.
state = v.append(state, hmac_key, msg)
}
console.log(state)
t.end()
})
tape('rerun', function (t) {
for(var i = 0; i < state.queue.length; i++) {
try {
console.log(state.queue[i])
state = v.append(state, hmac_key, state.queue[i])
t.fail('should have thrown')
} catch (err) {
t.equal(err.fatal, undefined)
}
}
t.end()
})
tape('invalid - fork', function (t) {
var last = state.queue[9]
var m = v.create({
id: last.previous,
sequence: 10,
timestamp: last.timestamp,
queue: []
}, keys, hmac_key, {type: 'invalid'}, last.timestamp+1)
console.log(m)
try {
state = v.append(state, hmac_key, m)
t.fail('should have thrown')
} catch (err) {
console.log(err)
t.equal(err.fatal, true)
t.end()
}
})
// monotonic timestamps no longer a requirement
tape('rewind ok', function (t) {
var last = state.queue[9]
var m = v.create({
id: state.feeds[keys.id].id,
sequence: 10,
timestamp: last.timestamp-2, queue: []
}, keys, hmac_key, {type: 'invalid'}, last.timestamp-1)
console.log(m, last)
state = v.append(state, hmac_key, m)
t.end()
})
tape('create with invalid date', function (t) {
t.throws(function () {
var m = v.create(null, keys, hmac_key, {type: 'invalid'}, new Date('foo'))
})
t.end()
})
tape('invalid because of empty content', function (t) {
t.throws(function () {
var m = v.create(null, keys, hmac_key, null, Date.now())
})
t.end()
})
var ctxt =
pseudorandom('test', 1024).toString('base64')+'.box'
//create a purposefully invalid message (for testing)
// function create_invalid (state, keys, hmac_key, content, timestamp) {
// invalid.push(ssbKeys.signObj(keys, hmac_key, {
// previous: state ? state.id : null,
// sequence: state ? state.sequence + 1 : 1,
// author: keys.id,
// timestamp: +timestamp,
// hash: 'sha256',
// content: content,
// }))
// return v.create(state, keys, hmac_key, null, timestamp)
// }
function test_invalid(t, state, keys, hmac_key, content, timestamp) {
var msg = ssbKeys.signObj(keys, hmac_key, {
previous: state ? state.id : null,
sequence: state ? state.sequence + 1 : 1,
author: keys.id,
timestamp: +timestamp,
hash: 'sha256',
content: content,
})
if(!Buffer.isBuffer(content))
data.push({state: state, msg: msg, cap: hmac_key, valid: false, id: id(msg)})
t.throws(function () {
console.log(state, v.create(state, keys, hmac_key, content, timestamp))
})
t.throws(function () {
var _state = {feeds: {}}
_state.feeds[keys.id] = state
v.append(_state, hmac_key, msg)
})
}
//create invalid messages with invalid orders
function test_invalid_msg(t, state, keys, hmac_key, _msg) {
var msg = ssbKeys.signObj(keys, hmac_key, _msg)
if(!state)
state = {
queue: [],
feeds: {},
validated: 0
}
data.push({state: state, msg: msg, cap: hmac_key, valid: false, id: id(msg)})
t.throws(function () {
console.log(v.append(state, hmac_key, msg))
})
t.throws(function () {
var _state = {feeds: {}}
_state.feeds[keys.id] = state
v.append(_state, hmac_key, msg)
})
}
function test_valid(t, state, keys, hmac_key, content, timestamp) {
var msg
msg = v.create(state, keys, hmac_key, content, timestamp)
data.push({state: state, msg: msg, cap: hmac_key, valid: true, id: id(msg)})
var _state = {queue: [], feeds: {}}
_state.feeds[keys.id] = state
v.append(_state, hmac_key, msg)
t.deepEqual(msg.content, content)
t.equal(msg.timestamp, timestamp)
}
function test_valid_msg(t, state, keys, hmac_key, _msg) {
var msg = ssbKeys.signObj(keys, hmac_key, _msg)
data.push({state: state, msg: msg, cap: hmac_key, valid: true, id: id(msg)})
var _state = {queue: [], feeds: {}}
_state.feeds[keys.id] = state
v.append(_state, hmac_key, msg)
}
var invalid = []
tape('various invalid first messages', function (t) {
var date = +new Date('2017-04-11 9:09 UTC')
test_invalid(t, null, keys, hmac_key, null, date)
test_invalid(t, null, keys, hmac_key, date)
test_invalid(t, null, keys, hmac_key, false, date)
test_invalid(t, null, keys, hmac_key, 0, date)
test_invalid(t, null, keys, hmac_key, 100, date)
test_invalid(t, null, keys, hmac_key, [], date)
test_invalid(t, null, keys, hmac_key, new Buffer('hello'), date)
test_invalid(t, null, keys, hmac_key, new Date(date), date)
test_invalid(t, null, keys, hmac_key, {}, date)
test_invalid(t, null, keys, hmac_key, {tyfe:'not-okay' }, date)
test_invalid(t,null, keys, hmac_key, {tyfe:'not-okay' }, date)
test_invalid(t, null, keys, hmac_key, {
type: //too long!
pseudorandom('test', 100).toString('base64')
}, date)
//type too long
test_invalid(t, null, keys, hmac_key, {type:keys.id }, date)
// type too short
test_invalid(t, null, keys, hmac_key, {type:'T' }, date)
// type too short
test_invalid(t, null, keys, hmac_key, {type:'TT' }, date)
// content too long
test_invalid(t,
null, keys, hmac_key,
pseudorandom('test', 8*1024).toString('base64')+'.box',
date
)
t.end()
})
tape('extended invalid first messages', function (t) {
var date = +new Date('2017-04-11 9:09 UTC')
test_invalid_msg(t, null, keys, hmac_key, {
previous: null,
author: keys.id,
sequence: 1,
timestamp: +date,
hash: 'oanteuhnoatehuneotuh', //unsupported hash
content: {type:'invalid'}
})
test_invalid_msg(t, null, keys, hmac_key, {
previous: null,
author: keys.id,
sequence: 1,
timestamp: +date,
//missing hash
content: {type:'invalid'}
})
//invalid orders
test_invalid_msg(t, null, keys, hmac_key, {
content: {type:'invalid'},
hash: 'sha256',
author: keys.id,
timestamp: +date,
sequence: 1,
previous: null,
})
test_invalid_msg(t, null, keys, hmac_key, {
previous: null,
author: keys.id,
sequence: 1,
timestamp: +date,
content: {type:'invalid'},
hash: 'sha256',
})
t.end()
})
tape('disallow extra fields', function (t) {
var msg = ssbKeys.signObj(keys, hmac_key, {
previous: null,
author: keys.id,
sequence: 1,
timestamp: +new Date('2017-04-11 9:09 UTC'),
hash: 'sha256',
content: {type: 'invalid'},
extra: 'INVALID'
})
var signature = msg.signature
delete msg.signature
delete msg.extra
msg.signature = signature
msg.extra = 'INVALID'
var state = {
queue: [],
feeds: {},
validated: 0
}
data.push({state: state, msg: msg, cap: hmac_key, valid: false, id: id(msg)})
t.throws(function () {
console.log(v.append(state, hmac_key, msg))
})
t.throws(function () {
var _state = {feeds: {}}
_state.feeds[keys.id] = state
v.append(_state, hmac_key, msg)
})
t.end()
})
tape('valid messages', function (t) {
var msg
//type must be 3 chars
test_valid(t, null, keys, hmac_key, {type:'TTT' }, +new Date('2017-04-11 9:09 UTC'))
//author and sequence fields may come in either order!
//all other fields must be in exact order.
test_valid_msg(t, null, keys, hmac_key, {
previous: null,
author: keys.id, sequence: 1,
timestamp: +new Date('2017-04-11 9:09 UTC'),
hash: 'sha256',
content: { type: 'valid' }
})
test_valid_msg(t, null, keys, hmac_key, {
previous: null,
sequence: 1, author: keys.id,
timestamp: +new Date('2017-04-11 9:09 UTC'),
hash: 'sha256',
content: { type: 'valid' }
})
//type can be msg id
var msg_id = '%'+hash('test_msg_id').toString('base64')+'.sha256'
test_valid(t, null, keys2, hmac_key, {type: msg_id}, +new Date('2017-04-11 9:09 UTC'))
//content can be encrypted.
test_valid(t, null, keys2, hmac_key, ctxt, +new Date('2017-04-11 9:09 UTC'))
//content encrypted with future private box version
test_valid(t, null, keys2, hmac_key, ctxt+'2', +new Date('2017-04-11 9:09 UTC'))
test_valid(t, null, keys, hmac_key, {type: 'okay'}, +new Date('2019-01-01 0:00 UTC'))
//for backwards compatibilty reasons, we only count the
//javascript string length of a message, so it may actually
//be encoded as longer that 8k, if it uses unicode.
//so a message with 7000 euro signs is valid, even though
//it is technically longer than we intended.
//at some point, we'll introduce a binary encoding for
//messages and we'll fix this then.
var text = ''
for(var i = 0; i < 7000; i++)
text += '\u20ac'
t.ok(text.length < 8124)
console.log(text.length)
console.log(new Buffer(JSON.stringify({text: text}), 'utf8').length)
t.ok(new Buffer(JSON.stringify({text: text}), 'utf8').length > 8000)
test_valid(t, null, keys, hmac_key, {type: 'euros', text: text}, +new Date('2019-01-01 0:00 UTC'))
t.end()
})
}
test()
test(hash('hmac_key').toString('base64'))
test(hash('hmac_key2').toString('base64'))
tape('write data', function (t) {
fs.writeFileSync(path.join(__dirname, 'data', 'test_messages.json'), JSON.stringify(data, null, 2))
t.end()
})