Проверить вложения и блоки в каналах

This commit is contained in:
AidarKC
2026-11-21 00:24:25 +03:00
parent 859dbee1f1
commit 792f368175
27 changed files with 786 additions and 137 deletions
@@ -0,0 +1,41 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { buildAttachmentBlock, parseMessageAttachments } from '../js/services/attachment-format.js';
const root = '4vJ9JU1bJJE96FWSJKvHsmmFADCg4gpZQff4P3bkLKi';
const source = (name) => ({ name, size: 1837243, pr: root, mm: 'video/mp4' });
test('v2: UTF-8 имена без лишних %-кодов', () => {
const marker = buildAttachmentBlock(source('Водопад.mp4'));
assert.match(marker, /^<S:att;v=2;nm=Водопад.mp4;sz=1837243;/u);
assert.equal(parseMessageAttachments(`${marker}\nцук`).attachments[0].name, 'Водопад.mp4');
assert.equal(parseMessageAttachments(`${marker}\nцук`).text, 'цук');
});
test('v2: только служебные символы экранируются; круговой обмен', () => {
for (const name of ['Лето;фильм=1%.mp4', 'Привет<мир>.mp4', '中文;عربي=тест%.mp4']) {
const marker = buildAttachmentBlock(source(name));
assert.equal(parseMessageAttachments(marker).attachments[0].name, name);
assert.equal(marker.includes('nm='+encodeURIComponent(name)), false);
assert.doesNotMatch(marker, /nm=[^;]*;фильм/u);
}
});
test('v1: прежние URI-имена остаются читаемыми', () => {
const name = 'Водопад.mp4';
const marker = `<S:att;v=1;nm=${encodeURIComponent(name)};sz=10;pr=${root};mm=video/mp4>`;
assert.equal(parseMessageAttachments(marker).attachments[0].name, name);
});
test('v2: дубликаты, неизвестные поля и плохие escape не интерпретируются как вложение', () => {
for (const body of [
`v=2;nm=a;sz=10;sz=500;pr=${root}`,
`v=2;nm=foo;bad=value;sz=10;pr=${root}`,
`v=2;nm=abc%zz;sz=10;pr=${root}`,
`v=2;nm=abc%3;sz=10;pr=${root}`,
`v=2;nm=abc%0Aevil;sz=10;pr=${root}`,
`v=2;nm=abc;sz=-1;pr=${root}`,
`v=2;nm=abc;sz=01;pr=${root}`,
`v=2;nm=abc<b;sz=10;pr=${root}`,
`v=2;nm=abc=d;sz=10;pr=${root}`,
`v=2;nm=abc;sz=10;pr=${root};ar=wrong-id`,
]) {
assert.equal(parseMessageAttachments(`<S:att;${body}>`).attachments.length, 0, body);
}
});
+38
View File
@@ -0,0 +1,38 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { verifyAns104DataItem } from '../js/services/ans104-data-item.js';
import { arweaveBlockUrl, readSignedBlock } from '../js/services/blockchain-proof-service.js';
import { bytesToBase64Url } from '../js/services/crypto-utils.js';
import { webcrypto } from 'node:crypto';
if (!globalThis.crypto?.subtle) globalThis.crypto = webcrypto;
// Изолированный эталон ANS104 (файл взят из текущего тестового блока проекта).
const signedPath = new URL('./fixtures/block11.ans104', import.meta.url);
const expectedHash = '46db963eab3e9a9aeddba0b67bb2c44daf3be3472706c25a8035ad4ae914e99f';
const expectedId = 'T0dcFEjRymardqVuTEU5HdkPjkKm8ALU7nyf6LOzCXE';
test('подпись Ed25519 полного DataItem и SHA-256 полезного блока', async () => {
const bytes = new Uint8Array(await readFile(signedPath));
const proof = await verifyAns104DataItem(bytes);
assert.equal(bytes.length, 412);
assert.equal(proof.data.length, 274);
assert.equal(proof.signatureValid, true);
assert.equal(bytesToBase64Url(proof.dataItemIdBytes), expectedId);
assert.equal(Buffer.from(proof.frameHashBytes).toString('hex'), expectedHash);
});
test('подмена payload не проходит проверку подписи и SHA-256', async () => {
const bytes = new Uint8Array(await readFile(signedPath));
bytes[bytes.length - 1] ^= 0x01;
const proof = await verifyAns104DataItem(bytes);
assert.equal(proof.signatureValid, false);
assert.notEqual(Buffer.from(proof.frameHashBytes).toString('hex'), expectedHash);
});
test('по API нельзя скачать DataItem с подменённым хешем или ID', async () => {
const bytes = new Uint8Array(await readFile(signedPath));
const authService = { ws: { request: async () => ({ status: 200, payload: {
blockHash: expectedHash, blockBytesB64: Buffer.from(bytes).toString('base64'),
} }) } };
assert.equal((await readSignedBlock({authService, blockchainName:'wwwww@shine-001',blockNumber:11,blockHash:expectedHash,dataItemId:expectedId})).bytes.length, 412);
await assert.rejects(readSignedBlock({authService, blockchainName:'wwwww@shine-001',blockNumber:11,blockHash:expectedHash,dataItemId:'A'.repeat(43)}), /Arweave/);
assert.equal(arweaveBlockUrl('../evil'), '');
});
Binary file not shown.