validate.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #
  2. # Copyright (c) Daniel Sheffield 2023
  3. # All rights reserved
  4. #
  5. # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
  6. # https://www.ietf.org/rfc/rfc3696.txt
  7. """
  8. Without quotes, local-parts may consist of any combination of
  9. alphabetic characters, digits, or any of the special characters
  10. ! # $ % & ' * + - / = ? ^ _ ` . { | } ~
  11. period (".") may also appear, but may not be used to start or end the
  12. local part, nor may two or more consecutive periods appear. Stated
  13. differently, any ASCII graphic (printing) character other than the
  14. at-sign ("@"), backslash, double quote, comma, or square brackets may
  15. appear without quoting. If any of that list of excluded characters
  16. are to appear, they must be quoted.
  17. """
  18. from io import BufferedReader
  19. from itertools import chain, zip_longest
  20. from bottle import static_file, HTTPError, abort, LocalRequest
  21. from urllib.parse import urlparse, quote, quote_plus, quote_from_bytes, urlencode
  22. from .hash_util import bytes_to_base32, blake
  23. # according to rfc3696
  24. URL_MUST_ESCAPE = bytes([
  25. x for x in chain(
  26. # control characters
  27. range(int('0x1F', 0)+1),
  28. # 0x7F and non 7bit-ASCII
  29. range(int('0x7F', 0,), int('0xFF', 0)+1),
  30. # specifically excluded
  31. b'@\\",[]'
  32. )
  33. ])
  34. # so give this list to urllib.parse.quote which follows rfc3986
  35. URL_SAFE = bytes(( i for i in range(int('0xff',0)+1) if i not in map(int, URL_MUST_ESCAPE) ))
  36. CLIP_SIZE_LIMIT = 65535
  37. def validate(filename: str) -> bytes:
  38. ret = static_file('/'.join([filename,]*2) + '.file', root='app/rest/static')
  39. if isinstance(ret, HTTPError):
  40. return abort(404, f"No such paste: {filename}")
  41. if ret.content_length > CLIP_SIZE_LIMIT:
  42. return abort(418, f"Paste size exceeds {CLIP_SIZE_LIMIT}")
  43. content: bytes = ret.body.read() if isinstance(ret.body, BufferedReader) else ret.body.encode('utf-8')
  44. _bytes = blake(content, person='clip'.encode('utf-8'))
  45. _b32 = bytes_to_base32(_bytes)
  46. if _b32 != filename:
  47. return abort(410, f"Paste content differs")
  48. return content
  49. def validate_parameter(request: LocalRequest, name: str) -> bytes:
  50. if name not in request.params:
  51. return abort(400, f"Missing parameter: '{name}'")
  52. # TODO: what is correct overhead for form content?
  53. OVERHEAD = 1024
  54. content: bytes = request.query.get(name, None)
  55. content_length = request.content_length
  56. if content_length == -1:
  57. return abort(418, f"Content-Length must be specified")
  58. if content_length > CLIP_SIZE_LIMIT + OVERHEAD:
  59. return abort(418, f"Content-Length can not exceed {CLIP_SIZE_LIMIT*3} bytes")
  60. # TODO: add test for both query/form param
  61. if 'multipart/form-data' in request.content_type:
  62. # TODO: what about binary data ?
  63. content: bytes = (content or request.params[name].encode('utf-8'))
  64. else:
  65. content: bytes = (content or request.params[name].encode('latin-1'))
  66. if len(content) > CLIP_SIZE_LIMIT:
  67. return abort(418, f"Paste can not exceed {CLIP_SIZE_LIMIT} bytes")
  68. return content
  69. def validate_url(url: str) -> str:
  70. scheme, netloc, path, params, query, fragment = urlparse(url)
  71. if not scheme: return abort(400, "URL has no scheme")
  72. if scheme == 'file' and not path: return abort(400, "File URL has no path")
  73. if scheme in ('http', 'https') and not netloc: return abort(400, "HTTP(S) URL has no netloc")
  74. if netloc:
  75. try:
  76. user_info, loc = netloc.rsplit('@', 1)
  77. except ValueError:
  78. user_info = ''
  79. loc = ''
  80. if user_info:
  81. user_info = quote(user_info, safe=URL_SAFE)
  82. netloc = f"{user_info}@{''.join(loc)}"
  83. else:
  84. # TODO: do this properly, ie, valid dns-name/ip/port etc
  85. netloc = quote(netloc, safe=URL_SAFE)
  86. path = quote(path, safe=URL_SAFE)
  87. params = quote_plus(params, safe=URL_SAFE)
  88. query = quote(query, safe=URL_SAFE)
  89. fragment = quote(fragment, safe=URL_SAFE)
  90. url = f'{scheme}://{netloc}{path}{params}'
  91. if query:
  92. url = f'{url}?{query}'
  93. if fragment:
  94. url = f'{url}#{fragment}'
  95. return url