Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,11 @@ func (r *Reader) initEncrypt(password string) error {
h.Write([]byte(O))
h.Write([]byte{byte(P), byte(P >> 8), byte(P >> 16), byte(P >> 24)})
h.Write([]byte(ID))
if R >= 4 {
if encryptMetadata, ok := encrypt["EncryptMetadata"].(bool); ok && !encryptMetadata {
h.Write([]byte{0xff, 0xff, 0xff, 0xff})
}
}
key := h.Sum(nil)

if R >= 3 {
Expand Down
25 changes: 25 additions & 0 deletions read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package pdf

import (
"bytes"
"context"
"io"
"strings"
"testing"
)
Expand Down Expand Up @@ -151,3 +153,26 @@ func TestNewReaderMaliciousPDF(t *testing.T) {
t.Fatal("expected error from malicious PDF, got nil")
}
}

// Regression test for https://github.com/ledongthuc/pdf/issues/82:
// AES-128 encrypted PDFs (V=4, R=4) with /EncryptMetadata false and an empty
// user password must open successfully instead of returning ErrInvalidPassword.
func TestOpenEncryptedMetadataFalse(t *testing.T) {
f, r, err := Open("testdata/encrypted_metadata_false.pdf")
if err != nil {
t.Fatalf("Open: %v", err)
}
defer f.Close()

b, err := r.GetPlainText(context.Background())
if err != nil {
t.Fatalf("GetPlainText: %v", err)
}
text, err := io.ReadAll(b)
if err != nil {
t.Fatalf("ReadAll: %v", err)
}
if !strings.Contains(string(text), "hello world") {
t.Fatalf("unexpected text: %q", text)
}
}
Binary file added testdata/encrypted_metadata_false.pdf
Binary file not shown.