Skip to content
Open
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
42 changes: 41 additions & 1 deletion core/security.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import config
import random
import ipaddress
import re
import socket
import time

from urllib.parse import quote

from core import helpers

def simulate_load():
Expand Down Expand Up @@ -92,4 +96,40 @@ def cost_exceeded(qry_fields):
if total_query_cost > total_cost_allowed:
return True

return False
return False

IMPORT_ALLOWED_SCHEMES = ('http', 'https')
IMPORT_ALLOWED_PORTS = (80, 443)
IMPORT_MAX_BYTES = 64 * 1024
_HOSTNAME_RE = re.compile(r'[A-Za-z0-9](?:[A-Za-z0-9.-]{0,251}[A-Za-z0-9])?')

def build_safe_import_url(scheme, host, port, path):
"""Validate an importPaste destination and return the URL to fetch.

Only http/https on the standard ports are allowed, and every address the
host resolves to must be a public (globally routable) address, which blocks
loopback, RFC1918, link-local (cloud metadata) and other internal targets.
"""
scheme = (scheme or '').lower()
if scheme not in IMPORT_ALLOWED_SCHEMES:
raise ValueError('Scheme not allowed')
if port is None:
port = 443 if scheme == 'https' else 80
if not is_port(port) or port not in IMPORT_ALLOWED_PORTS:
raise ValueError('Port not allowed')
if not isinstance(host, str) or not _HOSTNAME_RE.fullmatch(host):
raise ValueError('Invalid host')
try:
addresses = socket.getaddrinfo(host, port, proto=socket.IPPROTO_TCP)
except (socket.gaierror, UnicodeError):
raise ValueError('Host does not resolve')
if not addresses:
raise ValueError('Host does not resolve')
for address in addresses:
ip = ipaddress.ip_address(address[4][0])
if not ip.is_global:
raise ValueError('Destination not allowed')
path = path if isinstance(path, str) else '/'
if not path.startswith('/'):
path = '/' + path
return '{}://{}:{}{}'.format(scheme, host, port, quote(path, safe="/-._~%?=&"))
15 changes: 13 additions & 2 deletions core/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import graphene
import requests

from graphql import GraphQLError

Expand Down Expand Up @@ -207,8 +208,18 @@ class Arguments:
scheme = graphene.String(required=True)

def mutate(self, info, host='pastebin.com', port=443, path='/', scheme="http"):
url = security.strip_dangerous_characters(f"{scheme}://{host}:{port}{path}")
cmd = helpers.run_cmd(f'curl --insecure {url}')
# Fetch with an HTTP client instead of a shell `curl`, only towards
# validated public destinations, without following redirects.
try:
url = security.build_safe_import_url(scheme, host, port, path)
response = requests.get(url, timeout=5, allow_redirects=False, stream=True)
try:
raw = response.raw.read(security.IMPORT_MAX_BYTES, decode_content=True)
finally:
response.close()
except (ValueError, requests.RequestException):
return ImportPaste(result='error')
cmd = raw.decode('utf-8', errors='replace')

owner = Owner.query.filter_by(name='DVGAUser').first()
Paste.create_paste(
Expand Down
Loading