|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 LabKey Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.labkey.targetedms; |
| 17 | + |
| 18 | +import jakarta.servlet.http.HttpServletResponse; |
| 19 | +import org.apache.logging.log4j.Logger; |
| 20 | +import org.junit.After; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | +import org.labkey.api.data.Container; |
| 24 | +import org.labkey.api.data.SimpleFilter; |
| 25 | +import org.labkey.api.data.Table; |
| 26 | +import org.labkey.api.data.TableSelector; |
| 27 | +import org.labkey.api.exp.api.ExpRun; |
| 28 | +import org.labkey.api.exp.api.ExperimentService; |
| 29 | +import org.labkey.api.pipeline.PipeRoot; |
| 30 | +import org.labkey.api.pipeline.PipelineService; |
| 31 | +import org.labkey.api.query.FieldKey; |
| 32 | +import org.labkey.api.security.permissions.AbstractContainerScopingTest; |
| 33 | +import org.labkey.api.util.GUID; |
| 34 | +import org.labkey.api.util.logging.LogHelper; |
| 35 | +import org.labkey.api.view.ActionURL; |
| 36 | +import org.labkey.api.view.ViewBackgroundInfo; |
| 37 | +import org.labkey.targetedms.parser.skyaudit.UnitTestUtil; |
| 38 | + |
| 39 | +import java.io.File; |
| 40 | +import java.util.Collections; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Set; |
| 43 | + |
| 44 | +/** |
| 45 | + * Container-scoping integration tests for {@link TargetedMSController} actions that resolve an object by a |
| 46 | + * global id. Each test sets up the same object in one folder and confirms the action rejects a request that addresses |
| 47 | + * via the wrong container |
| 48 | + */ |
| 49 | +public class TargetedMSContainerScopingTest extends AbstractContainerScopingTest |
| 50 | +{ |
| 51 | + private static final Logger LOG = LogHelper.getLogger(TargetedMSContainerScopingTest.class, "TargetedMS container scoping tests"); |
| 52 | + private static final GUID DOC_GUID = new GUID("8f1c2a44-3c5e-4f0a-9d2b-6e7a1b3c5d9e"); |
| 53 | + |
| 54 | + @Before |
| 55 | + @After |
| 56 | + public void cleanupAuditLog() |
| 57 | + { |
| 58 | + UnitTestUtil.cleanupDatabase(DOC_GUID); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * RemoveLinkVersionAction relinks a Skyline document-version chain by run rowId. It must reject a run that lives in |
| 63 | + * a different container (mirroring its sibling SaveLinkVersionsAction), otherwise an editor in one folder could |
| 64 | + * rewrite the version chain of documents they can't see. |
| 65 | + */ |
| 66 | + @Test |
| 67 | + public void removeLinkVersionIsContainerScoped() throws Exception |
| 68 | + { |
| 69 | + Container owner = createContainer("LinkOwner"); |
| 70 | + Container other = createContainer("LinkOther"); |
| 71 | + |
| 72 | + // Negative: a run that lives in 'owner' cannot be relinked through the 'other' folder |
| 73 | + ExpRun foreignRun = createRun(owner, "foreign-version"); |
| 74 | + ActionURL foreign = new ActionURL(TargetedMSController.RemoveLinkVersionAction.class, other) |
| 75 | + .addParameter("rowId", foreignRun.getRowId()); |
| 76 | + assertStatus(HttpServletResponse.SC_BAD_REQUEST, post(foreign, getAdmin())); |
| 77 | + |
| 78 | + // Positive: a run that lives in the acting folder is accepted |
| 79 | + ExpRun localRun = createRun(other, "local-version"); |
| 80 | + ActionURL local = new ActionURL(TargetedMSController.RemoveLinkVersionAction.class, other) |
| 81 | + .addParameter("rowId", localRun.getRowId()); |
| 82 | + assertStatus(HttpServletResponse.SC_OK, post(local, getAdmin())); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * ShowSkylineAuditLogExtraInfoAJAXAction renders the full Skyline audit detail (user, timestamp, extra info) for an |
| 87 | + * audit entry resolved by global EntryId. It must only render entries whose owning run is in the requested folder, |
| 88 | + * otherwise any reader could read another folder's document audit history by guessing entry ids. |
| 89 | + */ |
| 90 | + @Test |
| 91 | + public void auditLogExtraInfoIsContainerScoped() throws Exception |
| 92 | + { |
| 93 | + Container owner = createContainer("AuditOwner"); |
| 94 | + Container other = createContainer("AuditOther"); |
| 95 | + |
| 96 | + int entryId = importAuditLogEntry(owner); |
| 97 | + |
| 98 | + // Negative: the entry's run lives in 'owner', so requesting it via 'other' must 404 rather than leak detail |
| 99 | + ActionURL foreign = new ActionURL(TargetedMSController.ShowSkylineAuditLogExtraInfoAJAXAction.class, other) |
| 100 | + .addParameter("entryId", entryId); |
| 101 | + assertStatus(HttpServletResponse.SC_NOT_FOUND, get(foreign, getAdmin())); |
| 102 | + |
| 103 | + // Positive: requesting the entry through its owning folder renders normally |
| 104 | + ActionURL owned = new ActionURL(TargetedMSController.ShowSkylineAuditLogExtraInfoAJAXAction.class, owner) |
| 105 | + .addParameter("entryId", entryId); |
| 106 | + assertStatus(HttpServletResponse.SC_OK, get(owned, getAdmin())); |
| 107 | + } |
| 108 | + |
| 109 | + /** Persist a minimal, saved {@link ExpRun} in the given container so the controller can resolve it by rowId. */ |
| 110 | + private ExpRun createRun(Container c, String name) throws Exception |
| 111 | + { |
| 112 | + ExperimentService svc = ExperimentService.get(); |
| 113 | + ExpRun run = svc.createExperimentRun(c, name); |
| 114 | + PipeRoot pipeRoot = PipelineService.get().findPipelineRoot(c); |
| 115 | + assertNotNull("No pipeline root for " + c.getPath(), pipeRoot); |
| 116 | + run.setFilePathRoot(pipeRoot.getRootPath()); |
| 117 | + run.setProtocol(svc.ensureSampleDerivationProtocol(getAdmin())); |
| 118 | + ViewBackgroundInfo info = new ViewBackgroundInfo(c, getAdmin(), null); |
| 119 | + return svc.saveSimpleExperimentRun(run, Collections.emptyMap(), Collections.emptyMap(), |
| 120 | + Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), info, null, false); |
| 121 | + } |
| 122 | + |
| 123 | + /** Insert a TargetedMS run in the given container, import a sample Skyline audit log for it, and return an EntryId. */ |
| 124 | + private int importAuditLogEntry(Container c) throws Exception |
| 125 | + { |
| 126 | + TargetedMSRun run = new TargetedMSRun(); |
| 127 | + run.setContainer(c); |
| 128 | + run.setDocumentGUID(DOC_GUID); |
| 129 | + Table.insert(getAdmin(), TargetedMSManager.getTableInfoRuns(), run); |
| 130 | + |
| 131 | + File zip = UnitTestUtil.getSampleDataFile("AuditLogFiles/MethodEdit_v1.zip"); |
| 132 | + File logFile = UnitTestUtil.extractLogFromZip(zip, LOG); |
| 133 | + new SkylineAuditLogManager(c, null).importAuditLogFile(getAdmin(), logFile, DOC_GUID, run); |
| 134 | + |
| 135 | + List<Integer> entryIds = new TableSelector( |
| 136 | + TargetedMSManager.getTableInfoSkylineRunAuditLogEntry(), |
| 137 | + Collections.singleton("AuditLogEntryId"), |
| 138 | + new SimpleFilter(FieldKey.fromParts("VersionId"), run.getId()), null) |
| 139 | + .getArrayList(Integer.class); |
| 140 | + assertFalse("No audit log entries were imported", entryIds.isEmpty()); |
| 141 | + return entryIds.get(0); |
| 142 | + } |
| 143 | +} |
0 commit comments