Skip to content

Commit a43e933

Browse files
committed
XWIKI-22424: Improve attachment filtering
1 parent 2a4f19a commit a43e933

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/main/java/org/xwiki/rest/internal/resources/BaseAttachmentsResource.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import org.xwiki.rest.internal.Utils;
5757
import org.xwiki.rest.model.jaxb.Attachment;
5858
import org.xwiki.rest.model.jaxb.Attachments;
59+
import org.xwiki.security.authorization.ContextualAuthorizationManager;
60+
import org.xwiki.security.authorization.Right;
5961
import org.xwiki.user.UserReferenceResolver;
6062

6163
import com.xpn.xwiki.XWiki;
@@ -113,6 +115,9 @@ public boolean isAlreadyExisting()
113115
FILTER_TO_QUERY.put("author", "attachment.author");
114116
}
115117

118+
@Inject
119+
protected ContextualAuthorizationManager authorization;
120+
116121
@Inject
117122
private ModelFactory modelFactory;
118123

@@ -157,15 +162,19 @@ protected Attachments getAttachments(EntityReference scope, Map<String, String>
157162

158163
List<Object> queryResults = getAttachmentsQuery(scope, filters).setLimit(limit).setOffset(offset).execute();
159164
attachments.withAttachments(queryResults.stream().map(this::processAttachmentsQueryResult)
165+
// Apply passed filters
160166
.filter(getFileTypeFilter(filters.getOrDefault(FILTER_FILE_TYPES, "")))
167+
// Filter out attachments the current user is not allowed to see
168+
.filter(a -> authorization.hasAccess(Right.VIEW, a.getReference()))
169+
// Convert XWikiAttachment to REST Attachment
161170
.map(xwikiAttachment -> toRestAttachment(xwikiAttachment, withPrettyNames))
162171
.toList());
163172
} catch (QueryException e) {
164173
throw new XWikiRestException(e);
165174
} finally {
166175
xcontext.setWikiId(database);
167176
}
168-
177+
169178
return attachments;
170179
}
171180

xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/main/java/org/xwiki/rest/internal/resources/attachments/AttachmentsResourceImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import org.xwiki.rest.model.jaxb.Attachments;
5050
import org.xwiki.rest.resources.attachments.AttachmentResource;
5151
import org.xwiki.rest.resources.attachments.AttachmentsResource;
52-
import org.xwiki.security.authorization.ContextualAuthorizationManager;
5352
import org.xwiki.security.authorization.Right;
5453

5554
import com.xpn.xwiki.XWikiException;
@@ -64,9 +63,6 @@ public class AttachmentsResourceImpl extends BaseAttachmentsResource implements
6463
{
6564
private static final String NAME = "name";
6665

67-
@Inject
68-
private ContextualAuthorizationManager authorization;
69-
7066
@Inject
7167
private Container container;
7268

xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/test/java/org/xwiki/rest/internal/resources/attachments/AttachmentsResourceImplTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import static org.junit.jupiter.api.Assertions.assertThrows;
6767
import static org.mockito.ArgumentMatchers.any;
6868
import static org.mockito.ArgumentMatchers.eq;
69+
import static org.mockito.ArgumentMatchers.same;
6970
import static org.mockito.Mockito.doThrow;
7071
import static org.mockito.Mockito.mock;
7172
import static org.mockito.Mockito.times;
@@ -157,6 +158,8 @@ void getAttachments() throws Exception
157158
new Object[] {"Path.To", "Page", "1.3", imageAttachment});
158159
when(query.execute()).thenReturn(results);
159160

161+
when(this.authorization.hasAccess(same(Right.VIEW), any())).thenReturn(true);
162+
160163
DocumentReference documentReference = new DocumentReference("test", Arrays.asList("Path", "To"), "Page");
161164
when(this.defaultSpaceReferenceResover.resolve(eq("Path.To"), any()))
162165
.thenReturn(documentReference.getLastSpaceReference());

xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/test/java/org/xwiki/rest/internal/resources/spaces/SpaceAttachmentsResourceImplTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@
1919
*/
2020
package org.xwiki.rest.internal.resources.spaces;
2121

22+
import java.util.Arrays;
2223
import java.util.Collections;
2324
import java.util.List;
2425

2526
import org.junit.jupiter.api.BeforeEach;
2627
import org.junit.jupiter.api.Test;
28+
import org.xwiki.model.reference.AttachmentReference;
2729
import org.xwiki.model.reference.SpaceReference;
2830
import org.xwiki.query.Query;
2931
import org.xwiki.rest.internal.resources.AbstractAttachmentsResourceTest;
3032
import org.xwiki.rest.model.jaxb.Attachment;
3133
import org.xwiki.rest.model.jaxb.Attachments;
34+
import org.xwiki.security.authorization.ContextualAuthorizationManager;
35+
import org.xwiki.security.authorization.Right;
3236
import org.xwiki.test.junit5.mockito.InjectMockComponents;
37+
import org.xwiki.test.junit5.mockito.MockComponent;
3338

3439
import com.xpn.xwiki.doc.XWikiAttachment;
3540
import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
@@ -52,6 +57,9 @@ class SpaceAttachmentsResourceImplTest extends AbstractAttachmentsResourceTest
5257
@InjectMockComponents
5358
private SpaceAttachmentsResourceImpl spaceAttachmentsResource;
5459

60+
@MockComponent
61+
private ContextualAuthorizationManager authorization;
62+
5563
@BeforeEach
5664
@Override
5765
public void setUp() throws Exception
@@ -74,7 +82,17 @@ void getAttachments() throws Exception
7482
when(query.setLimit(5)).thenReturn(query);
7583

7684
XWikiAttachment xwikiAttachment = mock(XWikiAttachment.class);
77-
List<Object> results = Collections.singletonList(new Object[] {"Path.To", "Page", "1.3", xwikiAttachment});
85+
AttachmentReference xwikiAttachmentReference = mock(AttachmentReference.class, "image");
86+
when(xwikiAttachment.getReference()).thenReturn(xwikiAttachmentReference);
87+
when(this.authorization.hasAccess(Right.VIEW, xwikiAttachmentReference)).thenReturn(true);
88+
89+
XWikiAttachment forbiddenAttachment = mock(XWikiAttachment.class);
90+
AttachmentReference forbiddenAttachmentReference = mock(AttachmentReference.class, "forbidden");
91+
when(forbiddenAttachment.getReference()).thenReturn(forbiddenAttachmentReference);
92+
when(this.authorization.hasAccess(Right.VIEW, forbiddenAttachmentReference)).thenReturn(false);
93+
94+
List<Object> results = Arrays.asList(new Object[] {"Path.To", "Page", "1.3", xwikiAttachment},
95+
new Object[] {"Path.To", "ForbiddenPage", "1.3", forbiddenAttachment});
7896
when(query.execute()).thenReturn(results);
7997

8098
SpaceReference spaceReference = new SpaceReference("test", "Path", "To");
@@ -89,6 +107,8 @@ void getAttachments() throws Exception
89107
this.spaceAttachmentsResource.getAttachments("test", "Path/spaces/To", "", "xyz", "", "", 10, 5, false);
90108

91109
verify(query).bindValue("localSpaceReference", "Path.To");
110+
verify(this.authorization).hasAccess(Right.VIEW, xwikiAttachmentReference);
111+
verify(this.authorization).hasAccess(Right.VIEW, forbiddenAttachmentReference);
92112

93113
assertEquals(Collections.singletonList(attachment), attachments.getAttachments());
94114
}

xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/test/java/org/xwiki/rest/internal/resources/wikis/WikiAttachmentsResourceImplTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@
1919
*/
2020
package org.xwiki.rest.internal.resources.wikis;
2121

22+
import java.util.Arrays;
2223
import java.util.Collections;
2324
import java.util.List;
2425

2526
import org.junit.jupiter.api.BeforeEach;
2627
import org.junit.jupiter.api.Test;
28+
import org.xwiki.model.reference.AttachmentReference;
2729
import org.xwiki.model.reference.SpaceReference;
2830
import org.xwiki.query.Query;
2931
import org.xwiki.rest.internal.resources.AbstractAttachmentsResourceTest;
3032
import org.xwiki.rest.model.jaxb.Attachment;
3133
import org.xwiki.rest.model.jaxb.Attachments;
34+
import org.xwiki.security.authorization.ContextualAuthorizationManager;
35+
import org.xwiki.security.authorization.Right;
3236
import org.xwiki.test.junit5.mockito.InjectMockComponents;
37+
import org.xwiki.test.junit5.mockito.MockComponent;
3338

3439
import com.xpn.xwiki.doc.XWikiAttachment;
3540
import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
@@ -38,6 +43,7 @@
3843
import static org.mockito.ArgumentMatchers.any;
3944
import static org.mockito.ArgumentMatchers.eq;
4045
import static org.mockito.Mockito.mock;
46+
import static org.mockito.Mockito.verify;
4147
import static org.mockito.Mockito.when;
4248

4349
/**
@@ -51,6 +57,9 @@ class WikiAttachmentsResourceImplTest extends AbstractAttachmentsResourceTest
5157
@InjectMockComponents
5258
private WikiAttachmentsResourceImpl wikiAttachmentsResource;
5359

60+
@MockComponent
61+
private ContextualAuthorizationManager authorization;
62+
5463
@BeforeEach
5564
@Override
5665
public void setUp() throws Exception
@@ -71,7 +80,17 @@ void getAttachments() throws Exception
7180
when(query.setLimit(10)).thenReturn(query);
7281

7382
XWikiAttachment xwikiAttachment = mock(XWikiAttachment.class);
74-
List<Object> results = Collections.singletonList(new Object[] {"Path.To", "Page", "1.3", xwikiAttachment});
83+
AttachmentReference xwikiAttachmentReference = mock(AttachmentReference.class, "image");
84+
when(xwikiAttachment.getReference()).thenReturn(xwikiAttachmentReference);
85+
when(this.authorization.hasAccess(Right.VIEW, xwikiAttachmentReference)).thenReturn(true);
86+
87+
XWikiAttachment forbiddenAttachment = mock(XWikiAttachment.class);
88+
AttachmentReference forbiddenAttachmentReference = mock(AttachmentReference.class, "forbidden");
89+
when(forbiddenAttachment.getReference()).thenReturn(forbiddenAttachmentReference);
90+
when(this.authorization.hasAccess(Right.VIEW, forbiddenAttachmentReference)).thenReturn(false);
91+
92+
List<Object> results = Arrays.asList(new Object[] {"Path.To", "Page", "1.3", xwikiAttachment},
93+
new Object[] {"Path.To", "ForbiddenPage", "1.3", forbiddenAttachment});
7594
when(query.execute()).thenReturn(results);
7695

7796
when(this.defaultSpaceReferenceResover.resolve(eq("Path.To"), any()))
@@ -84,6 +103,9 @@ void getAttachments() throws Exception
84103
Attachments attachments =
85104
this.wikiAttachmentsResource.getAttachments("test", "", "", "abc", "", "", 0, 10, true);
86105

106+
verify(this.authorization).hasAccess(Right.VIEW, xwikiAttachmentReference);
107+
verify(this.authorization).hasAccess(Right.VIEW, forbiddenAttachmentReference);
108+
87109
assertEquals(Collections.singletonList(attachment), attachments.getAttachments());
88110
}
89111
}

0 commit comments

Comments
 (0)