Skip to content

Commit 9aaa4d5

Browse files
committed
chore: 'security-hardening' for OpenPDF handler #567
1 parent bda4b73 commit 9aaa4d5

File tree

6 files changed

+117
-8
lines changed

6 files changed

+117
-8
lines changed

fj-doc-base/src/main/java/org/fugerit/java/doc/base/typehelper/generic/SecurityHardeningUtil.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import lombok.extern.slf4j.Slf4j;
44
import org.fugerit.java.doc.base.model.DocBase;
55

6+
import java.util.function.Supplier;
7+
68
@Slf4j
79
public class SecurityHardeningUtil {
810

@@ -31,4 +33,17 @@ public static int findCurrentValue( String securityHardeningInfo ) {
3133
return SecurityHardeningConsts.SECURITY_HARDENING_DISABLED;
3234
}
3335

36+
public static <T> T applyHardening( DocBase docBase, int minimumSecurityHardening, Supplier<T> securityHardening, Supplier<T> noSecurityHardening) {
37+
return applyHardening( docBase.getStableInfo().getProperty(GenericConsts.SECURITY_HARDENING, GenericConsts.SECURITY_HARDENING_DEFAULT), minimumSecurityHardening, securityHardening, noSecurityHardening );
38+
}
39+
40+
public static <T> T applyHardening( String securityHardeningInfo, int minimumSecurityHardening, Supplier<T> securityHardening, Supplier<T> noSecurityHardening) {
41+
int valueSecurityHardening = SecurityHardeningUtil.findCurrentValue( securityHardeningInfo );
42+
if ( valueSecurityHardening >= minimumSecurityHardening) {
43+
return securityHardening.get();
44+
} else {
45+
return noSecurityHardening.get();
46+
}
47+
}
48+
3449
}

fj-doc-base/src/test/java/test/org/fugerit/java/doc/base/typehelper/generic/TestSecurityHardeningUtil.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,24 @@ void testDocBase() {
4141
Assertions.assertEquals( SecurityHardeningConsts.SECURITY_HARDENING_1, SecurityHardeningUtil.findCurrentValue( docBase ) );
4242
}
4343

44+
private static final String YES = "YES";
45+
46+
private static final String NO = "NO";
47+
48+
@Test
49+
void testApplyNoSecurityHardening() {
50+
DocBase docBase = new DocBase();
51+
docBase.getStableInfoSafe().setProperty( GenericConsts.SECURITY_HARDENING, GenericConsts.SECURITY_HARDENING_DISABLED );
52+
String result = SecurityHardeningUtil.applyHardening( docBase, SecurityHardeningConsts.SECURITY_HARDENING_1, () -> YES, () -> NO );
53+
Assertions.assertEquals( NO, result );
54+
}
55+
56+
@Test
57+
void testApplySecurityHardening() {
58+
DocBase docBase = new DocBase();
59+
docBase.getStableInfoSafe().setProperty( GenericConsts.SECURITY_HARDENING, GenericConsts.SECURITY_HARDENING_1 );
60+
String result = SecurityHardeningUtil.applyHardening( GenericConsts.SECURITY_HARDENING_1 , SecurityHardeningConsts.SECURITY_HARDENING_1, () -> YES, () -> NO );
61+
Assertions.assertEquals( YES, result );
62+
}
63+
4464
}

fj-doc-mod-fop/src/main/java/org/fugerit/java/doc/mod/fop/PdfFopTypeHandler.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.fugerit.java.core.xml.dom.DOMUtils;
2828
import org.fugerit.java.doc.base.config.*;
2929
import org.fugerit.java.doc.base.model.DocBase;
30-
import org.fugerit.java.doc.base.typehelper.generic.GenericConsts;
3130
import org.fugerit.java.doc.base.typehelper.generic.SecurityHardeningConsts;
3231
import org.fugerit.java.doc.base.typehelper.generic.SecurityHardeningUtil;
3332
import org.fugerit.java.doc.mod.fop.config.FopConfigClassLoaderWrapper;
@@ -148,12 +147,7 @@ private static String getApacheFOPVersion() {
148147
private static final String PRODUCER_DEFAULT_SH1 = String.format( VenusVersion.VENUS_PRODUCER_FORMAT_SH1, DocConfig.FUGERIT_VENUS_DOC , PRODUCER_OVER );
149148

150149
private String findDefaultProducer( DocBase docBase ) {
151-
int valueSecurityHardening = SecurityHardeningUtil.findCurrentValue( docBase );
152-
if ( valueSecurityHardening >= SecurityHardeningConsts.SECURITY_HARDENING_1 ) {
153-
return PRODUCER_DEFAULT_SH1;
154-
} else {
155-
return PRODUCER_DEFAULT;
156-
}
150+
return SecurityHardeningUtil.applyHardening( docBase, SecurityHardeningConsts.SECURITY_HARDENING_1, () -> PRODUCER_DEFAULT_SH1, () -> PRODUCER_DEFAULT );
157151
}
158152

159153
public PdfFopTypeHandler( Charset charset, FopConfig fopConfig, boolean accessibility, boolean keepEmptyTags ) {

fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/DocumentMetaHelper.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.fugerit.java.doc.base.config.VenusVersion;
99
import org.fugerit.java.doc.base.model.DocBase;
1010
import org.fugerit.java.doc.base.typehelper.generic.GenericConsts;
11+
import org.fugerit.java.doc.base.typehelper.generic.SecurityHardeningConsts;
12+
import org.fugerit.java.doc.base.typehelper.generic.SecurityHardeningUtil;
1113

1214
public class DocumentMetaHelper {
1315

@@ -21,8 +23,22 @@ private static String getOpenPDFVersion() {
2123
return MavenProps.getProperty( "com.github.librepdf", "openpdf", MavenProps.VERSION );
2224
}
2325

26+
private static final String PRODUCER_OVER = "OpenPDF";
27+
28+
/**
29+
* OpenPDF producer
30+
*/
2431
private static final String PRODUCER_DEFAULT = String.format( VenusVersion.VENUS_PRODUCER_FORMAT, DocConfig.FUGERIT_VENUS_DOC , getModuleVersion() , Document.getProduct(), getOpenPDFVersion() );
2532

33+
/**
34+
* Security hardened producer
35+
*/
36+
private static final String PRODUCER_DEFAULT_SH1 = String.format( VenusVersion.VENUS_PRODUCER_FORMAT_SH1, DocConfig.FUGERIT_VENUS_DOC , PRODUCER_OVER );
37+
38+
private static String findDefaultProducer( DocBase docBase ) {
39+
return SecurityHardeningUtil.applyHardening( docBase, SecurityHardeningConsts.SECURITY_HARDENING_1, () -> PRODUCER_DEFAULT_SH1, () -> PRODUCER_DEFAULT );
40+
}
41+
2642
private static void metaWorker(String property, UnsafeConsumer<String, Exception> fun ) {
2743
SafeFunction.applyIfNotNull( property, () -> fun.accept( property ) );
2844
}
@@ -45,7 +61,7 @@ public static void handleDocMeta(Document document, DocBase docBase) {
4561
docBase.getStableInfo().getProperty(GenericConsts.INFO_KEY_DOC_CREATOR, VenusVersion.VENUS_CREATOR),
4662
document::addCreator );
4763
metaWorker(
48-
docBase.getStableInfo().getProperty(GenericConsts.INFO_KEY_DOC_PRODUCER, PRODUCER_DEFAULT),
64+
docBase.getStableInfo().getProperty(GenericConsts.INFO_KEY_DOC_PRODUCER, findDefaultProducer(docBase)),
4965
document::addProducer );
5066
}
5167

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package test.org.fugerit.java.doc.mod.openpdf.ext;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.apache.pdfbox.pdmodel.PDDocument;
5+
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
6+
import org.fugerit.java.core.lang.helpers.ClassHelper;
7+
import org.fugerit.java.doc.base.config.DocInput;
8+
import org.fugerit.java.doc.base.config.DocOutput;
9+
import org.fugerit.java.doc.base.config.DocTypeHandler;
10+
import org.fugerit.java.doc.mod.openpdf.ext.PdfTypeHandler;
11+
import org.junit.jupiter.api.Assertions;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.io.File;
15+
import java.io.FileOutputStream;
16+
import java.io.InputStreamReader;
17+
18+
@Slf4j
19+
class TestSecurityHardeningOpenPDF {
20+
21+
private static final String TEST_PRODUCER = "Venus Fugerit Doc over OpenPDF";
22+
private static final String TEST_CREATOR = "Venus Fugerit Doc (https://venusdocs.fugerit.org)";
23+
24+
@Test
25+
void testSecurityHardening() throws Exception {
26+
DocTypeHandler handler = PdfTypeHandler.HANDLER;
27+
String fileName = "doc_security_hardening_1";
28+
File outputFile = new File( String.format( "target/%s.%s", fileName, handler.getType() ) );
29+
try ( InputStreamReader reader = new InputStreamReader( ClassHelper.loadFromDefaultClassLoader( String.format( "xml/%s.xml", fileName ) ) );
30+
FileOutputStream fos = new FileOutputStream( outputFile ) ) {
31+
handler.handle( DocInput.newInput( handler.getType(), reader ) , DocOutput.newOutput( fos ) );
32+
log.info( "file {}", outputFile.getCanonicalFile() );
33+
}
34+
try (PDDocument document = PDDocument.load(outputFile)) {
35+
PDDocumentInformation info = document.getDocumentInformation();
36+
String producer = info.getProducer();
37+
String creator = info.getCreator();
38+
log.info( "producer : {}, creator : {}", producer, creator );
39+
Assertions.assertEquals( TEST_PRODUCER, producer );
40+
Assertions.assertEquals( TEST_CREATOR, creator );
41+
}
42+
}
43+
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<doc xmlns="http://javacoredoc.fugerit.org"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://javacoredoc.fugerit.org https://www.fugerit.org/data/java/doc/xsd/doc-2-1.xsd">
4+
<metadata>
5+
<info name="margins">10;10;10;30</info>
6+
<info name="table-border-collapse">collapse</info>
7+
<info name="doc-title">Module OpenPDF Metadata Test</info>
8+
<info name="doc-subject">Simple document to test PDF security hardening</info>
9+
<info name="doc-author">fugerit79</info>
10+
<info name="doc-language">en</info>
11+
<info name="security-hardening">1</info>
12+
<footer-ext>
13+
<para align="center">Page ${currentPage}</para>
14+
</footer-ext>
15+
</metadata>
16+
<body>
17+
<h head-level="1">Security hardening doc sample</h>
18+
<para>Test security-hardening for OpenPDF Module</para>
19+
</body>
20+
</doc>

0 commit comments

Comments
 (0)