Gitlab Community Edition Instance

Commit a01ae50d authored by mhellka's avatar mhellka
Browse files

tus: Prevent unfinished uploads from beeing used

If a tus upload has a target length, it should be considered incomplete
if it is smaller (or larger) than that.
parent 7b389ef5
Pipeline #291611 passed with stages
in 9 minutes
......@@ -88,7 +88,7 @@ public class TusBlueprint implements RestBlueprint {
ctx.status(200);
ctx.header("Cache-Control", "no-store");
ctx.header(TUS_RESUMABLE, TUS_VERSION);
ctx.header(TUS_UPLOAD_OFFSET, chunk.getOffset());
ctx.header(TUS_UPLOAD_OFFSET, chunk.getActualSize());
if (chunk.getLength() > -1)
ctx.header(TUS_UPLOAD_LENGTH, chunk.getLength());
if (chunk.getExpireMillis() > 0)
......@@ -132,7 +132,7 @@ public class TusBlueprint implements RestBlueprint {
if (!chunk.tryLock())
throw new ErrorResponse(409, "Conflict", "Failed to open chunk: Resource locked.");
if (offset != chunk.getOffset()) {
if (offset != chunk.getActualSize()) {
chunk.unlock();
throw new ErrorResponse(409, "TusError", "Upload-Offset header does not match current file size.");
}
......@@ -155,12 +155,12 @@ public class TusBlueprint implements RestBlueprint {
.then(r -> {
Utils.closeQuietly(ch);
chunk.unlock();
ctx.header(TUS_UPLOAD_OFFSET, chunk.getOffset());
ctx.header(TUS_UPLOAD_OFFSET, chunk.getActualSize());
ctx.close();
}, err -> {
Utils.closeQuietly(ch);
chunk.unlock();
ctx.header(TUS_UPLOAD_OFFSET, chunk.getOffset());
ctx.header(TUS_UPLOAD_OFFSET, chunk.getActualSize());
ctx.abort(err);
});
......
......@@ -62,7 +62,7 @@ class TusFile {
this.length = length;
}
public long getOffset() {
public long getActualSize() {
try {
return Files.size(path);
} catch (final IOException e) {
......
......@@ -38,10 +38,13 @@ public class TusUrlFetch implements UrlFetchService {
final TusFile chunk = tus.getChunk(uri.getSchemeSpecificPart())
.orElseThrow(() -> new IOException("Chunk not found"));
if (chunk.getOffset() > 0) {
if (chunk.getActualSize() > 0) {
if (!chunk.tryLock())
throw new IOException("Chunk locked");
throw new IllegalStateException("TUS chunk is locked");
if (chunk.getLength() > 0 && chunk.getActualSize() != chunk.getLength())
throw new IllegalStateException("TUS chunk is not finished (size smaller than expected)");
try (FileChannel src = FileChannel.open(chunk.getPath());
WritableByteChannel dst = target.getWriteChannel()) {
......
......@@ -92,7 +92,7 @@ public class TusServiceTest {
rs.assertHeaderEquals("Tus-Resumable", "1.0.0");
rs.assertHeaderEquals("Upload-Offset", Integer.toString(data.position()));
assertEquals(data.position(), tus.getChunk(chunkId).get().getOffset());
assertEquals(data.position(), tus.getChunk(chunkId).get().getActualSize());
}
@Test
......@@ -130,7 +130,7 @@ public class TusServiceTest {
rs.assertHeaderEquals("Tus-Resumable", "1.0.0");
rs.assertHeaderEquals("Upload-Offset", Long.toString(size));
assertEquals(size, tus.getChunk(chunkId).get().getOffset());
assertEquals(size, tus.getChunk(chunkId).get().getActualSize());
Path tmpFile = tmp.newFile().toPath();
try (SeekableByteChannel wchan = Files.newByteChannel(tmpFile, StandardOpenOption.WRITE)) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment