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
.idea
yarn-error.log
__tests__/outputs/**
.DS_Store
......@@ -2,7 +2,7 @@ import * as fs from "fs";
import * as path from "path";
import { describe, it, expect } from "@jest/globals";
import PDFparser from "../src/pdfParser";
import { AppError } from "../src/lib/errors";
import { AppError, GeneralError } from "../src/lib/errors";
describe("PDF parser", () => {
it("should return pdf document metadata including signatures", async () => {
......@@ -49,4 +49,17 @@ describe("PDF parser", () => {
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 = "**********";
export const checkForSubFilter = (pdfBuffer: Buffer) => {
const matches = pdfBuffer.toString().match(/\/SubFilter\s*\/([\w.]*)/);
const subFilter = Array.isArray(matches) && matches[1];
if (!subFilter) {
throw new AppError("Can not find subfilter");
}
......@@ -16,7 +17,9 @@ export const checkForSubFilter = (pdfBuffer: Buffer) => {
];
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) => {
try {
checkForSubFilter(pdfBuffer);
} catch (error) {
console.log("no supported signatures found");
return null;
throw new Error(error.message);
}
try {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment