node-signpdf
Advanced tools
Comparing version
# CHANGELOG | ||
## [1.3.2] | ||
* Fixed detection of ByteRange (including when it is a placeholder); | ||
* Bumped node-forge version; | ||
## [1.3.0] | ||
@@ -4,0 +9,0 @@ |
@@ -24,1 +24,2 @@ # Contributing | ||
* [brunoserrano](https://github.com/brunoserrano) | ||
* [waaronking](https://github.com/waaronking) |
@@ -30,2 +30,8 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "findByteRange", { | ||
enumerable: true, | ||
get: function () { | ||
return _findByteRange.default; | ||
} | ||
}); | ||
@@ -40,4 +46,6 @@ var _extractSignature = _interopRequireDefault(require("./extractSignature")); | ||
var _findByteRange = _interopRequireDefault(require("./findByteRange")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
'This string is added so that jest collects coverage for this file'; // eslint-disable-line |
@@ -48,12 +48,13 @@ "use strict"; | ||
const byteRangePlaceholder = [0, `/${this.byteRangePlaceholder}`, `/${this.byteRangePlaceholder}`, `/${this.byteRangePlaceholder}`]; | ||
const byteRangeString = `/ByteRange [${byteRangePlaceholder.join(' ')}]`; | ||
const byteRangePos = pdf.indexOf(byteRangeString); | ||
const { | ||
byteRangePlaceholder | ||
} = (0, _helpers.findByteRange)(pdf); | ||
if (byteRangePos === -1) { | ||
throw new _SignPdfError.default(`Could not find ByteRange placeholder: ${byteRangeString}`, _SignPdfError.default.TYPE_PARSE); | ||
} // Calculate the actual ByteRange that needs to replace the placeholder. | ||
if (!byteRangePlaceholder) { | ||
throw new _SignPdfError.default(`Could not find empty ByteRange placeholder: ${byteRangePlaceholder}`, _SignPdfError.default.TYPE_PARSE); | ||
} | ||
const byteRangePos = pdf.indexOf(byteRangePlaceholder); // Calculate the actual ByteRange that needs to replace the placeholder. | ||
const byteRangeEnd = byteRangePos + byteRangeString.length; | ||
const byteRangeEnd = byteRangePos + byteRangePlaceholder.length; | ||
const contentsTagPos = pdf.indexOf('/Contents ', byteRangeEnd); | ||
@@ -69,3 +70,3 @@ const placeholderPos = pdf.indexOf('<', contentsTagPos); | ||
let actualByteRange = `/ByteRange [${byteRange.join(' ')}]`; | ||
actualByteRange += ' '.repeat(byteRangeString.length - actualByteRange.length); // Replace the /ByteRange placeholder with the actual ByteRange | ||
actualByteRange += ' '.repeat(byteRangePlaceholder.length - actualByteRange.length); // Replace the /ByteRange placeholder with the actual ByteRange | ||
@@ -72,0 +73,0 @@ pdf = Buffer.concat([pdf.slice(0, byteRangePos), Buffer.from(actualByteRange), pdf.slice(byteRangeEnd)]); // Remove the placeholder signature |
{ | ||
"name": "node-signpdf", | ||
"version": "1.3.0", | ||
"version": "1.3.2", | ||
"description": "Simple signing of PDFs in node.", | ||
@@ -45,3 +45,3 @@ "repository": { | ||
"peerDependencies": { | ||
"node-forge": "^0.7.6" | ||
"node-forge": "^0.10.0" | ||
}, | ||
@@ -65,5 +65,5 @@ "devDependencies": { | ||
"jest": "^24.5.0", | ||
"node-forge": "^0.7.6", | ||
"node-forge": "^0.10.0", | ||
"pdfkit": "^0.10.0" | ||
} | ||
} |
@@ -5,3 +5,4 @@ export {default as extractSignature} from './extractSignature'; | ||
export {default as removeTrailingNewLine} from './removeTrailingNewLine'; | ||
export {default as findByteRange} from './findByteRange'; | ||
'This string is added so that jest collects coverage for this file'; // eslint-disable-line |
import forge from 'node-forge'; | ||
import SignPdfError from './SignPdfError'; | ||
import {removeTrailingNewLine} from './helpers'; | ||
import {removeTrailingNewLine, findByteRange} from './helpers'; | ||
@@ -42,13 +42,7 @@ export {default as SignPdfError} from './SignPdfError'; | ||
// Find the ByteRange placeholder. | ||
const byteRangePlaceholder = [ | ||
0, | ||
`/${this.byteRangePlaceholder}`, | ||
`/${this.byteRangePlaceholder}`, | ||
`/${this.byteRangePlaceholder}`, | ||
]; | ||
const byteRangeString = `/ByteRange [${byteRangePlaceholder.join(' ')}]`; | ||
const byteRangePos = pdf.indexOf(byteRangeString); | ||
if (byteRangePos === -1) { | ||
const {byteRangePlaceholder} = findByteRange(pdf); | ||
if (!byteRangePlaceholder) { | ||
throw new SignPdfError( | ||
`Could not find ByteRange placeholder: ${byteRangeString}`, | ||
`Could not find empty ByteRange placeholder: ${byteRangePlaceholder}`, | ||
SignPdfError.TYPE_PARSE, | ||
@@ -58,4 +52,6 @@ ); | ||
const byteRangePos = pdf.indexOf(byteRangePlaceholder); | ||
// Calculate the actual ByteRange that needs to replace the placeholder. | ||
const byteRangeEnd = byteRangePos + byteRangeString.length; | ||
const byteRangeEnd = byteRangePos + byteRangePlaceholder.length; | ||
const contentsTagPos = pdf.indexOf('/Contents ', byteRangeEnd); | ||
@@ -71,3 +67,3 @@ const placeholderPos = pdf.indexOf('<', contentsTagPos); | ||
let actualByteRange = `/ByteRange [${byteRange.join(' ')}]`; | ||
actualByteRange += ' '.repeat(byteRangeString.length - actualByteRange.length); | ||
actualByteRange += ' '.repeat(byteRangePlaceholder.length - actualByteRange.length); | ||
@@ -74,0 +70,0 @@ // Replace the /ByteRange placeholder with the actual ByteRange |
@@ -31,3 +31,3 @@ import PDFDocument from 'pdfkit'; | ||
if (requestParams.pages < 1) { | ||
requestParams.pages = 1 | ||
requestParams.pages = 1; | ||
} | ||
@@ -191,28 +191,28 @@ | ||
it('signs big PDF twice producing big AcroForm ID on the first time', async () => { | ||
let pdfBuffer = await createPdf({ | ||
pages: 100, | ||
}); | ||
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`); | ||
let pdfBuffer = await createPdf({ | ||
pages: 100, | ||
}); | ||
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`); | ||
pdfBuffer = signer.sign(pdfBuffer, p12Buffer); | ||
expect(pdfBuffer instanceof Buffer).toBe(true); | ||
pdfBuffer = signer.sign(pdfBuffer, p12Buffer); | ||
expect(pdfBuffer instanceof Buffer).toBe(true); | ||
const {signature, signedData} = extractSignature(pdfBuffer); | ||
expect(typeof signature === 'string').toBe(true); | ||
expect(signedData instanceof Buffer).toBe(true); | ||
const secondP12Buffer = fs.readFileSync(`${__dirname}/../resources/withpass.p12`); | ||
pdfBuffer = plainAddPlaceholder({ | ||
pdfBuffer: pdfBuffer, | ||
reason: 'second', | ||
location: 'test location', | ||
signatureLength: 1592, | ||
}); | ||
pdfBuffer = signer.sign(pdfBuffer, secondP12Buffer, {passphrase: 'node-signpdf'}); | ||
expect(pdfBuffer instanceof Buffer).toBe(true); | ||
const {signature: secondSignature, signedData: secondSignatureData} = extractSignature(pdfBuffer, 2); | ||
expect(typeof secondSignature === 'string').toBe(true); | ||
expect(secondSignatureData instanceof Buffer).toBe(true); | ||
}) | ||
const {signature, signedData} = extractSignature(pdfBuffer); | ||
expect(typeof signature === 'string').toBe(true); | ||
expect(signedData instanceof Buffer).toBe(true); | ||
const secondP12Buffer = fs.readFileSync(`${__dirname}/../resources/withpass.p12`); | ||
pdfBuffer = plainAddPlaceholder({ | ||
pdfBuffer, | ||
reason: 'second', | ||
location: 'test location', | ||
signatureLength: 1592, | ||
}); | ||
pdfBuffer = signer.sign(pdfBuffer, secondP12Buffer, {passphrase: 'node-signpdf'}); | ||
expect(pdfBuffer instanceof Buffer).toBe(true); | ||
const {signature: secondSignature, signedData: secondSignatureData} = extractSignature(pdfBuffer, 2); | ||
expect(typeof secondSignature === 'string').toBe(true); | ||
expect(secondSignatureData instanceof Buffer).toBe(true); | ||
}); | ||
it('signs a ready pdf containing a link', async () => { | ||
@@ -326,2 +326,14 @@ const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`); | ||
}); | ||
it('expects siging to fail because of no byteRangePlaceholder available to sign', async () => { | ||
try { | ||
const pdfBuffer = fs.readFileSync(`${__dirname}/../resources/signed.pdf`); | ||
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`); | ||
signer.sign(pdfBuffer, p12Buffer); | ||
expect('here').not.toBe('here'); | ||
} catch (e) { | ||
expect(e instanceof SignPdfError).toBe(true); | ||
expect(e.type).toBe(SignPdfError.TYPE_PARSE); | ||
} | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
528283
1.12%152
2.01%4455
3.34%12
9.09%