fix: throw error if NIP-19 has invalid id

This commit is contained in:
Shusui MOYATANI
2024-02-21 01:10:12 +09:00
parent e96e8f4897
commit a43bae7262
2 changed files with 13 additions and 0 deletions

View File

@@ -179,6 +179,10 @@ describe('parseTextNote', () => {
'npub1a',
'npub133vj8ycevdle0cq8mtgddq0xtn34kxkwxvak983dx0u5vhqnycyqj6tcz1',
'npub133vj8ycevdle0cq8mtgddq0xtn34kxkwxvak983dx0u5vhqnycyqj6tczb',
// bech32 problem: checksum cannot detect arbitrary number of 'q' before 'p'
'npub1harpehyplx66gzpsskwzejtwgm45zenlzr4md58lxrvsvllupe8qunwsqqqqqqqqp',
// bech32 problem: checksum cannot detect deletion of 'q' in 'qp'
'npub1harpehyplx66gzpsskwzejtwgm45zenlzr4md58lxrvsvllupe8qunwsp',
])('should parse text note which includes invalid npub string (%s)', (invalidPubkey) => {
const content = `this is pubkey\n${invalidPubkey}\nhello`;
const parsed = parseTextNote(content);

View File

@@ -151,6 +151,15 @@ const parseTextNote = (textNoteContent: string) => {
data: decoded,
isNIP19: match.groups.nip19 === 'nostr:',
};
if (
((decoded.type === 'npub' || decoded.type === 'note') && !isValidId(decoded.data)) ||
(decoded.type === 'nprofile' && !isValidId(decoded.data.pubkey)) ||
(decoded.type === 'nevent' && !isValidId(decoded.data.id)) ||
(decoded.type === 'naddr' && !isValidId(decoded.data.pubkey)) ||
(decoded.type === 'nsec' && decoded.data.length !== 32)
) {
throw new Error('Invalid ID');
}
result.push(bech32Entity);
} catch (e) {
console.warn(`ignored invalid bech32 entity: ${match[0]}`);