Skip to content

Commit d8d5bd0

Browse files
committed
added filename blacklist
1 parent d5a81fc commit d8d5bd0

4 files changed

Lines changed: 42 additions & 1 deletion

File tree

src/main/java/de/unirostock/sems/cbarchive/web/Fields.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
*/
1919

2020
import java.io.File;
21+
import java.util.Arrays;
22+
import java.util.Collections;
23+
import java.util.HashSet;
24+
import java.util.Set;
2125

2226
import javax.servlet.ServletContext;
2327

@@ -40,6 +44,11 @@ public class Fields {
4044
public static File STORAGE = new File ("/tmp/CombineArchiveWebStorage");
4145

4246
public static File SETTINGS_FILE = new File( STORAGE, SETTINGS_FILE_NAME );
47+
48+
/** Blacklist of file names */
49+
public static final Set<String> FILENAME_BLACKLIST = Collections.unmodifiableSet(
50+
new HashSet<String>( Arrays.asList( new String[] {"metadata.rdf", "manifest.xml"} ) )
51+
);
4352

4453
/** The Constant COOKIE_AGE. */
4554
public static final int COOKIE_AGE = 60*60*24*365;

src/main/java/de/unirostock/sems/cbarchive/web/Tools.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import javax.servlet.http.Part;
3535
import javax.xml.bind.DatatypeConverter;
3636

37+
import org.apache.commons.io.FilenameUtils;
38+
3739
import com.fasterxml.jackson.core.JsonProcessingException;
3840

3941
import de.binfalse.bflog.LOGGER;
@@ -258,4 +260,21 @@ public static URI generateWorkspaceRedirectUri( HttpServletRequest requestContex
258260

259261
return newLocation;
260262
}
263+
264+
/**
265+
* checks whether a filename is blacklisted or not
266+
*
267+
* @param filename
268+
* @return true if filename is blacklisted
269+
*/
270+
public static boolean isFilenameBlacklisted( String filename ) {
271+
272+
if( filename == null || filename.isEmpty() )
273+
return true;
274+
275+
if( Fields.FILENAME_BLACKLIST.contains( FilenameUtils.getName(filename) ) )
276+
return true;
277+
278+
return false;
279+
}
261280
}

src/main/java/de/unirostock/sems/cbarchive/web/UserManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,13 @@ public void deleteArchiveSilent( String archiveId ) {
305305
}
306306

307307
public void updateArchiveEntry( String archiveId, ArchiveEntryDataholder newEntryDataholder ) throws CombineArchiveWebException {
308+
Archive archive = null;
309+
310+
if( Tools.isFilenameBlacklisted(newEntryDataholder.getFileName()) || Tools.isFilenameBlacklisted(newEntryDataholder.getFilePath()) )
311+
throw new CombineArchiveWebException(
312+
MessageFormat.format("The filename is blacklisted. You may not add files called {0}!", newEntryDataholder.getFileName())
313+
);
308314

309-
Archive archive;
310315
try {
311316
archive = getArchive(archiveId);
312317
} catch (FileNotFoundException e) {

src/main/java/de/unirostock/sems/cbarchive/web/dataholder/Archive.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import javax.xml.bind.annotation.XmlAccessorType;
3434
import javax.xml.transform.TransformerException;
3535

36+
import org.apache.commons.io.FilenameUtils;
3637
import org.jdom2.JDOMException;
3738

3839
import com.fasterxml.jackson.annotation.JsonIgnore;
@@ -46,6 +47,7 @@
4647
import de.unirostock.sems.cbarchive.ArchiveEntry;
4748
import de.unirostock.sems.cbarchive.CombineArchive;
4849
import de.unirostock.sems.cbarchive.CombineArchiveException;
50+
import de.unirostock.sems.cbarchive.web.Tools;
4951
import de.unirostock.sems.cbarchive.web.exception.CombineArchiveWebException;
5052
import de.unirostock.sems.cbext.Formatizer;
5153

@@ -242,6 +244,12 @@ public ArchiveEntry addArchiveEntry(String fileName, Path file, ReplaceStrategy
242244
throw new CombineArchiveWebException("The archive was not opened");
243245
}
244246

247+
// check for blacklisted filename
248+
if( Tools.isFilenameBlacklisted(fileName) )
249+
throw new CombineArchiveWebException(
250+
MessageFormat.format("The filename is blacklisted. You may not add files called {0}!", FilenameUtils.getName(fileName))
251+
);
252+
245253
ArchiveEntry entry = null;
246254

247255
if( strategy == ReplaceStrategy.RENAME || strategy == ReplaceStrategy.OVERRIDE ) {

0 commit comments

Comments
 (0)