package org.openslx.taskmanager.tasks; import java.nio.file.Files; import java.nio.file.Paths; import org.apache.commons.io.FilenameUtils; import org.openslx.satserver.util.Util; import org.openslx.taskmanager.api.AbstractTask; import com.google.gson.annotations.Expose; public class DeleteFile extends AbstractTask { protected static final String[] ALLOWED_DIRS = { "/tmp/", "/opt/openslx/configs/" }; @Expose private String file = null; private Output status = new Output(); @Override protected boolean initTask() { this.setStatusObject( status ); this.file = FilenameUtils.normalize( this.file ); if ( this.file == null || !Util.startsWith( this.file, ALLOWED_DIRS ) ) { status.error = "File not in allowed directory"; return false; } return true; } @Override protected boolean execute() { try { Files.deleteIfExists( Paths.get( this.file ) ); } catch ( Exception e1 ) { status.error = e1.toString(); return false; } return true; } /** * Output - contains additional status data of this task */ @SuppressWarnings( "unused" ) private static class Output { protected String error = null; } }