This repository has been archived by the owner on May 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
232 lines (204 loc) · 7.09 KB
/
validate.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
// SPDX-FileCopyrightText: 2021 Anders Rune Jensen
//
// SPDX-License-Identifier: LGPL-3.0-only
const bencode = require('bencode')
const bfe = require('ssb-bfe')
const ssbKeys = require('ssb-keys')
const SSBURI = require('ssb-uri2')
const ref = require('ssb-ref')
const isCanonicalBase64 = require('is-canonical-base64')
const CONTENT_SIG_PREFIX = Buffer.from('bendybutt', 'utf8')
/**
* Validate a single meta feed message.
*
* @param {Object} msg - a meta feed message in the form of a JSON object
* @param {Buffer | string | null} hmacKey - a valid HMAC key for signature
* verification
* @returns {Boolean} `true` in the case of successful validation, `false`
* otherwise
*/
function isValid(msg, hmacKey) {
if (msg.value.content && msg.value.contentSignature) {
const contentSection = [msg.value.content, msg.value.contentSignature]
const validationResult = validateSingle(contentSection, hmacKey)
return validationResult === undefined
} else {
return false
}
}
/**
* Validate a single meta feed message `contentSection`.
*
* @param {Array | string} contentSection - an array of `content` and
* `contentSignature` or an encrypted string
* @param {Buffer | string | null} hmacKey - a valid HMAC key for signature
* verification
* @returns {Error | undefined} an `Error` object or `undefined` in the case of
* successful validation
*/
function validateSingle(contentSection, hmacKey) {
if (contentSection === null || contentSection === undefined)
return new Error(
`invalid message: contentSection cannot be null or undefined`
)
// check if content is (maybe) encrypted
if (typeof contentSection === 'string')
return new Error(
'invalid message: cannot validate encrypted contentSection'
)
if (!(Array.isArray(contentSection) && contentSection.length === 2))
return new Error(
`invalid message: contentSection ${typeof contentSection} with length ${
contentSection.length
} is incorrect, expected a list of content and contentSignature`
)
const [content, contentSignature] = contentSection
if (
!(
content.type === 'metafeed/add/existing' ||
content.type === 'metafeed/add/derived' ||
content.type === 'metafeed/update' ||
content.type === 'metafeed/tombstone'
)
)
return new Error(
`invalid message: content type ${content.type} is incorrect`
)
const subfeedBFE = bfe.encode(content.subfeed)
const subfeedType = subfeedBFE.slice(0, 1).toString('hex')
if (subfeedType !== '00')
return new Error(
`invalid message: content subfeed type "0x${subfeedType}" is incorrect, expected 0x00`
)
const metafeedBFE = bfe.encode(content.metafeed)
const metafeedType = metafeedBFE.slice(0, 2).toString('hex')
if (metafeedType !== '0003')
return new Error(
`invalid message: content metafeed type "0x${metafeedType}" is incorrect, expected 0x0003`
)
if (content.type === 'metafeed/add/derived') {
if (content.nonce.length !== 32) {
const nonceString = content.nonce.toString('hex')
return new Error(
`invalid message: content nonce "${nonceString}" is ${content.nonce.length} bytes, expected 32`
)
}
}
const signatureErr = validateSignature(
content.subfeed,
content,
contentSignature,
hmacKey
)
if (signatureErr) return signatureErr
}
/**
* Verify that the contentSignature correctly signs the content.
*
* @param {string} subfeedKey - `subfeed` key for the message
* @param {Buffer} content - Dictionary of meta feed metadata
* @param {string} contentSignature - Base64-encoded signature for the given
* `content`
* @param {Buffer | string | null} hmacKey - HMAC key that was used to sign the
* payload
* @returns {Error | undefined} Either an Error containing a message or an
* `undefined` value for successful verification
*/
function validateSignature(subfeedKey, content, contentSignature, hmacKey) {
const hmacKeyErr = validateHmacKey(hmacKey)
if (hmacKeyErr) return hmacKeyErr
const isSignatureRx = isCanonicalBase64('', '\\.sig.\\w+')
if (!isSignatureRx.test(contentSignature))
return new Error(
`invalid message: contentSignature "${contentSignature}", expected a base64 string`
)
// if the subfeedKey is a supported uri, convert it to sigil for verification
if (!ref.isFeed(subfeedKey)) {
if (
!SSBURI.isFeedSSBURI(subfeedKey) &&
!SSBURI.isBendyButtV1FeedSSBURI(subfeedKey) &&
!SSBURI.isGabbyGroveV1FeedSSBURI(subfeedKey)
) {
return new Error(
`invalid message: subfeed key "${subfeedKey}", expected a canonical uri format or classic ssb sigil`
)
} else {
subfeedKey = SSBURI.decompose(subfeedKey).data
}
}
const contentBFE = bfe.encode(content)
if (
!ssbKeys.verify(
{ public: subfeedKey, curve: 'ed25519' },
contentSignature,
hmacKey,
Buffer.concat([CONTENT_SIG_PREFIX, bencode.encode(contentBFE)])
)
)
return new Error(
`invalid message: contentSignature must correctly sign the content using the subfeed key; ${subfeedKey}`
)
}
/**
* Validate an HMAC key.
*
* @param {Buffer | string | null | undefined} hmacKey
* @returns {Object | undefined} Either an Error containing a message or
* `undefined` for successful validation
*/
function validateHmacKey(hmacKey) {
if (hmacKey === undefined || hmacKey === null) return
const bytes = Buffer.isBuffer(hmacKey)
? hmacKey
: Buffer.from(hmacKey, 'base64')
if (typeof hmacKey === 'string') {
if (bytes.toString('base64') !== hmacKey)
return new Error(
`invalid hmac key: "${hmacKey}", expected string to be base64 encoded`
)
}
if (bytes.length !== 32)
return new Error(
`invalid hmac key: "${hmacKey}" with length ${hmacKey.length}, expected 32 bytes`
)
}
/**
* Validates a main-feed message for metafeed/announce.
*
* @param {Object} msg classic msg for a metafeed/announce
* @returns {Error | undefined} Either an Error or `undefined` for successful
* validation
*/
function validateMetafeedAnnounce(msg) {
if (!ref.isFeedId(msg.value.author)) {
return new Error(
`metafeed/announce ${msg.key} is invalid ` +
`because author is not a classic feed: ${msg.value.author}`
)
}
const { content } = msg.value
const metaFeedId = content.metafeed
if (!SSBURI.isBendyButtV1FeedSSBURI(metaFeedId)) {
return new Error(
`metafeed/announce ${msg.key} is invalid ` +
`because content.metafeed is not a bendy butt feed: ${metaFeedId}`
)
}
if (content.subfeed !== msg.value.author) {
return new Error(
`metafeed/announce ${msg.key} is invalid ` +
`because content.subfeed is not msg.value.author: ${content.subfeed}`
)
}
const { data } = SSBURI.decompose(metaFeedId)
const ed25519Public = `${data}.ed25519`
if (!ssbKeys.verifyObj(ed25519Public, content)) {
return new Error(
`metafeed/announce ${msg.key} is invalid ` +
`because content is not signed by the meta feed: ${content}`
)
}
}
exports.isValid = isValid
exports.validateSingle = validateSingle
exports.validateMetafeedAnnounce = validateMetafeedAnnounce