mirror of https://codeberg.org/pzp/pzp-db.git
refactor account msg validation
This commit is contained in:
parent
f1a13eee80
commit
3a9df124f6
43
lib/index.js
43
lib/index.js
|
@ -25,6 +25,7 @@ const { decrypt } = require('./encryption')
|
||||||
* @typedef {import('./msg-v3').AccountData} AccountData
|
* @typedef {import('./msg-v3').AccountData} AccountData
|
||||||
* @typedef {import('./msg-v3').AccountPower} AccountPower
|
* @typedef {import('./msg-v3').AccountPower} AccountPower
|
||||||
* @typedef {import('./encryption').EncryptionFormat} EncryptionFormat
|
* @typedef {import('./encryption').EncryptionFormat} EncryptionFormat
|
||||||
|
* @typedef {import('./msg-v3/tangle')} Tangle
|
||||||
*
|
*
|
||||||
* @typedef {Buffer | Uint8Array} B4A
|
* @typedef {Buffer | Uint8Array} B4A
|
||||||
*/
|
*/
|
||||||
|
@ -259,7 +260,7 @@ function initDB(peer, config) {
|
||||||
* Find which pubkeys are authorized to sign this msg given the account.
|
* Find which pubkeys are authorized to sign this msg given the account.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {DBTangle | null} accountTangle
|
* @param {Tangle | null} accountTangle
|
||||||
* @returns {Set<string>}
|
* @returns {Set<string>}
|
||||||
*/
|
*/
|
||||||
function getPubkeysInAccount(accountTangle) {
|
function getPubkeysInAccount(accountTangle) {
|
||||||
|
@ -310,7 +311,7 @@ function initDB(peer, config) {
|
||||||
|
|
||||||
// Identify the account and its pubkeys:
|
// Identify the account and its pubkeys:
|
||||||
const accountID = getAccountID(rec)
|
const accountID = getAccountID(rec)
|
||||||
let accountTangle = /** @type {DBTangle | null} */ (null)
|
let accountTangle = /** @type {Tangle | null} */ (null)
|
||||||
if (accountID) {
|
if (accountID) {
|
||||||
accountTangle = new DBTangle(accountID, records())
|
accountTangle = new DBTangle(accountID, records())
|
||||||
if (msgID === accountID) {
|
if (msgID === accountID) {
|
||||||
|
@ -329,11 +330,27 @@ function initDB(peer, config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Account tangle related validations
|
// Account tangle related validations
|
||||||
if (
|
if (msg.metadata.account === ACCOUNT_SELF) {
|
||||||
msg.metadata.account === ACCOUNT_SELF &&
|
if ((err = validateAccountMsg(msg, accountTangle))) {
|
||||||
accountTangle &&
|
// prettier-ignore
|
||||||
!MsgV3.isRoot(msg)
|
return cb(new Error('add() failed msg account validation', { cause: err }))
|
||||||
) {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logAppend(msgID, msg, (err, rec) => {
|
||||||
|
if (err) return cb(new Error('add() failed in the log', { cause: err }))
|
||||||
|
onRecordAdded.set(rec)
|
||||||
|
cb(null, rec)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Msg} msg
|
||||||
|
* @param {Tangle | null} accountTangle
|
||||||
|
* @returns {string | undefined}
|
||||||
|
*/
|
||||||
|
function validateAccountMsg(msg, accountTangle) {
|
||||||
|
if (accountTangle && !MsgV3.isRoot(msg)) {
|
||||||
/** @type {AccountData} */
|
/** @type {AccountData} */
|
||||||
const data = msg.data
|
const data = msg.data
|
||||||
if (data.action === 'add') {
|
if (data.action === 'add') {
|
||||||
|
@ -342,17 +359,11 @@ function initDB(peer, config) {
|
||||||
const powers = getAccountPowers(accountTangle, keypair)
|
const powers = getAccountPowers(accountTangle, keypair)
|
||||||
if (!powers.has('add')) {
|
if (!powers.has('add')) {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
return cb(new Error('add() failed because this msg.pubkey does not have "add" power'))
|
return `invalid message: pubkey "${msg.pubkey}" does not have "add" power`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO validate 'del'
|
// TODO validate 'del'
|
||||||
}
|
}
|
||||||
|
|
||||||
logAppend(msgID, msg, (err, rec) => {
|
|
||||||
if (err) return cb(new Error('add() failed in the log', { cause: err }))
|
|
||||||
onRecordAdded.set(rec)
|
|
||||||
cb(null, rec)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -522,7 +533,7 @@ function initDB(peer, config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {DBTangle} accountTangle
|
* @param {Tangle} accountTangle
|
||||||
* @param {KeypairPublicSlice} keypair
|
* @param {KeypairPublicSlice} keypair
|
||||||
* @returns {Set<AccountPower>}
|
* @returns {Set<AccountPower>}
|
||||||
*/
|
*/
|
||||||
|
@ -848,7 +859,7 @@ function initDB(peer, config) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} tangleID
|
* @param {string} tangleID
|
||||||
* @returns {DBTangle}
|
* @returns {Tangle}
|
||||||
*/
|
*/
|
||||||
function getTangle(tangleID) {
|
function getTangle(tangleID) {
|
||||||
return new DBTangle(tangleID, records())
|
return new DBTangle(tangleID, records())
|
||||||
|
|
|
@ -144,7 +144,7 @@ test('keypair with no "add" powers cannot account.add()', async (t) => {
|
||||||
// Test replicator-side power validation
|
// Test replicator-side power validation
|
||||||
assert.rejects(
|
assert.rejects(
|
||||||
p(peer1again.db.add)(msg3, id),
|
p(peer1again.db.add)(msg3, id),
|
||||||
/msg\.pubkey does not have "add" power/
|
/add\(\) failed msg account validation/
|
||||||
)
|
)
|
||||||
|
|
||||||
await p(peer1again.close)()
|
await p(peer1again.close)()
|
||||||
|
|
Loading…
Reference in New Issue