|
| 1 | +package stirling.software.proprietary.workflow.util; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import stirling.software.proprietary.security.model.User; |
| 10 | +import stirling.software.proprietary.storage.model.ShareAccessRole; |
| 11 | +import stirling.software.proprietary.storage.model.StoredFile; |
| 12 | +import stirling.software.proprietary.workflow.dto.ParticipantResponse; |
| 13 | +import stirling.software.proprietary.workflow.dto.WorkflowSessionResponse; |
| 14 | +import stirling.software.proprietary.workflow.model.ParticipantStatus; |
| 15 | +import stirling.software.proprietary.workflow.model.WorkflowParticipant; |
| 16 | +import stirling.software.proprietary.workflow.model.WorkflowSession; |
| 17 | +import stirling.software.proprietary.workflow.model.WorkflowType; |
| 18 | + |
| 19 | +/** |
| 20 | + * Regression test for GHSA-qgg6-mxw4-xg62 — verifies that the {@code includeShareToken(s)} flag |
| 21 | + * controls whether {@link WorkflowMapper} discloses participant bearer tokens in responses. |
| 22 | + * Owner-facing endpoints must still receive tokens (so they can distribute share links); |
| 23 | + * participant-facing endpoints must not, so a single participant token cannot be used to enumerate |
| 24 | + * peer bearer tokens. |
| 25 | + */ |
| 26 | +class WorkflowMapperShareTokenTest { |
| 27 | + |
| 28 | + private static final String TOKEN_A = "token-aaaa-1111"; |
| 29 | + private static final String TOKEN_B = "token-bbbb-2222"; |
| 30 | + |
| 31 | + private WorkflowSession buildSessionWithTwoParticipants() { |
| 32 | + User owner = new User(); |
| 33 | + owner.setId(1L); |
| 34 | + owner.setUsername("owner@example.com"); |
| 35 | + |
| 36 | + StoredFile original = new StoredFile(); |
| 37 | + original.setId(42L); |
| 38 | + |
| 39 | + WorkflowSession session = new WorkflowSession(); |
| 40 | + session.setSessionId("session-xyz"); |
| 41 | + session.setOwner(owner); |
| 42 | + session.setOriginalFile(original); |
| 43 | + session.setWorkflowType(WorkflowType.SIGNING); |
| 44 | + session.setDocumentName("contract.pdf"); |
| 45 | + |
| 46 | + WorkflowParticipant a = new WorkflowParticipant(); |
| 47 | + a.setId(10L); |
| 48 | + a.setEmail("alice@example.com"); |
| 49 | + a.setName("Alice"); |
| 50 | + a.setStatus(ParticipantStatus.PENDING); |
| 51 | + a.setShareToken(TOKEN_A); |
| 52 | + a.setAccessRole(ShareAccessRole.EDITOR); |
| 53 | + session.addParticipant(a); |
| 54 | + |
| 55 | + WorkflowParticipant b = new WorkflowParticipant(); |
| 56 | + b.setId(11L); |
| 57 | + b.setEmail("bob@example.com"); |
| 58 | + b.setName("Bob"); |
| 59 | + b.setStatus(ParticipantStatus.PENDING); |
| 60 | + b.setShareToken(TOKEN_B); |
| 61 | + b.setAccessRole(ShareAccessRole.EDITOR); |
| 62 | + session.addParticipant(b); |
| 63 | + |
| 64 | + return session; |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void toResponse_legacyOverload_includesShareTokensForOwnerCompatibility() { |
| 69 | + WorkflowSession session = buildSessionWithTwoParticipants(); |
| 70 | + |
| 71 | + WorkflowSessionResponse response = WorkflowMapper.toResponse(session); |
| 72 | + |
| 73 | + assertNotNull(response); |
| 74 | + assertEquals(2, response.getParticipants().size()); |
| 75 | + assertEquals(TOKEN_A, response.getParticipants().get(0).getShareToken()); |
| 76 | + assertEquals(TOKEN_B, response.getParticipants().get(1).getShareToken()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void toResponse_withIncludeShareTokensFalse_stripsAllPeerTokens() { |
| 81 | + WorkflowSession session = buildSessionWithTwoParticipants(); |
| 82 | + |
| 83 | + WorkflowSessionResponse response = WorkflowMapper.toResponse(session, null, false); |
| 84 | + |
| 85 | + assertNotNull(response); |
| 86 | + assertEquals(2, response.getParticipants().size()); |
| 87 | + for (ParticipantResponse p : response.getParticipants()) { |
| 88 | + assertNull( |
| 89 | + p.getShareToken(), |
| 90 | + "Participant share token must not be exposed in participant-facing responses"); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void toResponse_withIncludeShareTokensFalse_preservesOtherFields() { |
| 96 | + WorkflowSession session = buildSessionWithTwoParticipants(); |
| 97 | + |
| 98 | + WorkflowSessionResponse response = WorkflowMapper.toResponse(session, null, false); |
| 99 | + |
| 100 | + ParticipantResponse alice = response.getParticipants().get(0); |
| 101 | + assertEquals(10L, alice.getId()); |
| 102 | + assertEquals("alice@example.com", alice.getEmail()); |
| 103 | + assertEquals("Alice", alice.getName()); |
| 104 | + assertEquals(ParticipantStatus.PENDING, alice.getStatus()); |
| 105 | + assertEquals(ShareAccessRole.EDITOR, alice.getAccessRole()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void toParticipantResponse_legacyOverload_includesShareToken() { |
| 110 | + WorkflowParticipant p = new WorkflowParticipant(); |
| 111 | + p.setId(1L); |
| 112 | + p.setEmail("a@example.com"); |
| 113 | + p.setStatus(ParticipantStatus.PENDING); |
| 114 | + p.setShareToken(TOKEN_A); |
| 115 | + |
| 116 | + ParticipantResponse response = WorkflowMapper.toParticipantResponse(p); |
| 117 | + |
| 118 | + assertEquals(TOKEN_A, response.getShareToken()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void toParticipantResponse_withIncludeShareTokenFalse_stripsToken() { |
| 123 | + WorkflowParticipant p = new WorkflowParticipant(); |
| 124 | + p.setId(1L); |
| 125 | + p.setEmail("a@example.com"); |
| 126 | + p.setStatus(ParticipantStatus.PENDING); |
| 127 | + p.setShareToken(TOKEN_A); |
| 128 | + |
| 129 | + ParticipantResponse response = WorkflowMapper.toParticipantResponse(p, false); |
| 130 | + |
| 131 | + assertNull(response.getShareToken()); |
| 132 | + assertEquals(1L, response.getId()); |
| 133 | + assertEquals("a@example.com", response.getEmail()); |
| 134 | + assertEquals(ParticipantStatus.PENDING, response.getStatus()); |
| 135 | + } |
| 136 | +} |
0 commit comments