~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
Thin client for the Zotero Web API (https://www.zotero.org/support/dev/web_api/v3).
Uses the project's :class:`SafeSession` so every request is SSRF-validated
and response sizes are bounded, rather than pulling in ``pyzotero`` (which
would bypass those protections). A redirect from HTTPS to HTTP is refused,
including the attachment storage redirect that
:meth:`ZoteroClient.download_attachment` follows itself. Only the small slice
of the API needed for import / incremental sync is implemented:
- list collections (for the UI picker)
- list item *versions* for a collection or the whole library
- fetch full item data for a batch of keys
- fetch an item's child attachments
- download an attachment's file bytes
The sync service uses the ``/items/top?format=versions`` endpoint, which
returns the FULL ``{itemKey: version}`` map of the current top-level items.
Reconciling that map against its mapping table lets the sync detect additions,
metadata changes (version bump) and removals (key gone) — and, unlike a
``?since=`` delta, removals *from the collection* too. Full item data is then
fetched only for the keys that actually changed. Pure attachment edits that
don't bump the parent item's version are not detected by this approach —
acceptable for v1.
"""
import re
import time
from dataclasses import dataclass, field
from html import unescape
from typing import Dict, List, Optional, Tuple
from urllib.parse import quote
import requests
from loguru import logger
from ...security import SafeSession
def _html_to_text(html: str) -> str:
"""Minimal HTML→text for Zotero note bodies (which are stored as HTML)."""
if not html:
return ""
# Turn block-ish tags into newlines, drop the rest, unescape entities.
text = re.sub(r"(?i)<(br|/p|/div|/li|/h[1-6])\s*>", "\n", html)
text = re.sub(r"<[^>]+>", "", text)
text = unescape(text)
# Collapse runs of blank lines / trailing spaces.
lines = [ln.strip() for ln in text.splitlines()]
return "\n".join(ln for ln in lines if ln).strip()
ZOTERO_API_BASE = "https://api.zotero.org"
# Zotero 7 desktop's read-only local API (no API key; can serve local files).
ZOTERO_LOCAL_API_BASE = "http://localhost:23119/api"
ZOTERO_API_VERSION = "3"
# Zotero caps multi-key item requests at 50 keys.
_MAX_KEYS_PER_REQUEST = 50
# Pagination page size for list endpoints.
_PAGE_LIMIT = 100
# Hard cap on paginated requests per call. Bounds memory and guards against a
# server that returns full pages forever (ignoring `start`) — without it the
# while-True paginators below could loop unbounded. 10k pages * 100 = 1M items.
_MAX_PAGES = 10_000
# Cap retries on transient errors / explicit Backoff|Retry-After headers.
_MAX_RETRIES = 4
_MAX_BACKOFF_SECONDS = 60
# ---------------------------------------------------------------------------
# Client-safe diagnostics.
#
# Each of these is author-written constant text: no upstream response body, no
# filesystem path, no credential, no caller input. They exist as names rather
# than inline literals because the HTTP layer forwards a Zotero failure to the
# browser only if its text is one of them — see
# ``CLIENT_SAFE_ZOTERO_MESSAGES`` in ``sync_service.py``, which maps each
# message to ITSELF, so what a client receives is always one of these module
# constants and never the exception's own string.
#
# The consequence is the useful one: every other raise site in this package —
# including the ones that interpolate an HTTP status, a retry count or an
# exception class name — is replaced with a generic message automatically,
# and a future raise site cannot leak upstream detail by being forgotten.
# ---------------------------------------------------------------------------
ZOTERO_KEY_REJECTED_MESSAGE = (
"Zotero rejected the API key (HTTP 403). Check the key and that it has "
"read access to this library."
)
ZOTERO_RESOURCE_NOT_FOUND_MESSAGE = (
"Zotero resource not found (HTTP 404). Check the library id / collection "
"key."
)
# The most common Zotero misconfiguration by far, and unguessable from a bare
# "request failed": the library-ID setting holds a username instead of the
# numeric ID. Dropping this text costs the user the one hint that resolves it.
ZOTERO_LIBRARY_ID_NOT_NUMERIC_MESSAGE = (
"Zotero rejected the request (HTTP 400). This usually means the library "
"ID is not the NUMERIC userID / group ID from zotero.org/settings/keys "
"(e.g. a username was entered instead)."
)
class ZoteroError(Exception):
"""Base error for Zotero API interactions."""
class ZoteroAuthError(ZoteroError):
"""Raised on authentication / authorization failures (HTTP 403)."""
class ZoteroTransientError(ZoteroError):
"""Raised when a request fails for a transient reason (network error, or
HTTP 429/5xx) after retries are exhausted.
Distinct from a plain :class:`ZoteroError` so per-item callers
(``download_attachment`` / ``get_attachment_fulltext``) can tell a genuine
"this item has no file / no indexed text" (404 → ``None``) apart from a
temporary failure. A transient failure must NOT be recorded as "no PDF",
otherwise a single rate-limit blip during a bulk sync would permanently
downgrade the item to metadata-only until its Zotero version next changes.
"""
@dataclass
class ZoteroAttachment:
"""A child attachment of a Zotero item."""
key: str
version: int
content_type: Optional[str]
link_mode: Optional[str]
filename: Optional[str]
title: Optional[str]
@property
def is_downloadable_pdf(self) -> bool:
"""True if this is a stored PDF we can fetch via the API.
``linked_file`` attachments point at a file on the user's local disk
and are not retrievable through the Web API.
"""
return (self.content_type or "").lower() == "application/pdf" and (
self.link_mode or ""
) in ("imported_file", "imported_url")
@dataclass
class ZoteroItem:
"""A top-level Zotero item plus its (lazily attached) children."""
key: str
version: int
item_type: str
data: Dict = field(default_factory=dict)
attachments: List[ZoteroAttachment] = field(default_factory=list)
@property
def title(self) -> str:
return (self.data.get("title") or "").strip()
def best_pdf_attachment(self) -> Optional[ZoteroAttachment]:
"""Return the first downloadable PDF attachment, if any."""
for att in self.attachments:
if att.is_downloadable_pdf:
return att
return None
@property
def is_standalone_pdf_attachment(self) -> bool:
"""A top-level stored PDF with no bibliographic parent.
This is what Zotero creates when a PDF is dropped straight into the
library without "Retrieve Metadata" / "Create Parent Item" — very
common in real libraries. It carries no metadata beyond a filename,
but the PDF itself is fully retrievable, so it is importable as its
own document. ``linked_file`` attachments are excluded (the Web API
cannot serve them).
"""
return (
self.item_type == "attachment"
and not self.data.get("parentItem")
and (self.data.get("contentType") or "").lower()
== "application/pdf"
and (self.data.get("linkMode") or "").lower()
in ("imported_file", "imported_url")
)
def as_attachment(self) -> ZoteroAttachment:
"""View this standalone attachment item as its own attachment."""
return ZoteroAttachment(
key=self.key,
version=self.version,
content_type=self.data.get("contentType"),
link_mode=self.data.get("linkMode"),
filename=self.data.get("filename"),
title=self.data.get("title") or self.data.get("filename"),
)
def _sleep_for_backoff(headers, attempt: int) -> None:
"""Honor Zotero ``Backoff`` / ``Retry-After`` headers (capped)."""
raw = headers.get("Backoff") or headers.get("Retry-After")
delay: float
if raw:
try:
delay = float(raw)
except (TypeError, ValueError):
delay = 2.0 * attempt
else:
delay = min(2.0 * attempt, _MAX_BACKOFF_SECONDS)
delay = min(max(delay, 0.0), _MAX_BACKOFF_SECONDS)
if delay > 0:
logger.debug(f"Zotero: backing off {delay:.1f}s (attempt {attempt})")
time.sleep(delay)
class ZoteroClient:
"""Minimal Zotero Web API client scoped to one library."""
def __init__(
self,
api_key: str,
library_type: str,
library_id: str,
timeout: int = 30,
local: bool = False,
):
"""Initialize the client.
Args:
api_key: Zotero API key with read access (not needed when local).
library_type: ``"user"`` or ``"group"``.
library_id: Numeric user or group id.
timeout: Per-request timeout in seconds.
local: Talk to the desktop Zotero local API on localhost:23119
(no API key; can serve local/linked files) instead of the
cloud Web API.
"""
if library_type not in ("user", "group"):
raise ValueError(f"Invalid library_type: {library_type!r}")
if not api_key and not local:
raise ValueError("Zotero API key is required")
if not str(library_id).strip():
raise ValueError("Zotero library_id is required")
self.api_key = api_key
self.library_type = library_type
self.library_id = str(library_id).strip()
self.timeout = timeout
self.local = local
self._base = ZOTERO_LOCAL_API_BASE if local else ZOTERO_API_BASE
self._prefix = (
f"/{'users' if library_type == 'user' else 'groups'}"
f"/{quote(self.library_id, safe='')}"
)
# The local API is on the loopback interface; allow it explicitly.
# What this session fetches is imported into the research library
# (item data, notes, annotations, Zotero's full text), so a redirect
# from HTTPS to HTTP is refused (#6847). The local API is plain HTTP
# from the start, which the guard leaves alone.
self.session = SafeSession(
allow_localhost=local, block_https_downgrade=True
)
headers = {"Zotero-API-Version": ZOTERO_API_VERSION}
# The local desktop API needs no key and is reached over plaintext HTTP
# on the loopback; never attach the key there (it would put a real
# credential on the wire for no benefit).
if api_key and not local:
headers["Zotero-API-Key"] = self.api_key
self.session.headers.update(headers)
# -- lifecycle ---------------------------------------------------------
def close(self) -> None:
if getattr(self, "session", None) is not None:
try:
self.session.close()
except Exception:
logger.debug("Error closing Zotero session", exc_info=True)
finally:
self.session = None # type: ignore[assignment]
def __enter__(self) -> "ZoteroClient":
return self
def __exit__(self, exc_type, exc_val, exc_tb) -> bool:
self.close()
return False
# -- low-level request -------------------------------------------------
def _request(
self,
path: str,
params: Optional[Dict] = None,
*,
stream: bool = False,
allow_redirects: bool = True,
prefixed: bool = True,
):
"""Perform a GET against the Zotero API with retry/backoff.
``path`` is appended to the library prefix (e.g. ``/collections``)
unless ``prefixed=False`` (for non-library endpoints like
``/keys/current`` or ``/users/{id}/groups``).
Returns the ``requests.Response``. Raises :class:`ZoteroAuthError`
on 403 and :class:`ZoteroError` on other non-2xx responses.
When ``allow_redirects=False``, a 3xx response is returned as-is
(not raised) so the caller can follow the redirect itself — used by
``download_attachment`` to avoid forwarding the API key header to the
storage host.
"""
prefix = self._prefix if prefixed else ""
url = f"{self._base}{prefix}{path}"
last_exc: Optional[Exception] = None
for attempt in range(1, _MAX_RETRIES + 1):
try:
response = self.session.get(
url,
params=params,
timeout=self.timeout,
stream=stream,
allow_redirects=allow_redirects,
)
except (
requests.exceptions.Timeout,
requests.exceptions.ConnectionError,
) as exc:
# Transient network errors — worth retrying.
last_exc = exc
logger.warning(
f"Zotero request error (attempt {attempt}): "
f"{type(exc).__name__}"
)
if attempt == _MAX_RETRIES:
raise ZoteroTransientError(
f"Zotero request failed: {type(exc).__name__}"
) from exc
time.sleep(min(2.0 * attempt, _MAX_BACKOFF_SECONDS))
continue
except Exception as exc:
# Non-transient (e.g. SafeSession SSRF/oversize ValueError, or
# a malformed-URL error) — do NOT retry; fail fast.
raise ZoteroError(
f"Zotero request failed: {type(exc).__name__}"
) from exc
status = response.status_code
if status == 403:
raise ZoteroAuthError(ZOTERO_KEY_REJECTED_MESSAGE)
if status == 404:
raise ZoteroError(ZOTERO_RESOURCE_NOT_FOUND_MESSAGE)
if status in (429, 500, 502, 503, 504):
logger.warning(
f"Zotero transient HTTP {status} (attempt {attempt})"
)
if attempt == _MAX_RETRIES:
raise ZoteroTransientError(
f"Zotero request failed after {attempt} attempts "
f"(HTTP {status})"
)
_sleep_for_backoff(response.headers, attempt)
continue
# When the caller manages redirects itself, hand back the 3xx.
if not allow_redirects and 300 <= status < 400:
return response
if status == 400:
# The most common cause by far: a username (or other
# non-numeric value) in the library-ID setting.
raise ZoteroError(ZOTERO_LIBRARY_ID_NOT_NUMERIC_MESSAGE)
if not (200 <= status < 300):
raise ZoteroError(f"Zotero request failed (HTTP {status})")
# Honor a Backoff header even on success (server under load).
if response.headers.get("Backoff"):
_sleep_for_backoff(response.headers, attempt)
return response
# Unreachable, but keeps type-checkers happy.
raise ZoteroError(
f"Zotero request failed: {type(last_exc).__name__ if last_exc else 'unknown'}"
)
@staticmethod
def _library_version(response) -> int:
try:
return int(response.headers.get("Last-Modified-Version", "0"))
except (TypeError, ValueError):
return 0
# -- public API --------------------------------------------------------
def test_connection(self) -> int:
"""Validate credentials by reading the library version.
Returns the current library version. Raises on failure.
"""
response = self._request(
"/items/top", params={"format": "versions", "limit": 1}
)
return self._library_version(response)
def get_current_key_info(self) -> Dict:
"""Return ``/keys/current``: the key's owner ``userID`` + access map.
Cloud API only (the local API has no key); returns ``{}`` in local
mode or when the response isn't JSON. Used to resolve the numeric
userID when the user pasted a username into the library-ID field.
"""
if self.local:
return {}
response = self._request("/keys/current", prefixed=False)
try:
return response.json() or {}
except ValueError:
return {}
def get_collections(self) -> List[Dict]:
"""List all collections in the library (paginated).
Returns a list of ``{"key", "name", "parentCollection"}`` dicts.
"""
collections: List[Dict] = []
start = 0
pages = 0
while True:
pages += 1
if pages > _MAX_PAGES:
raise ZoteroError(
f"Zotero: pagination exceeded safety cap ({_MAX_PAGES} "
"pages) — aborting to avoid a runaway loop"
)
response = self._request(
"/collections",
params={
"format": "json",
"limit": _PAGE_LIMIT,
"start": start,
},
)
page = response.json()
if not page:
break
for entry in page:
data = entry.get("data", {})
collections.append(
{
"key": entry.get("key"),
"name": data.get("name", ""),
"parentCollection": data.get("parentCollection", False),
}
)
if len(page) < _PAGE_LIMIT:
break
start += _PAGE_LIMIT
return collections
def get_groups(self) -> List[Dict]:
"""List the groups this API key can access (id + name + type).
Resolves the key's owner via ``/keys/current`` then reads
``/users/{userID}/groups`` — both non-library-scoped endpoints. Lets
the UI offer a group picker instead of making the user hunt for a
numeric group id on zotero.org.
"""
if self.local:
# The local API has no key endpoint / auth; group discovery isn't
# applicable.
return []
info = self._request("/keys/current", prefixed=False).json()
user_id = info.get("userID") if isinstance(info, dict) else None
if not user_id:
return []
groups: List[Dict] = []
start = 0
pages = 0
while True:
pages += 1
if pages > _MAX_PAGES:
raise ZoteroError(
f"Zotero: pagination exceeded safety cap ({_MAX_PAGES} "
"pages) — aborting to avoid a runaway loop"
)
response = self._request(
f"/users/{quote(str(user_id), safe='')}/groups",
params={
"format": "json",
"limit": _PAGE_LIMIT,
"start": start,
},
prefixed=False,
)
page = response.json()
if not page:
break
for entry in page:
data = entry.get("data", {})
groups.append(
{
"id": str(entry.get("id") or data.get("id") or ""),
"name": data.get("name", ""),
"type": data.get("type", ""),
}
)
if len(page) < _PAGE_LIMIT:
break
start += _PAGE_LIMIT
return groups
def get_collection_name(self, collection_key: str) -> Optional[str]:
"""Return a single collection's display name, or None if missing."""
try:
response = self._request(
f"/collections/{quote(collection_key, safe='')}",
params={"format": "json"},
)
except ZoteroError:
return None
data = response.json().get("data", {})
return data.get("name")
def get_top_item_versions(
self,
collection_key: Optional[str] = None, # gitleaks:allow
) -> Tuple[Dict[str, int], int]:
"""Get ``{itemKey: version}`` for current top-level items.
Scoped to ``collection_key`` when given, else the whole library.
Also returns the library version (from ``Last-Modified-Version``).
This is the FULL current set (not a ``?since=`` delta) on purpose: the
sync service reconciles it against its mapping table to detect both
deletions AND items removed from the collection (which a library-level
``/deleted`` feed would miss). To make that reconciliation safe, this
method raises if the paginated result is INCOMPLETE relative to the
``Total-Results`` header — otherwise a truncated response would look
like a batch of deletions to the caller.
"""
if collection_key:
path = f"/collections/{quote(collection_key, safe='')}/items/top"
else:
path = "/items/top"
versions: Dict[str, int] = {}
library_version = 0
start = 0
pages = 0
expected_total: Optional[int] = None
while True:
pages += 1
if pages > _MAX_PAGES:
raise ZoteroError(
f"Zotero: pagination exceeded safety cap ({_MAX_PAGES} "
"pages) — aborting to avoid a runaway loop"
)
response = self._request(
path,
params={
"format": "versions",
"limit": _PAGE_LIMIT,
"start": start,
},
)
library_version = max(
library_version, self._library_version(response)
)
# Total-Results is the authoritative item count; capture it once.
if expected_total is None:
total = response.headers.get("Total-Results")
try:
expected_total = int(total) if total is not None else None
except (TypeError, ValueError):
expected_total = None
page = response.json() or {}
# ``format=versions`` returns a JSON object {key: version}.
for key, version in page.items():
try:
versions[key] = int(version)
except (TypeError, ValueError):
versions[key] = 0
# A short page is the authoritative end-of-data signal for both
# list and object (``format=versions``) responses.
if len(page) < _PAGE_LIMIT:
break
# When Total-Results is known, stop one round-trip early. Do NOT
# fall back to len(versions) when the header is absent — that makes
# the check a tautology and would stop after a single full page.
if expected_total is not None and len(versions) >= expected_total:
break
start += _PAGE_LIMIT
# Completeness guard: if the server told us how many items exist and we
# ended up with fewer (a truncated / short-circuited page), refuse to
# return a partial set — the caller treats "absent" keys as deletions,
# so a partial map would silently delete real documents.
if expected_total is not None and len(versions) < expected_total:
raise ZoteroError(
"Zotero returned an incomplete version map "
f"({len(versions)}/{expected_total} items); aborting so "
"missing items are not mistaken for deletions"
)
return versions, library_version
def get_items(self, item_keys: List[str]) -> List[ZoteroItem]:
"""Fetch full data for the given top-level item keys (batched)."""
items: List[ZoteroItem] = []
for batch_start in range(0, len(item_keys), _MAX_KEYS_PER_REQUEST):
batch = item_keys[batch_start : batch_start + _MAX_KEYS_PER_REQUEST]
if not batch:
continue
response = self._request(
"/items",
params={
"format": "json",
"itemKey": ",".join(batch),
"limit": _MAX_KEYS_PER_REQUEST,
},
)
for entry in response.json() or []:
data = entry.get("data", {})
items.append(
ZoteroItem(
key=entry.get("key"),
version=int(entry.get("version", 0) or 0),
item_type=data.get("itemType", ""),
data=data,
)
)
return items
def _get_children_raw(self, item_key: str) -> List[Dict]:
"""Fetch all raw child entries of an item/attachment (paginated)."""
entries: List[Dict] = []
start = 0
pages = 0
while True:
pages += 1
if pages > _MAX_PAGES:
raise ZoteroError(
f"Zotero: pagination exceeded safety cap ({_MAX_PAGES} "
"pages) — aborting to avoid a runaway loop"
)
response = self._request(
f"/items/{quote(item_key, safe='')}/children",
params={
"format": "json",
"limit": _PAGE_LIMIT,
"start": start,
},
)
page = response.json() or []
entries.extend(page)
if len(page) < _PAGE_LIMIT:
break
start += _PAGE_LIMIT
return entries
def get_children(self, item_key: str) -> List[ZoteroAttachment]:
"""Fetch an item's child attachments."""
attachments: List[ZoteroAttachment] = []
for entry in self._get_children_raw(item_key):
data = entry.get("data", {})
if data.get("itemType") != "attachment":
continue
attachments.append(
ZoteroAttachment(
key=entry.get("key"),
version=int(entry.get("version", 0) or 0),
content_type=data.get("contentType"),
link_mode=data.get("linkMode"),
filename=data.get("filename"),
title=data.get("title"),
)
)
return attachments
def get_notes(self, item_key: str) -> List[str]:
"""Return the plain text of an item's child notes (HTML stripped)."""
notes: List[str] = []
for entry in self._get_children_raw(item_key):
data = entry.get("data", {})
if data.get("itemType") != "note":
continue
text = _html_to_text(data.get("note") or "")
if text:
notes.append(text)
return notes
def get_annotations(self, attachment_key: str) -> List[str]:
"""Return an attachment's annotations as text (highlight + comment).
Image/area annotations carry no text (only position), so they're
skipped.
"""
out: List[str] = []
for entry in self._get_children_raw(attachment_key):
data = entry.get("data", {})
if data.get("itemType") != "annotation":
continue
parts = []
highlighted = (data.get("annotationText") or "").strip()
comment = (data.get("annotationComment") or "").strip()
if highlighted:
parts.append(highlighted)
if comment:
parts.append(f"[note] {comment}")
if parts:
out.append(" — ".join(parts))
return out
def download_attachment(self, attachment_key: str) -> Optional[bytes]:
"""Download an attachment's file bytes, or None on failure.
Zotero's ``/items/{key}/file`` endpoint typically answers with a 302
to the actual storage location (Zotero's S3, or a WebDAV/external host
for self-hosted storage). We must NOT follow that redirect with the
authenticated session: ``requests`` only strips the standard
``Authorization`` header on a cross-host redirect, so our custom
``Zotero-API-Key`` header would otherwise be forwarded verbatim to the
storage host — a credential leak. Instead we fetch with
``allow_redirects=False`` and follow the ``Location`` ourselves using a
fresh, credential-free :class:`SafeSession` (which still SSRF-validates
the target).
Following the ``Location`` ourselves bypasses the session's HTTPS
downgrade guard, so the same rule is applied here: when the API
answered over HTTPS (cloud mode), a ``Location`` that is not an
absolute ``https://`` URL is refused and the method returns None,
rather than fetching the file in cleartext where an on-path attacker
could replace the bytes stored in the library (#6847). Any such
``Location`` other than ``http://`` was never fetchable here: it is
not resolved against the API URL, so a relative or scheme-less value,
like any other scheme, fails the storage session's URL validation.
Local mode talks to the desktop API over plain HTTP from the start,
so it has no HTTPS to leave.
"""
try:
response = self._request(
f"/items/{quote(attachment_key, safe='')}/file",
allow_redirects=False,
)
except ZoteroTransientError:
# Temporary (429/5xx/network): let the caller skip this item and
# retry next sync rather than silently import it metadata-only.
raise
except ZoteroError:
logger.warning(
f"Zotero: could not download attachment {attachment_key}"
)
return None
if 300 <= response.status_code < 400:
location = (response.headers.get("Location") or "").strip()
if not location:
logger.warning(
f"Zotero: attachment {attachment_key} redirect had no "
"Location header"
)
return None
if location.lower().startswith("file:"):
# The local API redirects to a file:// path on disk. Only honor
# it in local mode (never follow a file:// redirect from the
# cloud API).
if not self.local:
logger.warning(
"Zotero: ignoring file:// redirect from the cloud API"
)
return None
return self._read_local_file(location)
if self._base.lower().startswith(
"https://"
) and not location.lower().startswith("https://"):
logger.warning(
f"Zotero: refusing attachment {attachment_key} redirect "
"that leaves HTTPS"
)
return None
return self._fetch_file_unauthenticated(location)
# Some configurations serve the file directly (200, no redirect).
content = response.content
return content or None
@staticmethod
def _read_local_file(file_url: str) -> Optional[bytes]:
"""Read bytes from a ``file://`` URL (local API attachment on disk).
Only PDF content is honored: the redirect target comes from whatever
answers on the local API port, so restricting reads to files with a
PDF magic number keeps this from acting as a generic local-file
disclosure primitive (defense in depth — the sync layer discards
non-PDF bodies anyway).
"""
from urllib.parse import urlparse
from urllib.request import url2pathname
try:
path = url2pathname(urlparse(file_url).path)
with open(path, "rb") as fh:
data = fh.read()
except OSError:
logger.warning("Zotero: could not read local attachment file")
return None
if not data:
return None
if b"%PDF" not in data[:1024]:
logger.warning("Zotero: local attachment is not a PDF; ignoring it")
return None
return data
def _fetch_file_unauthenticated(self, url: str) -> Optional[bytes]:
"""Fetch a storage URL with a fresh session that carries no Zotero
credentials. SafeSession SSRF-validates the URL (and any further
redirects) and refuses a further redirect from HTTPS to HTTP.
Raises :class:`ZoteroTransientError` on a transient failure (network
error or HTTP 429/5xx from the storage host) so the caller retries next
sync rather than importing the item metadata-only; returns None when
the session refuses the fetch (URL validation, an HTTPS-to-HTTP
redirect) and for a genuine "no file here" (other non-2xx / empty
body).
"""
try:
with SafeSession(block_https_downgrade=True) as session:
response = session.get(url, timeout=self.timeout)
except (
requests.exceptions.Timeout,
requests.exceptions.ConnectionError,
) as exc:
raise ZoteroTransientError(
f"Zotero storage fetch failed: {type(exc).__name__}"
) from exc
except Exception as exc:
logger.warning(
f"Zotero: storage download failed ({type(exc).__name__})"
)
return None
if response.status_code in (429, 500, 502, 503, 504):
raise ZoteroTransientError(
f"Zotero storage returned HTTP {response.status_code}"
)
if not (200 <= response.status_code < 300):
logger.warning(
f"Zotero: storage download returned HTTP {response.status_code}"
)
return None
content = response.content
return content or None
def get_attachment_fulltext(self, attachment_key: str) -> Optional[str]:
"""Return Zotero's already-extracted full text for an attachment.
Uses ``GET /items/{key}/fulltext``; returns None when Zotero has no
indexed text for it (HTTP 404) or the content is empty. Lets the sync
skip downloading + re-parsing the PDF in text-only mode.
"""
try:
response = self._request(
f"/items/{quote(attachment_key, safe='')}/fulltext"
)
except ZoteroTransientError:
# Temporary failure — don't conflate with "no indexed text"; let
# the caller retry next sync.
raise
except ZoteroError:
return None
try:
data = response.json()
except Exception:
return None
if not isinstance(data, dict):
return None
content = data.get("content")
return content or None