SHA256
152 lines
7.2 KiB
JavaScript
152 lines
7.2 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { createHash, webcrypto } from 'node:crypto';
|
|
import { DM_FILE_CHUNK_BYTES, encryptAndUploadDmFile, decryptDmFileToBlob } from '../../shine-UI/js/services/dm-file-service.js';
|
|
import { buildDmFileTechBlock, parseDmTechBlocks } from '../../shine-UI/js/services/dm-tech-blocks.js';
|
|
import {
|
|
bytesToBase58, bytesToBase64Url, encryptBytesAesGcm, exportPkcs8B64,
|
|
randomBytes, sha256Bytes,
|
|
} from '../../shine-UI/js/services/crypto-utils.js';
|
|
|
|
// Offline R2/Java simulation: fake IndexedDB provides the session key.
|
|
globalThis.crypto ??= webcrypto;
|
|
globalThis.window = { location: { href: 'https://shineup.me/' } };
|
|
const keyPair = await crypto.subtle.generateKey({ name: 'Ed25519' }, true, ['sign', 'verify']);
|
|
const session = { sessionId: 'test-session', sessionPrivPkcs8: await exportPkcs8B64(keyPair.privateKey) };
|
|
globalThis.indexedDB = {
|
|
open() {
|
|
const request = {};
|
|
const db = {
|
|
objectStoreNames: { contains: () => true },
|
|
transaction() {
|
|
return { objectStore() { return { get() {
|
|
const result = { result: session };
|
|
queueMicrotask(() => result.onsuccess?.());
|
|
return result;
|
|
} }; } };
|
|
},
|
|
close() {},
|
|
};
|
|
request.result = db;
|
|
queueMicrotask(() => request.onsuccess?.());
|
|
return request;
|
|
},
|
|
};
|
|
const stored = new Map();
|
|
let signedRequests = 0;
|
|
let putRequests = 0;
|
|
const bytesFromBody = async body => new Uint8Array(await new Response(body).arrayBuffer());
|
|
globalThis.fetch = async (url, opts = {}) => {
|
|
const method = opts.method || 'GET';
|
|
const target = String(url);
|
|
if (target.endsWith('/api/dm-files/prepare')) {
|
|
assert.equal(method, 'POST');
|
|
const { fileId, size } = JSON.parse(opts.body);
|
|
assert.ok(size >= 16 && size <= DM_FILE_CHUNK_BYTES + 16);
|
|
const sigBytes = Buffer.from(opts.headers['X-Shine-Signature'], 'base64');
|
|
const timeMs = opts.headers['X-Shine-Time-Ms'];
|
|
const preimage = `DM_R2_PREPARE_V1:test-session:${fileId}:${size}:${timeMs}`;
|
|
assert.equal(await crypto.subtle.verify('Ed25519', keyPair.publicKey, sigBytes, new TextEncoder().encode(preimage)), true);
|
|
signedRequests++;
|
|
return Response.json({
|
|
fileUrl: `https://dm-files.shineup.me/${fileId}`,
|
|
...(stored.has(fileId) ? { exists: true } : {
|
|
exists: false, uploadUrl: `https://r2.invalid/${fileId}`,
|
|
requiredHeaders: { 'content-type': 'application/octet-stream', 'if-none-match': '*' },
|
|
}),
|
|
});
|
|
}
|
|
const id = target.split('/').at(-1);
|
|
if (target.startsWith('https://r2.invalid/')) {
|
|
assert.equal(method, 'PUT');
|
|
assert.equal(opts.headers['if-none-match'], '*');
|
|
const bytes = await bytesFromBody(opts.body);
|
|
if (stored.has(id)) return new Response('', { status: 412 });
|
|
stored.set(id, bytes);
|
|
putRequests++;
|
|
return new Response('', { status: 200 });
|
|
}
|
|
if (target.startsWith('https://dm-files.shineup.me/')) {
|
|
const bytes = stored.get(id);
|
|
if (!bytes) return new Response('', { status: 404 });
|
|
return new Response(method === 'HEAD' ? null : bytes, { status: 200, headers: {
|
|
'Content-Length': String(bytes.byteLength), 'Content-Type': 'application/octet-stream',
|
|
} });
|
|
}
|
|
throw new Error(`Unexpected fetch: ${method} ${target}`);
|
|
};
|
|
|
|
const MB = 1024 * 1024;
|
|
assert.equal(DM_FILE_CHUNK_BYTES, 64 * MB);
|
|
const data = new Uint8Array(DM_FILE_CHUNK_BYTES + 137);
|
|
for (let i = 0; i < data.length; i++) data[i] = i % 251;
|
|
const file = new File([data], 'movie.bin', { type: 'application/octet-stream' });
|
|
const upload = await encryptAndUploadDmFile({
|
|
file, login: 'alice', sessionId: 'test-session', wsUrl: 'wss://shineup.me/ws',
|
|
});
|
|
assert.equal(upload.version, 3);
|
|
assert.equal(upload.chunkCount, 2);
|
|
assert.equal(upload.chunkSize, 64 * MB);
|
|
assert.equal(stored.size, 3); // ciphertext block 0, block 1, encrypted manifest
|
|
assert.equal(putRequests, 3);
|
|
assert.equal(signedRequests, 3);
|
|
assert.match(upload.url, /^https:\/\/dm-files\.shineup\.me\/[1-9A-HJ-NP-Za-km-z]+$/);
|
|
const dataObjects = [...stored.entries()].filter(([id]) => id !== upload.id);
|
|
assert.deepEqual(dataObjects.map(([, bytes]) => bytes.byteLength), [64 * MB + 16, 137 + 16]);
|
|
const raw = buildDmFileTechBlock(upload);
|
|
assert.match(raw, /<S:file;v=3;/);
|
|
assert.doesNotMatch(raw, /;th=|;pr=/);
|
|
const parsed = parseDmTechBlocks(`${raw}test`).fileAttachment;
|
|
assert.equal(parsed.version, 3);
|
|
assert.equal(parsed.torrentV2PiecesRootB64Url, undefined);
|
|
// В реальном приложении URL вычисляется по senderLogin через federated resolver.
|
|
// В offline-тесте используем допустимую историческую форму v3 с URL.
|
|
parsed.url = upload.url;
|
|
const recovered = new Uint8Array(await (await decryptDmFileToBlob(parsed)).arrayBuffer());
|
|
assert.equal(createHash('sha256').update(recovered).digest('hex'), createHash('sha256').update(data).digest('hex'));
|
|
const damaged = dataObjects[0][1].slice();
|
|
damaged[0] ^= 1;
|
|
stored.set(dataObjects[0][0], damaged);
|
|
await assert.rejects(() => decryptDmFileToBlob(parsed), /SHA-256/);
|
|
stored.set(dataObjects[0][0], dataObjects[0][1]);
|
|
|
|
// Legacy DM v3 (8 MiB) must remain readable after changing the upload size.
|
|
function makeIv(prefix, token) {
|
|
const iv = new Uint8Array(12);
|
|
iv.set(prefix);
|
|
new DataView(iv.buffer).setBigUint64(4, BigInt(token), false);
|
|
return iv;
|
|
}
|
|
function makeAad(prefix, domain, index) {
|
|
return new TextEncoder().encode(`SHINE-DM-FILE-V2:${bytesToBase64Url(prefix)}:${domain}:${index}`);
|
|
}
|
|
async function saveCipher(plain, key, prefix, token, domain, index) {
|
|
const ciphertext = await encryptBytesAesGcm(plain, key, makeIv(prefix, token), makeAad(prefix, domain, index));
|
|
const id = bytesToBase58(await sha256Bytes(ciphertext));
|
|
stored.set(id, ciphertext);
|
|
return { id, es: ciphertext.length, ps: plain.length };
|
|
}
|
|
const oldSize = 8 * MB;
|
|
const legacyData = new Uint8Array(oldSize + 137);
|
|
for (let i = 0; i < legacyData.length; i++) legacyData[i] = i % 193;
|
|
const legacyKey = randomBytes(32);
|
|
const legacyPrefix = randomBytes(4);
|
|
const chunks = [];
|
|
for (let i = 0; i < 2; i++) {
|
|
const start = i * oldSize;
|
|
chunks.push({ i, ...await saveCipher(legacyData.slice(start, start + oldSize), legacyKey, legacyPrefix, i, 'chunk', i) });
|
|
}
|
|
const manifest = {
|
|
v: 3, scheme: 'SHINE-DM-R2-AES-256-GCM', chunkSize: oldSize,
|
|
fileSize: legacyData.length, chunkCount: 2, chunks,
|
|
};
|
|
const root = await saveCipher(new TextEncoder().encode(JSON.stringify(manifest)), legacyKey, legacyPrefix, 0xffffffffffffffffn, 'root', 0);
|
|
const legacyAttachment = {
|
|
version: 3, id: root.id, url: `https://dm-files.shineup.me/${root.id}`,
|
|
keyB64Url: bytesToBase64Url(legacyKey), ivPrefixB64Url: bytesToBase64Url(legacyPrefix),
|
|
name: 'legacy.bin', size: legacyData.length, chunkSize: oldSize, chunkCount: 2,
|
|
};
|
|
const legacyRecovered = new Uint8Array(await (await decryptDmFileToBlob(legacyAttachment)).arrayBuffer());
|
|
assert.equal(createHash('sha256').update(legacyRecovered).digest('hex'), createHash('sha256').update(legacyData).digest('hex'));
|
|
await assert.rejects(() => decryptDmFileToBlob({ ...legacyAttachment, chunkSize: 64 * MB }), /Некорректный манифест/);
|
|
console.log('DM R2 v3 OK: 64 MiB upload, AES-GCM/SHA-256, corruption rejected, old 8 MiB readable');
|