|
@@ -1,5 +1,5 @@
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -24,10 +24,9 @@ from io import BufferedReader
|
|
|
import mimetypes
|
|
|
from itertools import chain
|
|
|
import os
|
|
|
-from bottle import static_file, response, HTTPError, abort, LocalRequest, HTTPResponse
|
|
|
+from bottle import static_file, HTTPError, abort, LocalRequest, HTTPResponse
|
|
|
from urllib.parse import urlparse, quote, quote_plus
|
|
|
from .hash_util import blake_file, bytes_to_base32, blake
|
|
|
-import bottle
|
|
|
|
|
|
|
|
|
URL_MUST_ESCAPE = bytes([
|
|
@@ -44,34 +43,6 @@ URL_MUST_ESCAPE = bytes([
|
|
|
URL_SAFE = bytes(( i for i in range(int('0xff',0)+1) if i not in map(int, URL_MUST_ESCAPE) ))
|
|
|
|
|
|
CLIP_SIZE_LIMIT = 65535
|
|
|
-def _validate(filename: str, tool: str, root='rest/static/files') -> bytes:
|
|
|
- ret = static_file('/'.join([filename,]*2) + '.file', root=root)
|
|
|
- if isinstance(ret, HTTPError):
|
|
|
- return (
|
|
|
- 'abort', 404, f"No such `{tool.title()}`: {filename}"
|
|
|
- )
|
|
|
- if ret.status_code == 304:
|
|
|
- return ret
|
|
|
-
|
|
|
- if ret.content_length > CLIP_SIZE_LIMIT:
|
|
|
- return (
|
|
|
- 'abort', 418, f"{tool.title()} size exceeds {CLIP_SIZE_LIMIT}"
|
|
|
- )
|
|
|
-
|
|
|
- content: bytes = ret.body.read() if isinstance(ret.body, BufferedReader) else ret.body.encode('utf-8')
|
|
|
-
|
|
|
- _bytes = blake(content, person=tool.encode('utf-8'))
|
|
|
- _b32 = bytes_to_base32(_bytes)
|
|
|
- if _b32 != filename:
|
|
|
- return (
|
|
|
- 'abort', 410, f"{tool.title()} content differs"
|
|
|
- )
|
|
|
- return content
|
|
|
-
|
|
|
-def validate(filename: str, tool: str, root='rest/static/files') -> bytes:
|
|
|
- ret = _validate(filename, tool, root)
|
|
|
- return getattr(bottle, ret[0])(*ret[1:]) if isinstance(ret, tuple) else ret
|
|
|
-
|
|
|
|
|
|
def get_filename(filename: str, root: str = 'rest/static/files'):
|
|
|
path = '/'.join([filename,]*2)
|
|
@@ -118,7 +89,7 @@ def validate_file(filename: str, root: str = 'rest/static/files', download=True,
|
|
|
def validate_parameter(request: LocalRequest, name: str) -> bytes:
|
|
|
if name not in request.params:
|
|
|
return abort(400, f"Missing parameter: '{name}'")
|
|
|
-
|
|
|
+
|
|
|
|
|
|
OVERHEAD = 1024
|
|
|
content: bytes = request.query.get(name, None)
|
|
@@ -134,11 +105,12 @@ def validate_parameter(request: LocalRequest, name: str) -> bytes:
|
|
|
content: bytes = (content or request.params[name].encode('utf-8'))
|
|
|
else:
|
|
|
content: bytes = (content or request.params[name].encode('latin-1'))
|
|
|
-
|
|
|
+
|
|
|
if len(content) > CLIP_SIZE_LIMIT:
|
|
|
return abort(418, f"Paste can not exceed {CLIP_SIZE_LIMIT} bytes")
|
|
|
return content
|
|
|
|
|
|
+
|
|
|
def validate_url(url: str) -> str:
|
|
|
scheme, netloc, path, params, query, fragment = urlparse(url)
|
|
|
|