fix: PDF Timestamp bug

This commit is contained in:
alam00000 committed 2026-05-01 13:02:53 +05:30
1 parent 6f5c75b913
commit ea53689bba
27 files changed
+201 -59

No files matched your search

+38
View File
@@ -115,4 +115,42 @@ describe('timestampPdf', () => {
expect(samplePdfBytes).toEqual(originalCopy);
});
it('should pre-proxy the TSA URL when CORS proxy is configured', async () => {
vi.stubEnv(
'VITE_CORS_PROXY_URL',
'https://bentopdf-cors-proxy.bentopdf.workers.dev'
);
vi.resetModules();
const { timestampPdf: freshTimestamp } =
await import('@/js/logic/digital-sign-pdf');
mockSign.mockResolvedValueOnce(new Uint8Array([1]));
await freshTimestamp(samplePdfBytes, 'http://timestamp.digicert.com');
const callArg = vi.mocked(PdfSigner).mock.calls[0][0] as {
signdate: { url: string };
};
expect(callArg.signdate.url).toMatch(
/^https:\/\/bentopdf-cors-proxy\.bentopdf\.workers\.dev\?url=/
);
expect(callArg.signdate.url).toContain(
encodeURIComponent('http://timestamp.digicert.com')
);
});
it('should throw a clear error when HTTPS page targets HTTP TSA without proxy', async () => {
vi.stubEnv('VITE_CORS_PROXY_URL', '');
Object.defineProperty(window, 'location', {
configurable: true,
value: { protocol: 'https:', origin: 'https://www.bentopdf.com' },
});
vi.resetModules();
const { timestampPdf: freshTimestamp } =
await import('@/js/logic/digital-sign-pdf');
await expect(
freshTimestamp(samplePdfBytes, 'http://timestamp.digicert.com')
).rejects.toThrow(/HTTPS page|VITE_CORS_PROXY_URL/);
});
});
+13 -6
View File
@@ -149,12 +149,14 @@ describe('TimestampNode', () => {
expect(result.pdf[0].filename).toBe('report_timestamped.pdf');
});
it('should wrap errors from timestampPdf with TSA context', async () => {
vi.mocked(timestampPdf).mockRejectedValueOnce(new Error('Network error'));
it('should wrap errors from timestampPdf with TSA context and original cause', async () => {
const originalError = new Error('Network error');
vi.mocked(timestampPdf).mockRejectedValueOnce(originalError);
const node = new TimestampNode();
await expect(
node.data({
let caught: Error | null = null;
try {
await node.data({
pdf: [
{
type: 'pdf' as const,
@@ -163,7 +165,12 @@ describe('TimestampNode', () => {
filename: 'test.pdf',
},
],
})
).rejects.toThrow(/Failed to timestamp using TSA/);
});
} catch (err) {
caught = err as Error;
}
expect(caught).toBeInstanceOf(Error);
expect(caught?.cause).toBe(originalError);
});
});