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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

This file owns the release history for `ScanLink`.

## v0.8.1b2

- **Callbacks Reach Public HTTPS Websites Only (security, R2-010)**: a linked website could list `http://127.0.0.1:…`, `localhost`, private, link-local or metadata addresses, or ScanLink itself, and every scan was then posted there from the operator's computer without `Origin` or `Sec-Fetch-*`; a callback to `http://localhost:5000/shutdown` stopped ScanLink. New `callback_transport` module: `check_callback_url` requires `https` and refuses `localhost`, `*.localhost` and non-public address literals in any spelling (`127.1`, `2130706433`, `[::ffff:127.0.0.1]`) at submission (`400 callback_not_allowed`); `post_scan` resolves the host itself, refuses the delivery if any address is loopback, private, CGNAT, link-local, unique-local, multicast, reserved, unspecified, an embedded IPv4 of those (mapped, NAT64, 6to4, Teredo) or one of this computer's own, connects to the checked address with TLS verified for the host name and re-checks the peer, so DNS rebinding cannot reach this computer (`callback.state: refused`, `reason: blocked_address`). Deliveries carry `X-ScanLink-Delivery`, and every endpoint answers such a request with `403 local_only`. Callbacks use the standard library instead of `requests` and never a proxy.
- **Widening A Linked List Asks Again (security, R2-054)**: `SiteRegistry.link` replaced a linked website's list without a window, so one approval let XSS on that site grant the scanner to any origin. A list that gains a site now opens the approval window again, naming the added sites (`confirm(..., added_sites=…)`); until the operator allows them the website stays linked with its approved list, and a declined widening answers `403 link_declined`. Shortening a list stays silent.
- **Bounded Callback Responses (R2-055)**: the callback answer was read in full with a per-read timeout, so a 1 GB body or a gzip bomb was buffered and a body dripping a byte every 20 s held a thread and the PDF forever. `post_scan` sends `Accept-Encoding: identity`, reads only the status line and headers, and a watchdog closes the connection 120 s (`DELIVERY_DEADLINE_SECONDS`) after the delivery started; at most two callbacks are in flight (`MAX_CALLBACKS_IN_FLIGHT`), a third is `failed` with `reason: busy`.
- **Rationed Approval Windows (security, R2-056)**: any website could reopen ScanLink's approval window at once from a sibling subdomain or another port, or forever after leaving it unanswered. Cooldowns now apply to the registrable domain (`sites.cooldown_key`): 10 minutes after **Don't allow** (`403 link_declined`), 5 minutes after an unanswered window (`429 link_rate_limited`). After an unknown website's window closes without **Allow**, other unknown websites wait 2 minutes (`429 link_rate_limited`). A website ScanLink already knows is exempt from that wait and closes an unknown website's open window to ask its own (that request answers `409 link_pending`). The window no longer takes keyboard focus or a grab.
- **Approval Window Shows Every Site In Full (security, R2-115)**: the other-sites list showed 6 rows of 56 characters, so long or numerous sites were approved unseen, and hosts with bidi overrides, zero-width characters or look-alike letters were shown verbatim. `gui.dialogs.confirm_site_link` lists every site (up to 20 rows, as wide as the longest, with scrollbars beyond that) and `utils.normalize_origin` refuses hosts with control, format or separator characters or a trailing dot and stores non-ASCII hosts in their `xn--` form, as browsers send them.
- **Correct Page Count (R2-118)**: the parent polled `state.json` every 250 ms and the child's final `completed` state had no `page_count`, so 5-page scans reported 1 or 2. `scan_runtime.run_scan_job` writes `page_count` into the final state and `ScanWorker._run_scan_subprocess` applies the final state after the child exits.
- **No Orphaned Scans (R2-119)**: killing ScanLink mid-scan left the child scanning, holding the scanner, and its `scanlink-job-*` folder with the PDF in `%TEMP%`. On Windows each child joins a kill-on-close job object; `request.json` carries `parent_pid` and the child (`scan_runtime.watch_parent`) removes its folder and exits when that process ends; `worker.sweep_stale_directories` removes `scanlink-job-*`/`scanlink-pages-*` folders older than 30 s at worker start and every 10 minutes while idle.
- **HEAD And Cut-Off Downloads Keep The Result (R2-120)**: `HEAD /scan/<id>/result` ran the download view and spent the single-use PDF, and a dropped download lost the scan. `HEAD` now answers from `ScanWorker.peek_result` without counting; `consume_result` answers one retry within 30 s of the first download (`RESULT_RETRY_GRACE_SECONDS`) before freeing the bytes.
- **Monotonic Result Lifetimes (R2-121)**: job and result TTLs used `time.time()`, so a clock jump forward deleted an unread result and one backwards kept the PDF in memory for days. `_cleanup_expired_jobs_locked` measures from `time.monotonic()` stamps; wall-clock fields remain for display.
- **Safe `doc_id` (R2-122)**: a CR/LF in `doc_id` (also through `scanlink://scan?docId=…%0a…`) forged `scanner.log` lines, and any JSON type was accepted. `worker.clean_doc_id` accepts text of at most 200 characters without control, format or line-separator characters, or a whole number; anything else is `400 invalid_doc_id` on `/scan` and `/internal/protocol/scan`. Job log lines use `%r`.
- **Standard JSON Only (R2-123)**: `{"doc_id": NaN}` was stored and every status poll echoed invalid JSON. The app's `StrictJSONProvider` rejects `NaN`, `Infinity` and `-Infinity` as `400 invalid_json`.
- **Mid-Acquisition Deadline Is A Timeout (R2-124)**: a scan deadline reached while pages were still arriving was re-wrapped as `acquire_failed`/`failed`. `ScannerManager.scan_to_pdf` re-raises `ScanError` unchanged, so it stays `acquire_timeout`/`timed_out`.
- **Whole-Request Deadline And Connection Cap (R2-125)**: the 30 s socket timeout restarted on every byte, so a client sending one byte every 20 s held its thread for good and about 200 such sockets filled the listen queue. `LoopbackRequestHandler` reads through a deadline reader: line, headers and body must arrive within 30 s (`REQUEST_DEADLINE_SECONDS`), and the TLS handshake within the same time; `BoundedThreadedWSGIServer` serves at most 64 connections (`MAX_CONCURRENT_CONNECTIONS`) and closes the rest at accept.

## v0.8.1b1

- **Callbacks Delivered, To Linked Websites Only (security)**: a finished scan was never posted to its `callback_url`. Since 2026-04-28, `_process_job` passed `_send_callback` the public job snapshot, which carries no URL. `ScanWorker._start_callback` now posts the stored PDF on its own thread (connect 5 s, read 30 s, redirects not followed, only 2xx counts). `SiteRegistry.allows_callback` limits the destination to an allowed website: a browser request may name only its own website or the sites linked with it, and a local tool or `scanlink://` link may name any linked or listed website. The check runs at submission (`400 callback_not_allowed` on `/scan` and `/internal/protocol/scan`; a refused `scanlink://` launch never scans) and again before sending. Each job reports `callback.state` (`waiting`, `sending`, `delivered`, `failed`, `refused`, `not_sent`), and the PDF stays available to poll either way. Without the rule, delivery would have let any page post a scanned document to a host of its choosing through a `scanlink://` link.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.1b1
0.8.1b2
1 change: 1 addition & 0 deletions build_exe.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def build_pyinstaller_args(build_name):
"--hidden-import=PIL.ImageDraw",
"--hidden-import=protocol_handler",
"--hidden-import=sites",
"--hidden-import=callback_transport",
"--hidden-import=flask",
"--hidden-import=werkzeug",
"--hidden-import=jinja2",
Expand Down
351 changes: 351 additions & 0 deletions callback_transport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,351 @@
"""Posting a finished scan to a callback URL on the public internet only.

A callback leaves the operator's computer with a scanned document, so its
destination is checked twice: the URL itself when the scan is requested
(``check_callback_url``), and the address ScanLink actually connects to when it
sends (``post_scan``). The second check resolves the host name itself, refuses
the delivery if any address it resolves to is not a public one, and connects to
the checked address, so a host name re-pointed between the check and the
connection (DNS rebinding) cannot reach this computer or its network.

Refused destinations: anything but ``https``; ``localhost`` and ``*.localhost``;
loopback, private, carrier-grade NAT, link-local (cloud metadata), unique-local,
multicast, reserved, unspecified and documentation addresses, including IPv4
addresses embedded in IPv6 (mapped, NAT64, 6to4, Teredo); and every address of
this computer itself.
"""

import http.client
import ipaddress
import socket
import ssl
import threading
import time
import unicodedata
import uuid
from urllib.parse import urlsplit

try:
import certifi
except ImportError:
certifi = None

CONNECT_TIMEOUT_SECONDS = 5
READ_TIMEOUT_SECONDS = 30
# Whole delivery, from resolving the host to reading the status line and headers.
DELIVERY_DEADLINE_SECONDS = 120
DELIVERY_HEADER = "X-ScanLink-Delivery"

_NAT64 = ipaddress.ip_network("64:ff9b::/96")
_LOCAL_NAT64 = ipaddress.ip_network("64:ff9b:1::/48")


class CallbackRefused(Exception):
"""The destination is not a public https website."""

def __init__(self, reason, detail=""):
super().__init__(detail or reason)
self.reason = reason


class CallbackFailed(Exception):
"""The destination could not be reached or did not answer in time."""


def _embedded_ipv4(address):
if address.version != 6:
return None
if address.ipv4_mapped is not None:
return address.ipv4_mapped
if address in _NAT64 or address in _LOCAL_NAT64:
return ipaddress.IPv4Address(int(address) & 0xFFFFFFFF)
if address.sixtofour is not None:
return address.sixtofour
if address.teredo is not None:
return address.teredo[1]
# IPv4-compatible ::a.b.c.d (deprecated, still routed by some stacks).
if int(address) >> 32 == 0 and int(address) > 1:
return ipaddress.IPv4Address(int(address))
return None


def is_public_address(value):
"""True only for a globally routable unicast address."""
try:
address = ipaddress.ip_address(value.split("%", 1)[0] if isinstance(value, str) else value)
except ValueError:
return False
embedded = _embedded_ipv4(address)
if embedded is not None and not is_public_address(embedded):
return False
return bool(
address.is_global
and not address.is_multicast
and not address.is_reserved
and not address.is_loopback
and not address.is_link_local
and not address.is_unspecified
and not address.is_private
)


def _literal_address(host):
"""The address a host spelled as a number means, in any notation the resolver accepts."""
try:
return ipaddress.ip_address(host.strip("[]"))
except ValueError:
pass
try:
# Shorthand, decimal, octal and hex spellings such as 127.1 or 0x7f000001.
return ipaddress.IPv4Address(socket.inet_aton(host))
except (OSError, ValueError):
return None


def _clean_host(host):
if not host:
return None
host = host.rstrip(".").lower()
if not host or any(unicodedata.category(ch)[0] in "CZ" for ch in host):
return None
return host


def check_callback_url(url):
"""``(host, port, path)`` for a callback URL ScanLink may post to; else ``CallbackRefused``.

Host names are only checked here; their addresses are checked when sending.
"""
if not isinstance(url, str) or not url or len(url) > 2048:
raise CallbackRefused("invalid_url")
try:
parts = urlsplit(url.strip())
port = parts.port or 443
except ValueError as exc:
raise CallbackRefused("invalid_url") from exc
if parts.scheme != "https":
raise CallbackRefused("https_required")
if parts.username is not None or parts.password is not None:
raise CallbackRefused("invalid_url")
host = _clean_host(parts.hostname)
if host is None:
raise CallbackRefused("invalid_url")
if host == "localhost" or host.endswith(".localhost"):
raise CallbackRefused("blocked_address")
literal = _literal_address(host)
if literal is not None and not is_public_address(literal):
raise CallbackRefused("blocked_address")
path = parts.path or "/"
if parts.query:
path = f"{path}?{parts.query}"
if any(ord(ch) < 0x21 or ord(ch) == 0x7F for ch in path):
raise CallbackRefused("invalid_url")
return host, port, path


def _own_addresses():
addresses = set()
for name in {socket.gethostname(), socket.getfqdn()}:
try:
for info in socket.getaddrinfo(name, None):
addresses.add(ipaddress.ip_address(info[4][0].split("%", 1)[0]))
except (OSError, ValueError):
continue
return addresses


def resolve_public(host, port, resolver=None, own_addresses=None):
"""Every address ``host`` resolves to, refusing the host if any of them is not public."""
resolver = resolver or socket.getaddrinfo
own_addresses = own_addresses or _own_addresses
try:
infos = resolver(host, port, type=socket.SOCK_STREAM)
except OSError as exc:
raise CallbackFailed(f"Could not resolve {host!r}: {exc}") from exc
own = own_addresses()
candidates = []
for family, socktype, proto, _canonname, sockaddr in infos:
address = sockaddr[0]
if not is_public_address(address):
raise CallbackRefused("blocked_address", f"{host!r} resolves to {address}")
try:
if ipaddress.ip_address(address.split("%", 1)[0]) in own:
raise CallbackRefused("blocked_address", f"{host!r} resolves to this computer")
except ValueError:
raise CallbackRefused("blocked_address", f"{host!r} resolves to {address!r}")
candidates.append((family, socktype, proto, sockaddr))
if not candidates:
raise CallbackFailed(f"{host!r} has no address")
return candidates


class _Watchdog:
"""Closes the connection when the whole delivery runs past its deadline."""

def __init__(self, seconds):
self.deadline = time.monotonic() + seconds
self.expired = False
self._sock = None
self._lock = threading.Lock()
self._timer = threading.Timer(seconds, self._expire)
self._timer.daemon = True
self._timer.start()

def remaining(self):
return self.deadline - time.monotonic()

def watch(self, sock):
with self._lock:
self._sock = sock
if self.expired:
self._close()

def _expire(self):
with self._lock:
self.expired = True
self._close()

def _close(self):
if self._sock is None:
return
try:
self._sock.shutdown(socket.SHUT_RDWR)
except OSError:
pass
try:
self._sock.close()
except OSError:
pass

def cancel(self):
self._timer.cancel()


def _ssl_context():
context = ssl.create_default_context()
if certifi is not None:
try:
context.load_verify_locations(certifi.where())
except OSError:
pass
return context


class _PinnedHTTPSConnection(http.client.HTTPSConnection):
"""Connects only to addresses ``resolve_public`` approved; verifies TLS for the host name."""

def __init__(self, host, port, watchdog, resolver, own_addresses, context):
super().__init__(host, port, timeout=READ_TIMEOUT_SECONDS, context=context)
self._watchdog = watchdog
self._resolver = resolver
self._own_addresses = own_addresses
self._ssl_context = context
self.peer_address = None

def connect(self):
candidates = resolve_public(self.host, self.port, self._resolver, self._own_addresses)
last_error = None
for family, socktype, proto, sockaddr in candidates:
remaining = self._watchdog.remaining()
if remaining <= 0:
break
sock = socket.socket(family, socktype, proto)
self._watchdog.watch(sock)
try:
sock.settimeout(min(CONNECT_TIMEOUT_SECONDS, remaining))
sock.connect(sockaddr)
peer = sock.getpeername()[0]
if not is_public_address(peer):
raise CallbackRefused("blocked_address", f"connected to {peer}")
sock.settimeout(READ_TIMEOUT_SECONDS)
self.sock = self._ssl_context.wrap_socket(sock, server_hostname=self.host)
self._watchdog.watch(self.sock)
self.peer_address = peer
return
except CallbackRefused:
sock.close()
raise
except OSError as exc:
last_error = exc
sock.close()
raise CallbackFailed(f"Could not connect to {self.host!r}: {last_error or 'deadline reached'}")


def encode_multipart(fields, files):
boundary = f"scanlink-{uuid.uuid4().hex}"
chunks = []
for name, value in fields:
chunks.append(
f'--{boundary}\r\nContent-Disposition: form-data; name="{name}"\r\n\r\n'.encode("ascii")
+ str(value).encode("utf-8")
+ b"\r\n"
)
for name, (filename, content, content_type) in files:
chunks.append(
(
f'--{boundary}\r\nContent-Disposition: form-data; name="{name}"; filename="{filename}"\r\n'
f"Content-Type: {content_type}\r\n\r\n"
).encode("ascii")
+ content
+ b"\r\n"
)
chunks.append(f"--{boundary}--\r\n".encode("ascii"))
return b"".join(chunks), f"multipart/form-data; boundary={boundary}"


def post_scan(
url,
fields,
files,
*,
delivery_id,
user_agent="ScanLink",
deadline_seconds=DELIVERY_DEADLINE_SECONDS,
resolver=None,
own_addresses=None,
context=None,
):
"""POST a multipart scan to ``url``; returns the HTTP status code.

The response body is never read, redirects are never followed, proxies are
never used, and the whole exchange ends at ``deadline_seconds``.
"""
host, port, path = check_callback_url(url)
body, content_type = encode_multipart(fields, files)
watchdog = _Watchdog(deadline_seconds)
connection = _PinnedHTTPSConnection(
host, port, watchdog, resolver, own_addresses, context or _ssl_context()
)
try:
connection.putrequest("POST", path, skip_accept_encoding=True)
connection.putheader("Accept-Encoding", "identity")
connection.putheader("Content-Type", content_type)
connection.putheader("Content-Length", str(len(body)))
connection.putheader("User-Agent", user_agent)
connection.putheader(DELIVERY_HEADER, delivery_id)
connection.endheaders(body)
response = connection.getresponse()
return response.status
except (CallbackRefused, CallbackFailed):
raise
except (OSError, http.client.HTTPException, ValueError) as exc:
if watchdog.expired:
raise CallbackFailed(f"No answer within {deadline_seconds} seconds") from exc
raise CallbackFailed(str(exc) or exc.__class__.__name__) from exc
finally:
watchdog.cancel()
try:
connection.close()
except Exception:
pass


__all__ = [
"CallbackFailed",
"CallbackRefused",
"DELIVERY_HEADER",
"check_callback_url",
"is_public_address",
"post_scan",
"resolve_public",
]
Loading
Loading