Skip to content
Snippets Groups Projects
Commit ae7c2355 authored by Zdravko Iliev's avatar Zdravko Iliev
Browse files

fix: throw error if there is unsported signature in the pdf

parent 6f7230d0
No related branches found
No related tags found
No related merge requests found
...@@ -2,3 +2,4 @@ node_modules ...@@ -2,3 +2,4 @@ node_modules
.idea .idea
yarn-error.log yarn-error.log
__tests__/outputs/** __tests__/outputs/**
.DS_Store
...@@ -2,7 +2,7 @@ import * as fs from "fs"; ...@@ -2,7 +2,7 @@ import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import { describe, it, expect } from "@jest/globals"; import { describe, it, expect } from "@jest/globals";
import PDFparser from "../src/pdfParser"; import PDFparser from "../src/pdfParser";
import { AppError } from "../src/lib/errors"; import { AppError, GeneralError } from "../src/lib/errors";
describe("PDF parser", () => { describe("PDF parser", () => {
it("should return pdf document metadata including signatures", async () => { it("should return pdf document metadata including signatures", async () => {
...@@ -49,4 +49,17 @@ describe("PDF parser", () => { ...@@ -49,4 +49,17 @@ describe("PDF parser", () => {
expect(error.message).toEqual("Only pdf file type is supported"); expect(error.message).toEqual("Only pdf file type is supported");
} }
}); });
it("should throw error when parsing file with unsupported subfilter type", async () => {
const file = fs.readFileSync(
path.resolve(__dirname, "./source/sample07.pdf")
);
try {
const parser = new PDFparser(file);
await parser.getPDFMeta();
} catch (error) {
expect(error).toBeInstanceOf(GeneralError);
}
});
}); });
File added
...@@ -5,6 +5,7 @@ const DEFAULT_BYTE_RANGE_PLACEHOLDER = "**********"; ...@@ -5,6 +5,7 @@ const DEFAULT_BYTE_RANGE_PLACEHOLDER = "**********";
export const checkForSubFilter = (pdfBuffer: Buffer) => { export const checkForSubFilter = (pdfBuffer: Buffer) => {
const matches = pdfBuffer.toString().match(/\/SubFilter\s*\/([\w.]*)/); const matches = pdfBuffer.toString().match(/\/SubFilter\s*\/([\w.]*)/);
const subFilter = Array.isArray(matches) && matches[1]; const subFilter = Array.isArray(matches) && matches[1];
if (!subFilter) { if (!subFilter) {
throw new AppError("Can not find subfilter"); throw new AppError("Can not find subfilter");
} }
...@@ -16,7 +17,9 @@ export const checkForSubFilter = (pdfBuffer: Buffer) => { ...@@ -16,7 +17,9 @@ export const checkForSubFilter = (pdfBuffer: Buffer) => {
]; ];
if (!supportedTypes.includes(subFilter.trim().toLowerCase())) { if (!supportedTypes.includes(subFilter.trim().toLowerCase())) {
throw new AppError("Subfilter not supported"); throw new AppError(
`Subfilter not supported - ${subFilter.trim().toLowerCase()}`
);
} }
}; };
......
...@@ -7,8 +7,7 @@ export const verifyPDF = (pdf: Buffer) => { ...@@ -7,8 +7,7 @@ export const verifyPDF = (pdf: Buffer) => {
try { try {
checkForSubFilter(pdfBuffer); checkForSubFilter(pdfBuffer);
} catch (error) { } catch (error) {
console.log("no supported signatures found"); throw new Error(error.message);
return null;
} }
try { try {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment