Skip to content

Commit

Permalink
introduce raise_on_encrypted option
Browse files Browse the repository at this point in the history
  • Loading branch information
leviwilson committed Jul 30, 2020
1 parent de5ab86 commit 2ca5ed6
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 5 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: ruby
9 changes: 8 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
require "bundler/gem_tasks"
require 'bundler/gem_tasks'
require 'rake/testtask'

task default: 'test'

Rake::TestTask.new do |t|
t.test_files = FileList['test/**/*_test.rb']
end

1 change: 1 addition & 0 deletions combine_pdf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
# spec.add_development_dependency "bundler", ">= 1.7"
spec.add_development_dependency "rake", ">= 12.3.3"
spec.add_development_dependency "minitest"
spec.add_development_dependency "minitest-around"
end
6 changes: 6 additions & 0 deletions lib/combine_pdf/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def load(file_name = '', options = {})
PDF.new(PDFParser.new(IO.read(file_name, mode: 'rb').force_encoding(Encoding::ASCII_8BIT), options))
end

def encrypted?(file_name, options = {})
parser = PDFParser.new(IO.read(file_name, mode: 'rb').force_encoding(Encoding::ASCII_8BIT), options)
parser.parse rescue Zlib::DataError
!parser.root_object[:Encrypt].nil?
end

# creats a new PDF object.
#
# Combine PDF will check to see if `string` is a filename.
Expand Down
4 changes: 3 additions & 1 deletion lib/combine_pdf/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PDFParser
# they are mainly to used to know if the file is (was) encrypted and to get more details.
attr_reader :info_object, :root_object, :names_object, :forms_object, :outlines_object, :metadata

attr_reader :allow_optional_content
attr_reader :allow_optional_content, :raise_on_encrypted
# when creating a parser, it is important to set the data (String) we wish to parse.
#
# <b>the data is required and it is not possible to set the data at a later stage</b>
Expand All @@ -58,6 +58,7 @@ def initialize(string, options = {})
@version = nil
@scanner = nil
@allow_optional_content = options[:allow_optional_content]
@raise_on_encrypted = options[:raise_on_encrypted]
end

# parse the data in the new parser (the data already set through the initialize / new method)
Expand Down Expand Up @@ -96,6 +97,7 @@ def parse
end

if @root_object[:Encrypt]
raise EncryptionError if @raise_on_encrypted
# change_references_to_actual_values @root_object
warn 'PDF is Encrypted! Attempting to decrypt - not yet fully supported.'
decryptor = PDFDecrypt.new @parsed, @root_object
Expand Down
9 changes: 8 additions & 1 deletion lib/combine_pdf/pdf_public.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ def initialize(parser = nil)
@info = {}
parser ||= PDFParser.new('')
raise TypeError, "initialization error, expecting CombinePDF::PDFParser or nil, but got #{parser.class.name}" unless parser.is_a? PDFParser
@objects = parser.parse

begin
@objects = parser.parse
rescue Zlib::DataError => e
raise CombinePDF::EncryptionError if parser.root_object[:Encrypt] && parser.raise_on_encrypted
raise e
end

# remove any existing id's
remove_old_ids
# set data from parser
Expand Down
58 changes: 58 additions & 0 deletions test/combine_pdf/load_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'bundler/setup'
require 'minitest/autorun'
require 'minitest/around/spec'
require 'combine_pdf'

describe 'CombinePDF.load' do
let(:options) { {} }

subject { CombinePDF.load "test/fixtures/files/#{file}", options }

describe 'raise_on_encrypted option' do
let(:raise_on_encrypted) { false }
let(:file) { 'sample_encrypted_pdf.pdf' }
let(:options) { { raise_on_encrypted: raise_on_encrypted } }

it('has a PDF') { assert_instance_of CombinePDF::PDF, subject }

describe 'raise_on_encrypted: true' do
let(:raise_on_encrypted) { true }

it('raises an CombinePDF::EncryptionError') do
assert_raises(CombinePDF::EncryptionError) { subject }
end

describe 'non-encrypted files' do
let(:file) { 'sample_pdf.pdf' }

it('has a PDF') { assert_instance_of CombinePDF::PDF, subject }
end

describe 'Zlib::DataError' do
let(:encrypted?) { false }
let(:parser) { Minitest::Mock.new }

around do |example|
parser.expect(:parse, true) { raise Zlib::DataError, 'incorrect data header' }
root_object = encrypted? ? { Encrypt: :yes } : { NotEncrypted: :cool }
parser.expect(:root_object, root_object)
parser.expect(:raise_on_encrypted, raise_on_encrypted)
parser.expect(:is_a?, true, [CombinePDF::PDFParser])
CombinePDF::PDFParser.stub(:new, parser) { |_| example.call }
end

it('raises Zlib::DataError') do
assert_raises(Zlib::DataError) { subject }
end

describe 'encrypted' do
let(:encrypted?) { true }

it('raises an CombinePDF::EncryptionError') do
assert_raises(CombinePDF::EncryptionError) { subject }
end
end
end
end
end
end
2 changes: 0 additions & 2 deletions test/combine_pdf/renderer_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'bundler/setup'
require 'minitest/autorun'
require 'combine_pdf/renderer'

class CombinePDFRendererTest < Minitest::Test

Expand Down
Binary file added test/fixtures/files/sample_encrypted_pdf.pdf
Binary file not shown.
Binary file added test/fixtures/files/sample_pdf.pdf
Binary file not shown.

0 comments on commit 2ca5ed6

Please sign in to comment.