Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions e2e/e2e_namespace_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,22 @@ def test_file_upload_png(page: Page, login):
expect(page.locator("text=Filesize: 2.81 KiB")).to_be_visible()


def test_file_upload_too_large(page: Page, login, tmp_path):
"""Check that uploading a file above the size limit gives a clear error."""
large_file = tmp_path / "large.png"
large_file.write_bytes(b"\0" * (5 * 1024 * 1024))

page.goto("http://localhost:8080/edit/File/en/Test.png")
expect(page.locator("text=Upload new file (max 4 MiB)")).to_be_visible()

page.locator("[name=file]").set_input_files(str(large_file))
with page.expect_navigation():
page.locator("[name=save]").click()
page.wait_for_url("http://localhost:8080/edit/File/en/Test.png")

expect(page.locator("text=The file you tried to upload is bigger than 4 MiB.")).to_be_visible()


def test_file_upload_wrong_png(page: Page, login):
"""Check if uploading a PNG on a .gif doesn't actually work."""
page.goto("http://localhost:8080/File/en/Test.gif")
Expand Down
4 changes: 1 addition & 3 deletions truewiki/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@

log = logging.getLogger(__name__)

# Don't allow any file above 4 MiB.
MAX_UPLOAD_SIZE = (1024**2) * 4
# Cache static files for 24 hours.
CACHE_TIME_STATIC = 3600 * 24
# Cache uploads for 5 minutes by default. These files can be overwritten by
Expand Down Expand Up @@ -188,7 +186,7 @@ def main(bind, port, storage, frontend_url, cache_time, remote_ip_header, valida
print(json.dumps(errors, indent=4))
return

webapp = web.Application(client_max_size=MAX_UPLOAD_SIZE, middlewares=[remove_cookie_middleware])
webapp = web.Application(client_max_size=config.MAX_UPLOAD_SIZE, middlewares=[remove_cookie_middleware])
webapp.on_response_prepare.append(cache_on_prepare)
if remote_ip_header:
global REMOTE_IP_HEADER
Expand Down
1 change: 1 addition & 0 deletions truewiki/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
}
JAVASCRIPT = []
LICENSE = "Unlicensed"
MAX_UPLOAD_SIZE = (1024**2) * 4
PRIMARY_LANGUAGE = "en"
PROJECT_NAME = "Unnamed"

Expand Down
5 changes: 3 additions & 2 deletions truewiki/namespaces/file/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
footer as folder_footer,
)
from ... import (
config,
metadata,
singleton,
wiki_page,
Expand Down Expand Up @@ -177,9 +178,9 @@ def file_get_img(url: str, thumb: Optional[int] = None) -> str:

@staticmethod
def add_edit_content() -> str:
return """
return f"""
<hr />
Upload new file: <input type="file" name="file" />
Upload new file (max {config.MAX_UPLOAD_SIZE // 1024**2} MiB): <input type="file" name="file" />
<p>Please ensure the upload has a license that is compatible with the license of this wiki.</p>
<p>Any new upload will overwrite the existing upload at this location. Preview won't work for new uploads.</p>
<hr />
Expand Down
13 changes: 12 additions & 1 deletion truewiki/web_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from .views import (
edit,
error,
license,
login,
source,
Expand Down Expand Up @@ -189,7 +190,17 @@ async def edit_page_post(request):
location = urllib.parse.quote(page)
return web.HTTPFound(f"/user/login?location=edit/{location}")

payload = await request.post()
try:
payload = await request.post()
except web.HTTPRequestEntityTooLarge:
max_size = config.MAX_UPLOAD_SIZE // 1024**2
return error.view(
user,
page,
f"The file you tried to upload is bigger than {max_size} MiB. "
"Your changes were not saved. Please make the file smaller and try again.",
status=413,
)
if "content" not in payload:
raise web.HTTPNotFound()
content = payload["content"].replace("\r", "")
Expand Down