#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
fix(security): restrict sensitive endpoints to authenticated human users (#7306)
Ludy381c325b5334
diff --git a/app/common/src/test/java/stirling/software/common/service/InternalApiClientTest.java b/app/common/src/test/java/stirling/software/common/service/InternalApiClientTest.javaindex 7bab141888..c7c8127fda 100644--- a/app/common/src/test/java/stirling/software/common/service/InternalApiClientTest.java+++ b/app/common/src/test/java/stirling/software/common/service/InternalApiClientTest.java@@ -221,6 +221,13 @@ class InternalApiClientTest {assertThrows(SecurityException.class, () -> client.post("/api/v1/admin/settings", body));}+ @Test+ void postRejectsUserEndpointsIncludingApiKeyEndpoint() {+ MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();++ assertThrows(SecurityException.class, () -> client.post("/api/v1/user/get-api-key", body));+ }+@Testvoid postRejectsAiEndpointsOutsideToolsSubnamespace() {// /api/v1/ai/orchestrate and other non-tool AI endpoints are not internallydiff --git a/app/proprietary/src/main/java/stirling/software/proprietary/controller/api/ProprietaryUIDataController.java b/app/proprietary/src/main/java/stirling/software/proprietary/controller/api/ProprietaryUIDataController.javaindex 2a1823d7c1..fc21f24cd5 100644--- a/app/proprietary/src/main/java/stirling/software/proprietary/controller/api/ProprietaryUIDataController.java+++ b/app/proprietary/src/main/java/stirling/software/proprietary/controller/api/ProprietaryUIDataController.java@@ -422,7 +422,8 @@ public class ProprietaryUIDataController {}@GetMapping("/account")- @PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")+ @PreAuthorize(+ "[email protected](authentication) && !hasAuthority('ROLE_DEMO_USER')")@Operation(summary = "Get account page data")public ResponseEntity<AccountData> getAccountData(Authentication authentication) {if (authentication == null || !authentication.isAuthenticated()) {diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/security/PrincipalPolicy.java b/app/proprietary/src/main/java/stirling/software/proprietary/security/PrincipalPolicy.javanew file mode 100644index 0000000000..35848a9fc5--- /dev/null+++ b/app/proprietary/src/main/java/stirling/software/proprietary/security/PrincipalPolicy.java@@ -0,0 +1,29 @@+package stirling.software.proprietary.security;++import org.springframework.security.core.Authentication;+import org.springframework.stereotype.Component;++import stirling.software.common.model.enumeration.Role;++/** Authorization predicates for endpoints that must only be callable by a real user. */+@Component("principalPolicy")+public class PrincipalPolicy {++ public boolean isInternalApiUser(Authentication authentication) {+ return authentication != null+ && (Role.INTERNAL_API_USER.getRoleId().equals(authentication.getName())+ || authentication.getAuthorities().stream()+ .anyMatch(+ authority ->+ Role.INTERNAL_API_USER+ .getRoleId()+ .equals(authority.getAuthority())));+ }++ public boolean isHumanUser(Authentication authentication) {+ return authentication != null+ && authentication.isAuthenticated()+ && !"anonymousUser".equals(authentication.getName())+ && !isInternalApiUser(authentication);+ }+}diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/security/controller/api/AuthController.java b/app/proprietary/src/main/java/stirling/software/proprietary/security/controller/api/AuthController.javaindex 8887bc38de..f804b8b4a9 100644--- a/app/proprietary/src/main/java/stirling/software/proprietary/security/controller/api/AuthController.java+++ b/app/proprietary/src/main/java/stirling/software/proprietary/security/controller/api/AuthController.java@@ -291,7 +291,8 @@ public class AuthController {* @param response HTTP response* @return Success message*/- @PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")+ @PreAuthorize(+ "[email protected](authentication) && !hasAuthority('ROLE_DEMO_USER')")@PostMapping("/logout")public ResponseEntity<?> logout(HttpServletRequest request, HttpServletResponse response) {try {@@ -317,7 +318,8 @@ public class AuthController {* @param response HTTP response to set new JWT cookie* @return New token information*/- @PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")+ @PreAuthorize(+ "[email protected](authentication) && !hasAuthority('ROLE_DEMO_USER')")@PostMapping("/refresh")public ResponseEntity<?> refresh(HttpServletRequest request, HttpServletResponse response) {try {@@ -421,7 +423,7 @@ public class AuthController {}}- @PreAuthorize("isAuthenticated() && !hasAuthority('ROLE_DEMO_USER')")+ @PreAuthorize("@principalPolicy.isHumanUser(authentication) && !hasAuthority('ROLE_DEMO_USER')")@GetMapping("/mfa/setup")public ResponseEntity<?> setupMfa(Authentication authentication) {if (authentication == null || !authentication.isAuthenticated()) {@@ -457,7 +459,7 @@ public class AuthController {}}- @PreAuthorize("isAuthenticated() && !hasAuthority('ROLE_DEMO_USER')")+ @PreAuthorize("@principalPolicy.isHumanUser(authentication) && !hasAuthority('ROLE_DEMO_USER')")@PostMapping("/mfa/enable")public ResponseEntity<?> enableMfa(@RequestBody MfaCodeRequest request, Authentication authentication) {@@ -509,7 +511,7 @@ public class AuthController {}}- @PreAuthorize("isAuthenticated() && !hasAuthority('ROLE_DEMO_USER')")+ @PreAuthorize("@principalPolicy.isHumanUser(authentication) && !hasAuthority('ROLE_DEMO_USER')")@PostMapping("/mfa/disable")public ResponseEntity<?> disableMfa(@RequestBody MfaCodeRequest request, Authentication authentication) {@@ -564,7 +566,7 @@ public class AuthController {}}- @PreAuthorize("isAuthenticated() && !hasAuthority('ROLE_DEMO_USER')")+ @PreAuthorize("@principalPolicy.isHumanUser(authentication) && !hasAuthority('ROLE_DEMO_USER')")@PostMapping("/mfa/setup/cancel")public ResponseEntity<?> cancelMfaSetup(Authentication authentication) {if (authentication == null || !authentication.isAuthenticated()) {diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/security/controller/api/UserController.java b/app/proprietary/src/main/java/stirling/software/proprietary/security/controller/api/UserController.javaindex fee62dadd4..10dde83245 100644--- a/app/proprietary/src/main/java/stirling/software/proprietary/security/controller/api/UserController.java+++ b/app/proprietary/src/main/java/stirling/software/proprietary/security/controller/api/UserController.java@@ -170,7 +170,7 @@ public class UserController {return userMap;}- @PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")+ @PreAuthorize("@principalPolicy.isHumanUser(authentication) && !hasAuthority('ROLE_DEMO_USER')")@PostMapping("/change-username")@Audited(type = AuditEventType.USER_PROFILE_UPDATE, level = AuditLevel.BASIC)public ResponseEntity<?> changeUsername(@@ -230,7 +230,7 @@ public class UserController {"Username changed successfully. Please log in again."));}- @PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")+ @PreAuthorize("@principalPolicy.isHumanUser(authentication) && !hasAuthority('ROLE_DEMO_USER')")@PostMapping("/change-password-on-login")@Audited(type = AuditEventType.USER_PROFILE_UPDATE, level = AuditLevel.BASIC)public ResponseEntity<?> changePasswordOnLogin(@@ -306,7 +306,7 @@ public class UserController {"Password changed successfully. Please log in again."));}- @PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")+ @PreAuthorize("@principalPolicy.isHumanUser(authentication) && !hasAuthority('ROLE_DEMO_USER')")@PostMapping("/change-password")@Audited(type = AuditEventType.USER_PROFILE_UPDATE, level = AuditLevel.BASIC)public ResponseEntity<?> changePassword(@@ -341,7 +341,7 @@ public class UserController {"Password changed successfully. Please log in again."));}- @PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")+ @PreAuthorize("@principalPolicy.isHumanUser(authentication) && !hasAuthority('ROLE_DEMO_USER')")@PostMapping("/updateUserSettings")/*** Updates the user settings based on the provided JSON payload.@@ -849,7 +849,7 @@ public class UserController {public record OwnershipTransfer(Long userId) {}- @PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")+ @PreAuthorize("@principalPolicy.isHumanUser(authentication) && !hasAuthority('ROLE_DEMO_USER')")@PostMapping("/get-api-key")public ResponseEntity<Map<String, String>> getApiKey(Principal principal) {if (principal == null) {@@ -865,7 +865,7 @@ public class UserController {return ResponseEntity.ok(Map.of("apiKey", apiKey));}- @PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")+ @PreAuthorize("@principalPolicy.isHumanUser(authentication) && !hasAuthority('ROLE_DEMO_USER')")@PostMapping("/update-api-key")public ResponseEntity<Map<String, String>> updateApiKey(Principal principal) {if (principal == null) {@@ -988,6 +988,7 @@ public class UserController {}}+ @PreAuthorize("@principalPolicy.isHumanUser(authentication)")@PostMapping("/complete-initial-setup")public ResponseEntity<?> completeInitialSetup() {try {@@ -1016,6 +1017,7 @@ public class UserController {}// Lists enabled users for the signing picker; 'org' scope = instance-wide, else caller's team.+ @PreAuthorize("@principalPolicy.isHumanUser(authentication)")@GetMapping("/users")public ResponseEntity<List<UserSummaryDTO>> listUsers(Principal principal) {if (principal == null) {diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/workflow/controller/SigningSessionController.java b/app/proprietary/src/main/java/stirling/software/proprietary/workflow/controller/SigningSessionController.javaindex b224a09841..00044e9055 100644--- a/app/proprietary/src/main/java/stirling/software/proprietary/workflow/controller/SigningSessionController.java+++ b/app/proprietary/src/main/java/stirling/software/proprietary/workflow/controller/SigningSessionController.java@@ -7,6 +7,7 @@ import java.util.List;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;+import org.springframework.security.access.prepost.PreAuthorize;import org.springframework.transaction.annotation.Transactional;import org.springframework.web.bind.annotation.DeleteMapping;import org.springframework.web.bind.annotation.GetMapping;@@ -48,6 +49,7 @@ import tools.jackson.databind.ObjectMapper;@Slf4j@RestController@RequestMapping("/api/v1/security")+@PreAuthorize("@principalPolicy.isHumanUser(authentication)")@Tag(name = "Signing Sessions",description = "Signing session lifecycle and participant management")diff --git a/app/proprietary/src/test/java/stirling/software/proprietary/security/PrincipalPolicyTest.java b/app/proprietary/src/test/java/stirling/software/proprietary/security/PrincipalPolicyTest.javanew file mode 100644index 0000000000..2fe9d68db0--- /dev/null+++ b/app/proprietary/src/test/java/stirling/software/proprietary/security/PrincipalPolicyTest.java@@ -0,0 +1,95 @@+package stirling.software.proprietary.security;++import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertTrue;++import java.util.List;++import org.junit.jupiter.api.Test;+import org.springframework.security.authentication.AnonymousAuthenticationToken;+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;+import org.springframework.security.core.Authentication;+import org.springframework.security.core.authority.SimpleGrantedAuthority;++import stirling.software.common.model.enumeration.Role;++class PrincipalPolicyTest {++ private final PrincipalPolicy policy = new PrincipalPolicy();++ @Test+ void rejectsNullAuthentication() {+ assertFalse(policy.isHumanUser(null));+ }++ @Test+ void doesNotTreatMissingAuthenticationAsInternal() {+ assertFalse(policy.isInternalApiUser(null));+ }++ @Test+ void rejectsUnauthenticatedAuthentication() {+ Authentication authentication =+ new UsernamePasswordAuthenticationToken("peter", "password");++ assertFalse(policy.isHumanUser(authentication));+ }++ @Test+ void rejectsAnonymousAuthentication() {+ Authentication authentication =+ new AnonymousAuthenticationToken(+ "test-key",+ "anonymousUser",+ List.of(new SimpleGrantedAuthority("ROLE_ANONYMOUS")));++ assertFalse(policy.isHumanUser(authentication));+ }++ @Test+ void acceptsAuthenticatedHumanUser() {+ assertTrue(policy.isHumanUser(authenticated("peter")));+ }++ @Test+ void acceptsAuthenticatedAdminUser() {+ Authentication authentication =+ authenticated("admin", new SimpleGrantedAuthority(Role.ADMIN.getRoleId()));++ assertTrue(policy.isHumanUser(authentication));+ }++ @Test+ void rejectsInternalApiAuthorityEvenWithHumanUsername() {+ Authentication authentication =+ authenticated(+ "peter", new SimpleGrantedAuthority(Role.INTERNAL_API_USER.getRoleId()));++ assertTrue(policy.isInternalApiUser(authentication));+ assertFalse(policy.isHumanUser(authentication));+ }++ @Test+ void rejectsInternalApiUsernameEvenWithoutAuthority() {+ Authentication authentication = authenticated(Role.INTERNAL_API_USER.getRoleId());++ assertTrue(policy.isInternalApiUser(authentication));+ assertFalse(policy.isHumanUser(authentication));+ }++ @Test+ void rejectsInternalAuthorityWhenCombinedWithOtherAuthorities() {+ Authentication authentication =+ authenticated(+ "peter",+ new SimpleGrantedAuthority(Role.USER.getRoleId()),+ new SimpleGrantedAuthority(Role.INTERNAL_API_USER.getRoleId()));++ assertFalse(policy.isHumanUser(authentication));+ }++ private static Authentication authenticated(+ String username, SimpleGrantedAuthority... authorities) {+ return new UsernamePasswordAuthenticationToken(username, "password", List.of(authorities));+ }+}