Skip to content

Commit dabc52e

Browse files
authored
Defaulting JWT settings to false (Stirling-Tools#4416)
Defaulting the configuration settings for Stirling PDF's JWT to false to avoid any unexpected issues
1 parent 7bd31a9 commit dabc52e

File tree

15 files changed

+112
-108
lines changed

15 files changed

+112
-108
lines changed

app/common/src/main/java/stirling/software/common/model/ApplicationProperties.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,10 @@ public Provider get(String registrationId) throws UnsupportedProviderException {
303303

304304
@Data
305305
public static class Jwt {
306-
private boolean enableKeystore = true;
307-
private boolean enableKeyRotation = false;
308-
private boolean enableKeyCleanup = true;
306+
private boolean enabled = true;
307+
private boolean keyCleanup = true;
309308
private int keyRetentionDays = 7;
310-
private boolean secureCookie;
309+
private Boolean secureCookie;
311310
}
312311
}
313312

@@ -377,16 +376,19 @@ public static class TempFileManagement {
377376

378377
@JsonIgnore
379378
public String getBaseTmpDir() {
380-
return baseTmpDir != null && !baseTmpDir.isEmpty()
381-
? baseTmpDir
382-
: java.lang.System.getProperty("java.io.tmpdir") + "/stirling-pdf";
379+
if (baseTmpDir != null && !baseTmpDir.isEmpty()) {
380+
return baseTmpDir;
381+
}
382+
String tmp = java.lang.System.getProperty("java.io.tmpdir");
383+
return new File(tmp, "stirling-pdf").getPath();
383384
}
384385

385386
@JsonIgnore
386387
public String getLibreofficeDir() {
387-
return libreofficeDir != null && !libreofficeDir.isEmpty()
388-
? libreofficeDir
389-
: getBaseTmpDir() + "/libreoffice";
388+
if (libreofficeDir != null && !libreofficeDir.isEmpty()) {
389+
return libreofficeDir;
390+
}
391+
return new File(getBaseTmpDir(), "libreoffice").getPath();
390392
}
391393
}
392394

app/common/src/main/java/stirling/software/common/util/PdfUtils.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ public boolean pageCount(PDDocument pdfDocument, int pageCount, String comparato
636636
case "equal" -> actualPageCount == pageCount;
637637
case "less" -> actualPageCount < pageCount;
638638
default ->
639-
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
639+
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
640640
};
641641
}
642642

@@ -659,15 +659,9 @@ public boolean pageSize(PDDocument pdfDocument, String expectedPageSize) throws
659659
return actualPageWidth == expectedPageWidth && actualPageHeight == expectedPageHeight;
660660
}
661661

662-
/**
663-
* Key for storing the dimensions of a rendered image in a map.
664-
*/
665-
private record PdfRenderSettingsKey(float mediaBoxWidth, float mediaBoxHeight, int rotation) {
666-
}
662+
/** Key for storing the dimensions of a rendered image in a map. */
663+
private record PdfRenderSettingsKey(float mediaBoxWidth, float mediaBoxHeight, int rotation) {}
667664

668-
/**
669-
* Value for storing the dimensions of a rendered image in a map.
670-
*/
671-
private record PdfImageDimensionValue(int width, int height) {
672-
}
665+
/** Value for storing the dimensions of a rendered image in a map. */
666+
private record PdfImageDimensionValue(int width, int height) {}
673667
}

app/common/src/test/java/stirling/software/common/model/ApplicationPropertiesLogicTest.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import stirling.software.common.model.ApplicationProperties.Premium;
1515
import stirling.software.common.model.ApplicationProperties.Security;
1616
import stirling.software.common.model.exception.UnsupportedProviderException;
17-
import stirling.software.common.util.RegexPatternUtils;
1817

1918
class ApplicationPropertiesLogicTest {
2019

@@ -39,15 +38,12 @@ void tempFileManagement_defaults_and_overrides() {
3938
new ApplicationProperties.TempFileManagement();
4039

4140
String expectedBase =
42-
RegexPatternUtils.getInstance()
43-
.getTrailingSlashesPattern()
44-
.matcher(java.lang.System.getProperty("java.io.tmpdir"))
45-
.replaceAll("")
46-
+ "/stirling-pdf";
47-
assertEquals(expectedBase, normalize.apply(tfm.getBaseTmpDir()));
48-
49-
String expectedLibre = expectedBase + "/libreoffice";
50-
assertEquals(expectedLibre, normalize.apply(tfm.getLibreofficeDir()));
41+
Paths.get(java.lang.System.getProperty("java.io.tmpdir"), "stirling-pdf")
42+
.toString();
43+
assertEquals(expectedBase, tfm.getBaseTmpDir());
44+
45+
String expectedLibre = Paths.get(expectedBase, "libreoffice").toString();
46+
assertEquals(expectedLibre, tfm.getLibreofficeDir());
5147

5248
tfm.setBaseTmpDir("/custom/base");
5349
assertEquals("/custom/base", normalize.apply(tfm.getBaseTmpDir()));

app/core/src/main/java/stirling/software/SPDF/UI/impl/LoadingWindow.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ private void checkAndRefreshExplorer() {
227227
if (!existingPids
228228
.contains(
229229
pid)) {
230-
log.debug("Found new explorer.exe with PID: {}", pid);
230+
log.debug(
231+
"Found new explorer.exe with PID: {}",
232+
pid);
231233
ProcessBuilder
232234
killProcess =
233235
new ProcessBuilder(
@@ -245,7 +247,9 @@ private void checkAndRefreshExplorer() {
245247
2,
246248
TimeUnit
247249
.SECONDS);
248-
log.debug("Explorer process terminated: {}", pid);
250+
log.debug(
251+
"Explorer process terminated: {}",
252+
pid);
249253
}
250254
}
251255
}

app/core/src/main/java/stirling/software/SPDF/controller/api/filters/FilterController.java

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ public ResponseEntity<byte[]> pageCount(@ModelAttribute PDFComparisonAndCount re
8787
PDDocument document = pdfDocumentFactory.load(inputFile);
8888
int actualPageCount = document.getNumberOfPages();
8989
// Perform the comparison
90-
boolean valid = switch (comparator) {
91-
case "Greater" -> actualPageCount > pageCount;
92-
case "Equal" -> actualPageCount == pageCount;
93-
case "Less" -> actualPageCount < pageCount;
94-
default ->
95-
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
96-
};
90+
boolean valid =
91+
switch (comparator) {
92+
case "Greater" -> actualPageCount > pageCount;
93+
case "Equal" -> actualPageCount == pageCount;
94+
case "Less" -> actualPageCount < pageCount;
95+
default ->
96+
throw ExceptionUtils.createInvalidArgumentException(
97+
"comparator", comparator);
98+
};
9799

98100
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
99101
return null;
@@ -123,13 +125,15 @@ public ResponseEntity<byte[]> pageSize(@ModelAttribute PageSizeRequest request)
123125
float standardArea = standardSize.getWidth() * standardSize.getHeight();
124126

125127
// Perform the comparison
126-
boolean valid = switch (comparator) {
127-
case "Greater" -> actualArea > standardArea;
128-
case "Equal" -> actualArea == standardArea;
129-
case "Less" -> actualArea < standardArea;
130-
default ->
131-
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
132-
};
128+
boolean valid =
129+
switch (comparator) {
130+
case "Greater" -> actualArea > standardArea;
131+
case "Equal" -> actualArea == standardArea;
132+
case "Less" -> actualArea < standardArea;
133+
default ->
134+
throw ExceptionUtils.createInvalidArgumentException(
135+
"comparator", comparator);
136+
};
133137

134138
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
135139
return null;
@@ -149,13 +153,15 @@ public ResponseEntity<byte[]> fileSize(@ModelAttribute FileSizeRequest request)
149153
long actualFileSize = inputFile.getSize();
150154

151155
// Perform the comparison
152-
boolean valid = switch (comparator) {
153-
case "Greater" -> actualFileSize > fileSize;
154-
case "Equal" -> actualFileSize == fileSize;
155-
case "Less" -> actualFileSize < fileSize;
156-
default ->
157-
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
158-
};
156+
boolean valid =
157+
switch (comparator) {
158+
case "Greater" -> actualFileSize > fileSize;
159+
case "Equal" -> actualFileSize == fileSize;
160+
case "Less" -> actualFileSize < fileSize;
161+
default ->
162+
throw ExceptionUtils.createInvalidArgumentException(
163+
"comparator", comparator);
164+
};
159165

160166
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
161167
return null;
@@ -179,13 +185,15 @@ public ResponseEntity<byte[]> pageRotation(@ModelAttribute PageRotationRequest r
179185
int actualRotation = firstPage.getRotation();
180186

181187
// Perform the comparison
182-
boolean valid = switch (comparator) {
183-
case "Greater" -> actualRotation > rotation;
184-
case "Equal" -> actualRotation == rotation;
185-
case "Less" -> actualRotation < rotation;
186-
default ->
187-
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
188-
};
188+
boolean valid =
189+
switch (comparator) {
190+
case "Greater" -> actualRotation > rotation;
191+
case "Equal" -> actualRotation == rotation;
192+
case "Less" -> actualRotation < rotation;
193+
default ->
194+
throw ExceptionUtils.createInvalidArgumentException(
195+
"comparator", comparator);
196+
};
189197

190198
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
191199
return null;

app/core/src/main/java/stirling/software/SPDF/controller/api/misc/MetadataController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
import stirling.software.SPDF.model.api.misc.MetadataRequest;
2525
import stirling.software.common.service.CustomPDFDocumentFactory;
26+
import stirling.software.common.service.PdfMetadataService;
2627
import stirling.software.common.util.GeneralUtils;
2728
import stirling.software.common.util.RegexPatternUtils;
28-
import stirling.software.common.service.PdfMetadataService;
2929
import stirling.software.common.util.WebResponseUtils;
3030
import stirling.software.common.util.propertyeditor.StringToMapPropertyEditor;
3131

app/core/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,15 @@ private void addTextWatermark(
170170
throws IOException {
171171
String resourceDir = "";
172172
PDFont font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
173-
resourceDir = switch (alphabet) {
174-
case "arabic" -> "static/fonts/NotoSansArabic-Regular.ttf";
175-
case "japanese" -> "static/fonts/Meiryo.ttf";
176-
case "korean" -> "static/fonts/malgun.ttf";
177-
case "chinese" -> "static/fonts/SimSun.ttf";
178-
case "thai" -> "static/fonts/NotoSansThai-Regular.ttf";
179-
default -> "static/fonts/NotoSans-Regular.ttf";
180-
};
173+
resourceDir =
174+
switch (alphabet) {
175+
case "arabic" -> "static/fonts/NotoSansArabic-Regular.ttf";
176+
case "japanese" -> "static/fonts/Meiryo.ttf";
177+
case "korean" -> "static/fonts/malgun.ttf";
178+
case "chinese" -> "static/fonts/SimSun.ttf";
179+
case "thai" -> "static/fonts/NotoSansThai-Regular.ttf";
180+
default -> "static/fonts/NotoSans-Regular.ttf";
181+
};
181182

182183
ClassPathResource classPathResource = new ClassPathResource(resourceDir);
183184
String fileExtension = resourceDir.substring(resourceDir.lastIndexOf("."));

app/core/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ public String getFormatFromExtension(String extension) {
288288
case "eot" -> "embedded-opentype";
289289
case "svg" -> "svg";
290290
default ->
291-
// or throw an exception if an unexpected extension is encountered
292-
"";
291+
// or throw an exception if an unexpected extension is encountered
292+
"";
293293
};
294294
}
295295

app/core/src/main/resources/settings.yml.template

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@ security:
6060
privateKey: classpath:saml-private-key.key # Your private key. Generated from your keypair
6161
spCert: classpath:saml-public-cert.crt # Your signing certificate. Generated from your keypair
6262
jwt: # This feature is currently under development and not yet fully supported. Do not use in production.
63-
persistence: true # Set to 'true' to enable JWT key store
64-
enableKeyRotation: true # Set to 'true' to enable key pair rotation
65-
enableKeyCleanup: true # Set to 'true' to enable key pair cleanup
63+
enabled: false # Set to 'true' to enable JWT key store
64+
keyCleanup: false # Set to 'true' to enable key pair cleanup
6665
keyRetentionDays: 7 # Number of days to retain old keys. The default is 7 days.
67-
secureCookie: false # Set to 'true' to use secure cookies for JWTs
66+
secureCookie: true # Set to 'true' to use secure cookies for JWTs
6867

6968
premium:
7069
key: 00000000-0000-0000-0000-000000000000

app/proprietary/src/main/java/stirling/software/proprietary/security/InitialSecuritySetup.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Optional;
66
import java.util.UUID;
77

8+
import org.springframework.beans.factory.annotation.Value;
89
import org.springframework.stereotype.Component;
910

1011
import jakarta.annotation.PostConstruct;
@@ -26,6 +27,9 @@
2627
@RequiredArgsConstructor
2728
public class InitialSecuritySetup {
2829

30+
@Value("${v2:false}")
31+
private boolean v2Enabled = false;
32+
2933
private final UserService userService;
3034
private final TeamService teamService;
3135
private final ApplicationProperties applicationProperties;
@@ -43,6 +47,7 @@ public void init() {
4347
}
4448
}
4549

50+
configureJWTSettings();
4651
assignUsersToDefaultTeamIfMissing();
4752
initializeInternalApiUser();
4853
} catch (IllegalArgumentException | SQLException | UnsupportedProviderException e) {
@@ -51,6 +56,18 @@ public void init() {
5156
}
5257
}
5358

59+
private void configureJWTSettings() {
60+
ApplicationProperties.Security.Jwt jwtProperties =
61+
applicationProperties.getSecurity().getJwt();
62+
63+
boolean jwtEnabled = jwtProperties.isEnabled();
64+
if (!v2Enabled || !jwtEnabled) {
65+
log.debug("V2 enabled: {}, JWT enabled: {} - disabling all JWT features", v2Enabled, jwtEnabled);
66+
67+
jwtProperties.setKeyCleanup(false);
68+
}
69+
}
70+
5471
private void assignUsersToDefaultTeamIfMissing() {
5572
Team defaultTeam = teamService.getOrCreateDefaultTeam();
5673
Team internalTeam = teamService.getOrCreateInternalTeam();

0 commit comments

Comments
 (0)