Spaces:
Running
Running
File size: 4,721 Bytes
35eb445 f551342 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | """Deciding what an upload is from its bytes."""
from __future__ import annotations
import io
import pytest
from app import uploads
from app.config import Limits
from app.errors import ApiError
@pytest.mark.parametrize(("head", "expected"), [
(b"\xff\xd8\xff\xe0\x00\x10JFIF", ("image", "jpg")),
(b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR", ("image", "png")),
(b"GIF89a\x01\x00\x01\x00", ("image", "gif")),
(b"RIFF\x24\x00\x00\x00WEBPVP8 ", ("image", "webp")),
(b"\x00\x00\x00\x20ftypisom\x00\x00\x02\x00", ("video", "mp4")),
(b"\x00\x00\x00\x14ftypqt \x00\x00\x00\x00", ("video", "mov")),
(b"\x00\x00\x00\x08wide\x00\x00\x00\x00mdat", ("video", "mov")),
(b"\x1a\x45\xdf\xa3\x9fB\x86\x81\x01B\xf7\x81\x01B\xf2\x81\x04B\xf3\x81\x08B\x82\x84webm", ("video", "webm")),
(b"\x1a\x45\xdf\xa3\xa3B\x86\x81\x01B\xf7\x81\x01B\xf2\x81\x04B\xf3\x81\x08B\x82\x88matroska", ("video", "mkv")),
])
def test_accepted_signatures(head, expected):
assert uploads.sniff(head) == expected
@pytest.mark.parametrize("head", [
b"#EXTM3U\n#EXT-X-VERSION:3\n",
b"ffconcat version 1.0\nfile /etc/passwd\n",
b'<?xml version="1.0"?><MPD>',
b"<svg xmlns='http://www.w3.org/2000/svg'>",
b"<!doctype html><html>",
b"RIFF\x24\x00\x00\x00WAVEfmt ",
b"RIFF\x24\x00\x00\x00AVI LIST",
b"\x00\x00\x00\x18ftypheic\x00\x00\x00\x00",
b"\x00\x00\x00\x1cftypavif\x00\x00\x00\x00",
b"%PDF-1.7\n",
b"PK\x03\x04\x14\x00\x00\x00",
b"BM\x36\x00\x0c\x00\x00\x00",
b"v=0\no=- 0 0 IN IP4 127.0.0.1\n",
b"",
])
def test_refused_signatures(head):
assert uploads.sniff(head) is None
@pytest.mark.parametrize(("given", "shown"), [
("team photo.JPG", "team photo.JPG"),
("../../etc/passwd", "passwd"),
("C:\\Users\\me\\clip.mp4", "clip.mp4"),
('<img src=x onerror="1">.png', "_img src_x onerror__1__.png"),
("grüße-şehir.jpg", "grüße-şehir.jpg"),
("...", "upload"),
(None, "upload"),
("a" * 200 + ".jpg", "a" * 80),
])
def test_file_name_becomes_a_harmless_label(given, shown):
assert uploads.label(given) == shown
def test_extension_comes_from_the_bytes_and_the_name_is_random(media, tmp_path):
first = uploads.store(io.BytesIO(media["team.png"].read_bytes()), "holiday.jpg", tmp_path, Limits())
second = uploads.store(io.BytesIO(media["team.png"].read_bytes()), "holiday.jpg", tmp_path, Limits())
assert first.path.suffix == ".png" and first.path.parent == tmp_path and first.path != second.path
assert (first.name, first.kind, first.width, first.height) == ("holiday.jpg", "image", 1200, 800)
def test_video_reports_its_size_and_length(media, tmp_path):
item = uploads.store(io.BytesIO(media["clip.mp4"].read_bytes()), "clip.mp4", tmp_path, Limits())
assert (item.kind, item.width, item.height, item.path.suffix) == ("video", 640, 360, ".mp4")
assert 2.5 < item.duration < 3.5
def test_refused_file_is_not_left_on_disk(media, tmp_path):
with pytest.raises(ApiError) as caught:
uploads.store(io.BytesIO(media["moving.gif"].read_bytes()), "moving.gif", tmp_path, Limits())
assert caught.value.code == "animated_image" and list(tmp_path.iterdir()) == []
def test_audio_only_file_in_a_video_container_is_refused(tmp_path):
import subprocess
sound = tmp_path / "sound.m4a"
subprocess.run(["ffmpeg", "-hide_banner", "-loglevel", "error", "-nostdin", "-y", "-f", "lavfi", "-i",
"sine=frequency=440:duration=1", "-c:a", "aac", str(sound)], check=True)
with pytest.raises(ApiError) as caught:
uploads.store(io.BytesIO(sound.read_bytes()), "sound.mp4", tmp_path / "in", Limits())
assert caught.value.code == "unreadable_file"
@pytest.mark.parametrize(("extension", "codec"), [("webm", ["-c:v", "libvpx", "-b:v", "200k"]),
("mkv", ["-c:v", "libx264", "-preset", "ultrafast"]),
("mov", ["-c:v", "libx264", "-preset", "ultrafast"])])
def test_webm_matroska_and_quicktime_videos_are_read(tmp_path, extension, codec):
import subprocess
clip = tmp_path / f"clip.{extension}"
subprocess.run(["ffmpeg", "-hide_banner", "-loglevel", "error", "-nostdin", "-y", "-f", "lavfi", "-i",
"testsrc2=size=160x90:rate=10:duration=1", *codec, "-pix_fmt", "yuv420p", str(clip)], check=True)
# The name claims another type on purpose: the stored extension follows the bytes.
item = uploads.store(io.BytesIO(clip.read_bytes()), "holiday.mp4", tmp_path / "in", Limits())
assert (item.kind, item.width, item.height, item.path.suffix) == ("video", 160, 90, f".{extension}")
assert 0.9 <= item.duration <= 1.2
|