diff --git a/openpdf-core/src/main/java/org/openpdf/text/FontFactoryImp.java b/openpdf-core/src/main/java/org/openpdf/text/FontFactoryImp.java index d712ce3d1..7ae4994d5 100644 --- a/openpdf-core/src/main/java/org/openpdf/text/FontFactoryImp.java +++ b/openpdf-core/src/main/java/org/openpdf/text/FontFactoryImp.java @@ -49,12 +49,11 @@ package org.openpdf.text; -import org.openpdf.text.html.Markup; -import org.openpdf.text.pdf.BaseFont; import java.awt.Color; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.List; @@ -64,6 +63,8 @@ import java.util.Set; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.openpdf.text.html.Markup; +import org.openpdf.text.pdf.BaseFont; /** @@ -242,10 +243,10 @@ public Font getFont(String fontname, String encoding, boolean embedded, float si } /** - * Constructs a Font-object for a font name that comes from a document, for example the face name - * of a font in an image or in HTML. Other than {@link #getFont(String, String, boolean, float, int, Color)}, - * this method never uses the name as the path or URL of a font file: the name is only resolved against the - * registered fonts, the registered font families and the built-in fonts. + * Constructs a Font-object for a font name that comes from a document, for example the face name of a + * font in an image or in HTML. Other than {@link #getFont(String, String, boolean, float, int, Color)}, this method + * never uses the name as the path or URL of a font file: the name is only resolved against the registered fonts, + * the registered font families and the built-in fonts. * * @param fontname the name of the font * @param encoding the encoding of the font @@ -564,7 +565,7 @@ public void registerFamily(String familyName, String fullName, String path) { tmp = new ArrayList<>(); tmp.add(fullName); fontFamilies.put(familyName, tmp); - } else { + } else if (!tmp.contains(fullName)) { int fullNameLength = fullName.length(); boolean inserted = false; for (int j = 0; j < tmp.size(); ++j) { @@ -776,6 +777,27 @@ public Set getRegisteredFamilies() { return Utilities.getKeySet(fontFamilies); } + /** + * Gets the font names registered for a given font family. Package-private: intended for testing the deduplication + * done by {@link #registerFamily}, not part of the public API. + * + * @param family the font family (case-insensitive) + * @return an unmodifiable copy of the registered font names for the family, or an empty list if the family is not + * registered + */ + List getRegisteredFamily(String family) { + lock.readLock().lock(); + try { + List members = fontFamilies.get(family.toLowerCase(Locale.ROOT)); + if (members == null) { + return Collections.emptyList(); + } + return Collections.unmodifiableList(new ArrayList<>(members)); + } finally { + lock.readLock().unlock(); + } + } + /** * Checks if a certain font is registered. * diff --git a/openpdf-core/src/test/java/org/openpdf/text/FontFactoryImpTest.java b/openpdf-core/src/test/java/org/openpdf/text/FontFactoryImpTest.java new file mode 100644 index 000000000..c0d1e1783 --- /dev/null +++ b/openpdf-core/src/test/java/org/openpdf/text/FontFactoryImpTest.java @@ -0,0 +1,29 @@ +package org.openpdf.text; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class FontFactoryImpTest { + + /** + * Registering the same family/name combination several times must not add duplicate entries to the font family + * list, otherwise the list grows unbounded and slows down the lookup loop in {@link FontFactoryImp#getFont}. + */ + @Test + void registerFamilyShouldNotStoreDuplicateFullNames() { + FontFactoryImp fontFactory = new FontFactoryImp(); + + fontFactory.registerFamily("my-family", "My-Font-Regular", null); + fontFactory.registerFamily("my-family", "My-Font-Regular", null); + fontFactory.registerFamily("my-family", "My-Font-Regular", null); + + // A distinct member of the same family must still be added. + fontFactory.registerFamily("my-family", "My-Font-Bold", null); + + assertThat(fontFactory.getRegisteredFamilies()).contains("my-family"); + assertThat(fontFactory.getRegisteredFamily("my-family")) + .containsExactlyInAnyOrder("My-Font-Regular", "My-Font-Bold"); + } + +} \ No newline at end of file diff --git a/openpdf-core/src/test/java/org/openpdf/text/pdf/PdfNumberTest.java b/openpdf-core/src/test/java/org/openpdf/text/pdf/PdfNumberTest.java index 87f283dd7..18f8b1432 100644 --- a/openpdf-core/src/test/java/org/openpdf/text/pdf/PdfNumberTest.java +++ b/openpdf-core/src/test/java/org/openpdf/text/pdf/PdfNumberTest.java @@ -5,15 +5,37 @@ import org.junit.jupiter.api.Test; /** - * Tests for issue #1585: some PDF producers (e.g. mPDF) write the /P permissions entry of the - * encryption dictionary as an unsigned 32-bit integer (4294963412) instead of the signed value - * required by ISO 32000 (-3884). {@link PdfNumber#intValue()} used a direct double-to-int cast, - * which clamps values above {@code Integer.MAX_VALUE} (JLS 5.1.3) instead of preserving the bit - * pattern, so the RC4/AES key derivation used a wrong /P and threw BadPasswordException even for - * documents with an empty user password. + * Tests for issue #1585: some PDF producers (e.g. mPDF) write the /P permissions entry of the encryption dictionary as + * an unsigned 32-bit integer (4294963412) instead of the signed value required by ISO 32000 (-3884). + * {@link PdfNumber#intValue()} used a direct double-to-int cast, which clamps values above {@code Integer.MAX_VALUE} + * (JLS 5.1.3) instead of preserving the bit pattern, so the RC4/AES key derivation used a wrong /P and threw + * BadPasswordException even for documents with an empty user password. */ class PdfNumberTest { + /** + * Some PDF producers write the encryption dictionary's {@code /P} (permissions) entry using its equivalent unsigned + * 32-bit decimal representation instead of the negative signed value, e.g. {@code 4294965956} instead of + * {@code -1340}. {@link PdfNumber#intValue()} must truncate such a value to its low-order 32 bits rather than + * clamping it to {@link Integer#MAX_VALUE}, otherwise the permission bits used to derive the decryption key no + * longer match the value the producer used, and a document with an empty user password is wrongly reported as + * requiring a password. + */ + @Test + void intValueTruncatesOutOfRangeUnsignedRepresentationInsteadOfClamping() { + assertEquals(-1340, new PdfNumber("4294965956").intValue()); + assertEquals(-1, new PdfNumber("4294967295").intValue()); + assertEquals(Integer.MIN_VALUE, new PdfNumber(String.valueOf(1L << 31)).intValue()); + } + + @Test + void intValueRoundTripsValuesWithinIntRange() { + assertEquals(-1340, new PdfNumber("-1340").intValue()); + assertEquals(0, new PdfNumber("0").intValue()); + assertEquals(Integer.MAX_VALUE, new PdfNumber(String.valueOf(Integer.MAX_VALUE)).intValue()); + assertEquals(Integer.MIN_VALUE, new PdfNumber(String.valueOf(Integer.MIN_VALUE)).intValue()); + } + @Test void intValueWrapsUnsignedInt32() { // 4294963412 == 0xFFFFF0D4 == -3884 as signed int32 (the mPDF example from the issue) diff --git a/openpdf-core/src/test/java/org/openpdf/text/pdf/encryption/DecryptUnsignedPermissionsTest.java b/openpdf-core/src/test/java/org/openpdf/text/pdf/encryption/DecryptUnsignedPermissionsTest.java new file mode 100644 index 000000000..9e8d1aa60 --- /dev/null +++ b/openpdf-core/src/test/java/org/openpdf/text/pdf/encryption/DecryptUnsignedPermissionsTest.java @@ -0,0 +1,38 @@ +package org.openpdf.text.pdf.encryption; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.io.InputStream; +import org.junit.jupiter.api.Test; +import org.openpdf.text.pdf.PdfReader; + +/** + * Regression test for a document that has no user password but is nevertheless reported by {@link PdfReader} as + * requiring one, because its {@code /Encrypt} dictionary writes the {@code /P} (permissions) entry using the unsigned + * 32-bit decimal representation of a negative value (e.g. {@code 4294965956} instead of {@code -1340}) - a convention + * some PDF producers use. + *

+ * {@link org.openpdf.text.pdf.PdfNumber#intValue()} used a plain {@code (int)} narrowing cast on the underlying + * double, which clamps such an out-of-range value to {@link Integer#MAX_VALUE} instead of truncating it + * to its low-order 32 bits. The corrupted permission value then fed into the standard security handler's key + * derivation, producing a user key that did not match {@code /U}, so {@link PdfReader} threw a + * {@link org.openpdf.text.exceptions.BadPasswordException} even though the correct (empty) password was supplied. + */ +class DecryptUnsignedPermissionsTest { + + @Test + void opensWithEmptyPasswordDespiteUnsignedPermissionsValue() throws IOException { + try (InputStream resource = getClass() + .getResourceAsStream("/permissions/empty-user-password-unsigned-p-value.pdf")) { + PdfReader pdfReader = new PdfReader(resource); + assertTrue(pdfReader.isEncrypted(), "PdfReader fails to report test file to be encrypted."); + assertFalse(pdfReader.isOwnerPasswordUsed(), "PdfReader fails to report limited permissions."); + assertEquals(1, pdfReader.getNumberOfPages(), + "PdfReader fails to report the correct number of pages"); + pdfReader.close(); + } + } +}