Skip to content

Commit 34e841f

Browse files
committed
refs #92 - check for blank lines in fetch file and throw error if there are any
1 parent 2a401dd commit 34e841f

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

src/main/java/gov/loc/repository/bagit/reader/FetchReader.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
public final class FetchReader {
2424
private static final Logger logger = LoggerFactory.getLogger(FetchReader.class);
2525
private static final ResourceBundle messages = ResourceBundle.getBundle("MessageBundle");
26+
private static final String FETCH_LINE_REGEX = ".*[ \t]*(\\d*|-)[ \t]*.*";
2627

2728
private FetchReader(){
2829
//intentionally left empty
@@ -51,14 +52,19 @@ public static List<FetchItem> readFetch(final Path fetchFile, final Charset enco
5152
long length = 0;
5253
URL url = null;
5354
while(line != null){
54-
parts = line.split("\\s+", 3);
55-
final Path path = TagFileReader.createFileFromManifest(bagRootDir, parts[2]);
56-
length = parts[1].equals("-") ? -1 : Long.decode(parts[1]);
57-
url = new URL(parts[0]);
58-
59-
logger.debug(messages.getString("read_fetch_file_line"), url, length, parts[2], fetchFile);
60-
final FetchItem itemToFetch = new FetchItem(url, length, path);
61-
itemsToFetch.add(itemToFetch);
55+
if(line.matches(FETCH_LINE_REGEX) && !line.matches("\\s*")){
56+
parts = line.split("\\s+", 3);
57+
final Path path = TagFileReader.createFileFromManifest(bagRootDir, parts[2]);
58+
length = parts[1].equals("-") ? -1 : Long.decode(parts[1]);
59+
url = new URL(parts[0]);
60+
61+
logger.debug(messages.getString("read_fetch_file_line"), url, length, parts[2], fetchFile);
62+
final FetchItem itemToFetch = new FetchItem(url, length, path);
63+
itemsToFetch.add(itemToFetch);
64+
}
65+
else{
66+
throw new InvalidBagitFileFormatException(messages.getString("invalid_fetch_file_line_error").replace("{}", line));
67+
}
6268

6369
line = reader.readLine();
6470
}

src/main/resources/MessageBundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ unparsable_version_error=Version must be in format MAJOR.MINOR but was [{}]!
115115
#for FetchReader.java
116116
reading_fetch_file=Attempting to read [{}].
117117
read_fetch_file_line=Read URL [{}] length [{}] path [{}] from fetch file [{}].
118+
invalid_fetch_file_line_error=The Line [{}] is invalid for fetch.txt. Each line must take the form of <URL> <LENGTH> <FILENAM>.
118119

119120
#for KeyValueReader.java
120121
read_key_value_line=Found key [{}] value [{}] in file [{}] using split regex [{}].

src/test/java/gov/loc/repository/bagit/reader/FetchReaderTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public void testReadFetchWithSizeSpecified() throws Exception{
6666
}
6767
}
6868

69+
@Test(expected=InvalidBagitFileFormatException.class)
70+
public void testReadBlankLinesThrowsException() throws Exception{
71+
Path fetchFile = Paths.get(getClass().getClassLoader().getResource("fetchFiles/fetchWithBlankLines.txt").toURI());
72+
FetchReader.readFetch(fetchFile, StandardCharsets.UTF_8, Paths.get("/foo"));
73+
}
74+
6975
@Test(expected=InvalidBagitFileFormatException.class)
7076
public void testReadWindowsSpecialDirMaliciousFetchThrowsException() throws Exception{
7177
Path fetchFile = Paths.get(getClass().getClassLoader().getResource("maliciousFetchFile/windowsSpecialDirectoryName.txt").toURI());
@@ -92,4 +98,16 @@ public void testReadFileUrlMaliciousFetchThrowsException() throws Exception{
9298
}
9399
throw new MaliciousPathException("Skipping for windows cause it isn't valid");
94100
}
101+
102+
@Test
103+
public void foo(){
104+
String regex = ".*[ \t]*(\\d*|-)[ \t]*.*";
105+
String test1 = "http://localhost/foo/data/test2.txt - ~/foo/bar/ham.txt";
106+
String test2 = "http://localhost/foo/data/dir1/test3.txt 100057 data/dir1/test3.txt";
107+
String test3 = "http://localhost/foo/data/dir1/test3.txt \t 100057 \t data/dir1/test3.txt";
108+
109+
System.err.println(test1.matches(regex));
110+
System.err.println(test2.matches(regex));
111+
System.err.println(test3.matches(regex));
112+
}
95113
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
http://localhost/foo/data/dir1/test3.txt 1 data/dir1/test3.txt
2+
http://localhost/foo/data/dir2/dir3/test5.txt 2 data/dir2/dir3/test5.txt
3+
http://localhost/foo/data/dir2/test4.txt 3 data/dir2/test4.txt
4+
http://localhost/foo/data/test%201.txt 4 data/test 1.txt
5+
http://localhost/foo/data/test2.txt 5 data/test2.txt
6+
7+

0 commit comments

Comments
 (0)