Skip to content

Commit ae37aee

Browse files
Improve file path validation (#181)
1 parent aa8072d commit ae37aee

File tree

4 files changed

+23
-47
lines changed

4 files changed

+23
-47
lines changed

genotyping/src/org/labkey/genotyping/HaplotypeAssayProvider.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.labkey.api.assay.AssayUrls;
4747
import org.labkey.api.study.assay.ParticipantVisitResolverType;
4848
import org.labkey.api.util.FileType;
49+
import org.labkey.api.util.HtmlString;
4950
import org.labkey.api.util.PageFlowUtil;
5051
import org.labkey.api.util.Pair;
5152
import org.labkey.api.view.ActionURL;
@@ -135,7 +136,7 @@ public String getName()
135136
@Override
136137
public HttpView<?> getDataDescriptionView(AssayRunUploadForm form)
137138
{
138-
return new HtmlView("");
139+
return new HtmlView(HtmlString.EMPTY_STRING);
139140
}
140141

141142
@Override
@@ -205,26 +206,18 @@ protected Map<String, Set<String>> getRequiredDomainProperties()
205206
{
206207
Map<String, Set<String>> domainMap = super.getRequiredDomainProperties();
207208

208-
Set<String> runProperties = domainMap.get(ExpProtocol.ASSAY_DOMAIN_RUN);
209-
if (runProperties == null)
210-
{
211-
runProperties = new HashSet<>();
212-
domainMap.put(ExpProtocol.ASSAY_DOMAIN_RUN, runProperties);
213-
}
209+
Set<String> runProperties = domainMap.computeIfAbsent(ExpProtocol.ASSAY_DOMAIN_RUN, k -> new HashSet<>());
214210
runProperties.add(ENABLED_PROPERTY_NAME);
215-
for (String propName : getColumnMappingProperties(true).keySet())
216-
{
217-
runProperties.add(propName);
218-
}
211+
runProperties.addAll(getColumnMappingProperties(true).keySet());
219212
runProperties.add(SPECIES_COLUMN.getName());
220213

221214
return domainMap;
222215
}
223216

224217
@Override
225-
public List<AssayDataCollector> getDataCollectors(@Nullable Map<String, File> uploadedFiles, AssayRunUploadForm context)
218+
public List<AssayDataCollector> getDataCollectors(@Nullable Map<String, org.labkey.vfs.FileLike> uploadedFiles, AssayRunUploadForm context)
226219
{
227-
return Collections.singletonList(new HaplotypeDataCollector());
220+
return Collections.singletonList(new HaplotypeDataCollector<>());
228221
}
229222

230223
@Override

genotyping/src/org/labkey/genotyping/HaplotypeDataCollector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class HaplotypeDataCollector<ContextType extends AssayRunUploadContext<Ha
4343
private Map<String, String> _reshowMap;
4444

4545
@Override
46-
public HttpView getView(ContextType context)
46+
public HttpView<?> getView(ContextType context)
4747
{
4848
// if reshowing on error, get the data param out of the context for the JSP to use
4949
HttpServletRequest request = context.getRequest();

genotyping/src/org/labkey/genotyping/HaplotypeDataHandler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.labkey.api.assay.AssayService;
4848
import org.labkey.api.view.ActionURL;
4949
import org.labkey.api.view.ViewBackgroundInfo;
50+
import org.labkey.vfs.FileLike;
5051

5152
import java.io.File;
5253
import java.io.IOException;
@@ -70,17 +71,17 @@ public DataType getDataType()
7071
}
7172

7273
@Override
73-
public void importFile(@NotNull ExpData data, File dataFile, @NotNull ViewBackgroundInfo info, @NotNull Logger log, @NotNull XarContext context) throws ExperimentException
74+
public void importFile(@NotNull ExpData data, @NotNull FileLike dataFile, @NotNull ViewBackgroundInfo info, @NotNull Logger log, @NotNull XarContext context) throws ExperimentException
7475
{
7576
if (!dataFile.exists())
7677
{
77-
log.warn("Could not find file " + dataFile.getAbsolutePath() + " on disk for data with LSID " + data.getLSID());
78+
log.warn("Could not find file " + dataFile + " on disk for data with LSID " + data.getLSID());
7879
return;
7980
}
8081
ExpRun expRun = data.getRun();
8182
if (expRun == null)
8283
{
83-
throw new ExperimentException("Could not load haplotype file " + dataFile.getAbsolutePath() + " because it is not owned by an experiment run");
84+
throw new ExperimentException("Could not load haplotype file " + dataFile + " because it is not owned by an experiment run");
8485
}
8586

8687
try
@@ -100,7 +101,7 @@ public void importFile(@NotNull ExpData data, File dataFile, @NotNull ViewBackgr
100101
Map<String, String> animalIds = new CaseInsensitiveTreeMap<>();
101102
List<HaplotypeIdentifier> haplotypes = new ArrayList<>();
102103
List<HaplotypeAssignmentDataRow> dataRows = new ArrayList<>();
103-
TabLoader tabLoader = new TabLoader(dataFile, true);
104+
TabLoader tabLoader = new TabLoader(dataFile.openInputStream(), true, data.getContainer());
104105
List<Map<String, Object>> rowsMap = tabLoader.load();
105106
parseHaplotypeData(protocol, rowsMap, runPropertyValues, animalIds, haplotypes, dataRows);
106107

genotyping/src/org/labkey/genotyping/IlluminaFastqParser.java

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ else if (reader.getLineNumber() == 1 && totalReads == 0 && !f.getName().contains
214214
sampleId = _sampleNameToIdMap.get(sampleName);
215215
}
216216
String name = (_outputPrefix == null ? "Reads" : _outputPrefix) + "-R" + pairNumber + "-" + (sampleIdx == 0 ? "Control" : sampleId) + ".fastq.gz";
217-
File newFile = new File(targetDir, name);
217+
File newFile = FileUtil.appendName(targetDir, name);
218218

219219
if (!f.equals(newFile))
220220
{
@@ -408,9 +408,7 @@ public void testHeaders() throws PipelineJobException, IOException
408408
{
409409
Module module = ModuleLoader.getInstance().getModule("genotyping");
410410
File newHeaderPath = JunitUtil.getSampleData(module, "genotyping/illumina_newHeader");
411-
String newHeaderSampledataLoc = newHeaderPath.toString();
412411
File oldHeaderPath = JunitUtil.getSampleData(module, "genotyping");
413-
String oldHeaderSampledataLoc = oldHeaderPath.toString();
414412
final List<String> filenamesOldHeader = Arrays.asList(
415413
"IlluminaSamples-R1-4892.fastq.gz",
416414
"IlluminaSamples-R1-4893.fastq.gz",
@@ -472,8 +470,7 @@ public void testHeaders() throws PipelineJobException, IOException
472470
"IlluminaSamplesNewHeader-R2-4905.fastq.gz"
473471
);
474472

475-
final Pair[] pairs =
476-
{
473+
final var pairs = List.of(
477474
new Pair<>(4892, 1),
478475
new Pair<>(4893, 1),
479476
new Pair<>(4894, 1),
@@ -501,11 +498,10 @@ public void testHeaders() throws PipelineJobException, IOException
501498
new Pair<>(4902, 2),
502499
new Pair<>(4903, 2),
503500
new Pair<>(4904, 2),
504-
new Pair<>(4905, 2)
505-
};
501+
new Pair<>(4905, 2));
506502

507503
int i = 0;
508-
int numOfPairs = pairs.length;
504+
int numOfPairs = pairs.size();
509505
Set<Pair<Integer, Integer>> expectedOutputs = new HashSet<>();
510506
Map<Integer, Integer> sampleIndexToIdMap = new IntHashMap<>();
511507
sampleIndexToIdMap.put(0, 0);
@@ -516,15 +512,15 @@ public void testHeaders() throws PipelineJobException, IOException
516512

517513
for (String fn : filenamesOldHeader)
518514
{
519-
File target = new File(_testRoot, fn);
520-
FileUtils.copyFile(new File(oldHeaderSampledataLoc, fn), target);
521-
expectedOutputs.add((Pair<Integer, Integer>) pairs[i]);
515+
File target = FileUtil.appendName(_testRoot, fn);
516+
FileUtils.copyFile(FileUtil.appendName(oldHeaderPath, fn), target);
517+
expectedOutputs.add(pairs.get(i));
522518
oldHeaderFiles.add(target);
523519

524520
if (i < (numOfPairs / 2))
525521
{
526-
sampleIndexToIdMap.put(i + 1, (Integer)pairs[i].getKey());
527-
sampleIdToIndexMap.put((Integer)pairs[i].getKey(), i + 1);
522+
sampleIndexToIdMap.put(i + 1, pairs.get(i).getKey());
523+
sampleIdToIndexMap.put(pairs.get(i).getKey(), i + 1);
528524
}
529525
i++;
530526
}
@@ -537,29 +533,15 @@ public void testHeaders() throws PipelineJobException, IOException
537533

538534
for (String fn : filenamesNewHeader)
539535
{
540-
File target = new File(_testRoot, fn);
541-
FileUtils.copyFile(new File(newHeaderSampledataLoc, fn), target);
536+
File target = FileUtil.appendName(_testRoot, fn);
537+
FileUtils.copyFile(FileUtil.appendName(newHeaderPath, fn), target);
542538
newHeaderFiles.add(target);
543539
}
544540

545541
parser = new IlluminaFastqParser(null, sampleIndexToIdMap, sampleIdToIndexMap, Collections.emptyMap(), LogManager.getLogger(HeaderTestCase.class), newHeaderFiles);
546542
outputs = parser.parseFastqFiles(null);
547543
Assert.assertEquals("Outputs from parseFastqFiles with new headers were not as expected.", expectedOutputs, outputs.keySet());
548544
}
549-
550-
// @After // TODO: Disabling to debug gradle failure on TeamCity
551-
public void cleanup() throws IOException
552-
{
553-
if (container != null)
554-
{
555-
ContainerManager.delete(container, TestContext.get().getUser());
556-
}
557-
558-
if (_testRoot != null)
559-
{
560-
FileUtils.deleteDirectory(_testRoot);
561-
}
562-
}
563545
}
564546
}
565547

0 commit comments

Comments
 (0)