From ddfd7dafdb63f3d0b787c0729ce8082a4c294202 Mon Sep 17 00:00:00 2001
From: Zdravko Iliev <zdravko.iliev@vereign.com>
Date: Fri, 18 Mar 2022 17:12:26 +0200
Subject: [PATCH] add types for signatures and test for non pdf file

---
 __tests__/index.test.ts    | 14 +++++++++++++-
 __tests__/test.txt         |  1 +
 dist/lib/generalUtils.d.ts |  1 +
 dist/lib/generalUtils.js   |  8 +++++++-
 dist/pdfParser.d.ts        |  4 ++--
 dist/pdfParser.js          |  4 ++++
 dist/types.d.ts            | 16 +++++++++++++---
 src/lib/generalUtils.ts    |  8 ++++++++
 src/pdfParser.ts           |  9 +++++++--
 src/types.ts               | 17 ++++++++++++++---
 10 files changed, 70 insertions(+), 12 deletions(-)
 create mode 100644 __tests__/test.txt

diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts
index 686e3cd..dc45347 100644
--- a/__tests__/index.test.ts
+++ b/__tests__/index.test.ts
@@ -11,8 +11,8 @@ describe("PDF parser", () => {
     );
 
     const parser = new PDFparser(file);
-
     const actual = await parser.getPDFMeta();
+
     expect(actual.pages).toEqual(2);
   });
 
@@ -35,4 +35,16 @@ describe("PDF parser", () => {
       expect(error).toBeInstanceOf(AppError);
     }
   });
+
+  it("should throw error if file type is different then pdf", async () => {
+    const file = fs.readFileSync(path.resolve(__dirname, "./test.txt"));
+
+    try {
+      const parser = new PDFparser(file);
+      const actual = await parser.getPDFMeta();
+    } catch (error) {
+      expect(error).toBeInstanceOf(AppError);
+      expect(error.message).toEqual("Only pdf file type is supported");
+    }
+  });
 });
diff --git a/__tests__/test.txt b/__tests__/test.txt
new file mode 100644
index 0000000..7f489a9
--- /dev/null
+++ b/__tests__/test.txt
@@ -0,0 +1 @@
+test txt file for tests
diff --git a/dist/lib/generalUtils.d.ts b/dist/lib/generalUtils.d.ts
index 71ffde6..8bdf208 100644
--- a/dist/lib/generalUtils.d.ts
+++ b/dist/lib/generalUtils.d.ts
@@ -6,3 +6,4 @@ export declare const getByteRange: (pdfBuffer: Buffer) => {
 };
 export declare const preparePDF: (pdf: any) => Buffer;
 export declare const getMetaRegexMatch: (keyName: string) => (str: any) => any;
+export declare const isPDF: (buf: Buffer) => boolean;
diff --git a/dist/lib/generalUtils.js b/dist/lib/generalUtils.js
index ff97296..f757ed5 100644
--- a/dist/lib/generalUtils.js
+++ b/dist/lib/generalUtils.js
@@ -1,6 +1,6 @@
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
-exports.getMetaRegexMatch = exports.preparePDF = exports.getByteRange = exports.checkForSubFilter = void 0;
+exports.isPDF = exports.getMetaRegexMatch = exports.preparePDF = exports.getByteRange = exports.checkForSubFilter = void 0;
 const errors_1 = require("./errors");
 const DEFAULT_BYTE_RANGE_PLACEHOLDER = "**********";
 const checkForSubFilter = (pdfBuffer) => {
@@ -50,3 +50,9 @@ const getMetaRegexMatch = (keyName) => (str) => {
     return meta;
 };
 exports.getMetaRegexMatch = getMetaRegexMatch;
+const isPDF = (buf) => {
+    return (Buffer.isBuffer(buf) &&
+        buf.lastIndexOf("%PDF-") === 0 &&
+        buf.lastIndexOf("%%EOF") > -1);
+};
+exports.isPDF = isPDF;
diff --git a/dist/pdfParser.d.ts b/dist/pdfParser.d.ts
index 3dac58c..05d9f6f 100644
--- a/dist/pdfParser.d.ts
+++ b/dist/pdfParser.d.ts
@@ -1,10 +1,10 @@
 /// <reference types="node" />
-import { IgetMetaResponse } from "./types";
+import { IGetMetaResponse } from "./types";
 declare class PDFparser {
     readonly document: any;
     readonly config: any;
     constructor(document: Buffer);
-    getPDFMeta: () => Promise<IgetMetaResponse>;
+    getPDFMeta: () => Promise<IGetMetaResponse>;
     insertQrCode: (imgBytes: ArrayBuffer, url: string, scaleFactor: number) => Promise<ArrayBuffer>;
     private createPageLinkAnnotation;
 }
diff --git a/dist/pdfParser.js b/dist/pdfParser.js
index de661c6..94d4530 100644
--- a/dist/pdfParser.js
+++ b/dist/pdfParser.js
@@ -15,12 +15,16 @@ const config_1 = require("./config");
 const lib_1 = require("./lib");
 const timeUtils_1 = require("./lib/timeUtils");
 const errors_1 = require("./lib/errors");
+const generalUtils_1 = require("./lib/generalUtils");
 class PDFparser {
     constructor(document) {
         this.getPDFMeta = () => __awaiter(this, void 0, void 0, function* () {
             if (!(this.document instanceof Buffer)) {
                 throw new errors_1.AppError("Document is not Buffer");
             }
+            if (!(0, generalUtils_1.isPDF)(this.document)) {
+                throw new errors_1.AppError("Only pdf file type is supported");
+            }
             try {
                 const signaturesMeta = yield (0, lib_1.verifyPDF)(this.document);
                 const pdfMeta = yield pdfdataextract_1.PdfData.extract(this.document, config_1.config);
diff --git a/dist/types.d.ts b/dist/types.d.ts
index fcab7ba..ab29491 100644
--- a/dist/types.d.ts
+++ b/dist/types.d.ts
@@ -1,4 +1,4 @@
-export interface Icert {
+export interface ICert {
     clientCertificate: boolean;
     issuedBy: {
         countryName: string;
@@ -17,9 +17,19 @@ export interface Icert {
     };
     pemCertificate: string;
 }
-export interface IgetMetaResponse {
+export interface ISignature {
+    isExpired: boolean;
+    meta: {
+        certs: Array<ICert>;
+        reason: string;
+        contactInfo: string;
+        location: string;
+        signDate: string;
+    };
+}
+export interface IGetMetaResponse {
     expired?: boolean;
-    signatures?: Array<any>;
+    signatures?: Array<ISignature>;
     pages: number;
     title: string;
     author: string;
diff --git a/src/lib/generalUtils.ts b/src/lib/generalUtils.ts
index 153dcc9..7c749b5 100644
--- a/src/lib/generalUtils.ts
+++ b/src/lib/generalUtils.ts
@@ -57,3 +57,11 @@ export const getMetaRegexMatch = (keyName: string) => (str) => {
 
   return meta;
 };
+
+export const isPDF = (buf: Buffer): boolean => {
+  return (
+    Buffer.isBuffer(buf) &&
+    buf.lastIndexOf("%PDF-") === 0 &&
+    buf.lastIndexOf("%%EOF") > -1
+  );
+};
diff --git a/src/pdfParser.ts b/src/pdfParser.ts
index 44548c4..0729fa8 100644
--- a/src/pdfParser.ts
+++ b/src/pdfParser.ts
@@ -1,10 +1,11 @@
 import { PDFName, PDFPage, PDFString, PDFDocument } from "pdf-lib";
 import { PdfData } from "pdfdataextract";
 import { config } from "./config";
-import { IgetMetaResponse } from "./types";
+import { IGetMetaResponse } from "./types";
 import { verifyPDF } from "./lib";
 import { formatPdfTime } from "./lib/timeUtils";
 import { AppError, GeneralError } from "./lib/errors";
+import { isPDF } from "./lib/generalUtils";
 class PDFparser {
   readonly document;
   readonly config;
@@ -14,11 +15,15 @@ class PDFparser {
     this.config = config;
   }
 
-  getPDFMeta = async (): Promise<IgetMetaResponse> => {
+  getPDFMeta = async (): Promise<IGetMetaResponse> => {
     if (!(this.document instanceof Buffer)) {
       throw new AppError("Document is not Buffer");
     }
 
+    if (!isPDF(this.document)) {
+      throw new AppError("Only pdf file type is supported");
+    }
+
     try {
       const signaturesMeta = await verifyPDF(this.document);
       const pdfMeta = await PdfData.extract(this.document, config);
diff --git a/src/types.ts b/src/types.ts
index 30659dd..d5e5128 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -1,4 +1,4 @@
-export interface Icert {
+export interface ICert {
   clientCertificate: boolean;
   issuedBy: {
     countryName: string;
@@ -18,11 +18,22 @@ export interface Icert {
   pemCertificate: string;
 }
 
-export interface IgetMetaResponse {
+export interface ISignature {
+  isExpired: boolean;
+  meta: {
+    certs: Array<ICert>;
+    reason: string;
+    contactInfo: string;
+    location: string;
+    signDate: string;
+  };
+}
+
+export interface IGetMetaResponse {
   // verified: boolean;
   // authenticity: boolean;
   expired?: boolean;
-  signatures?: Array<any>;
+  signatures?: Array<ISignature>;
   pages: number;
   title: string;
   author: string;
-- 
GitLab