Claude commited on
Commit
55c0bf1
·
unverified ·
1 Parent(s): caf91fb

Validate patent_id shape before hitting Google Patents/OPS

Browse files

patent_id (both the /scrap and /ops path params, and scrap_patents_bulk's
list) was an unvalidated string interpolated straight into a Google
Patents/OPS URL. Not a host-controllable SSRF (the host is a fixed
literal), but garbage input still reached the upstream API and came back
as a confusing 404/502 instead of a clear client error.

- serp.py: split PATENT_ID_REGEX into an unanchored PATENT_ID_CORE (used
to find an id inside scraped text) and the existing bounded regex built
from it, so there's one definition of "what a patent id looks like"
instead of two that could drift.
- app.py: added a PatentId = Annotated[str, StringConstraints(pattern=...)]
built from that same core pattern, applied to both path params and
ScrapPatentsRequest.patent_ids. Malformed input now gets a 422 with a
clear message instead of reaching the upstream API.

Wrote the new tests first (expecting 422 on malformed input), confirmed
they failed against the unvalidated endpoints, then added the validation.
85 tests pass.

Files changed (3) hide show
  1. app.py +13 -7
  2. serp.py +8 -2
  3. tests/test_app.py +37 -0
app.py CHANGED
@@ -3,22 +3,28 @@ import logging
3
  import os
4
  import secrets
5
  from contextlib import asynccontextmanager
6
- from typing import Optional
7
  from fastapi import APIRouter, FastAPI, HTTPException, Request
8
  from fastapi.responses import JSONResponse
9
  from fastapi.routing import APIRouter
10
  import httpx
11
  from httpx import HTTPStatusError
12
- from pydantic import BaseModel, Field
13
  from playwright.async_api import async_playwright, Browser, BrowserContext, Page
14
  import uvicorn
15
 
16
  from scrap import PatentScrapBulkResponse, PatentScrapResult, scrap_patent_async, scrap_patent_bulk_async
17
- from serp import SerpQuery, SerpResults, query_arxiv, query_bing_search, query_brave_search, query_ddg_search, query_google_patents, query_google_scholar
18
  from ops import OPSBulkResponse, OPSNotConfigured, ops_scrap_patent, ops_scrap_patent_bulk, ops_search, token_manager as ops_token_manager
19
  from utils import log_gathered_exceptions
20
  from mcp_server import mount_mcp_server
21
 
 
 
 
 
 
 
22
  logging.basicConfig(
23
  level=logging.INFO,
24
  format='[%(asctime)s][%(levelname)s][%(filename)s:%(lineno)d]: %(message)s',
@@ -235,7 +241,7 @@ async def search(params: SerpQuery):
235
 
236
 
237
  @scrap_router.get("/scrap_patent/{patent_id}")
238
- async def scrap_patent(patent_id: str) -> PatentScrapResult:
239
  """Scraps the specified patent from Google Patents.
240
 
241
  Falls back to the EPO OPS API (which covers patents missing from Google
@@ -274,8 +280,8 @@ async def scrap_patent(patent_id: str) -> PatentScrapResult:
274
 
275
  class ScrapPatentsRequest(BaseModel):
276
  """Request model for scrapping multiple patents."""
277
- patent_ids: list[str] = Field(...,
278
- description="List of patent IDs to scrap")
279
 
280
 
281
  @scrap_router.post("/scrap_patents_bulk", response_model=PatentScrapBulkResponse)
@@ -296,7 +302,7 @@ async def ops_keyword_search(params: SerpQuery) -> SerpResults:
296
 
297
 
298
  @ops_router.get("/scrap_patent/{patent_id}")
299
- async def ops_get_patent(patent_id: str) -> PatentScrapResult:
300
  """Retrieves a patent (biblio + abstract + claims + description) via EPO OPS."""
301
  if not ops_token_manager.configured:
302
  raise HTTPException(
 
3
  import os
4
  import secrets
5
  from contextlib import asynccontextmanager
6
+ from typing import Annotated, Optional
7
  from fastapi import APIRouter, FastAPI, HTTPException, Request
8
  from fastapi.responses import JSONResponse
9
  from fastapi.routing import APIRouter
10
  import httpx
11
  from httpx import HTTPStatusError
12
+ from pydantic import BaseModel, Field, StringConstraints
13
  from playwright.async_api import async_playwright, Browser, BrowserContext, Page
14
  import uvicorn
15
 
16
  from scrap import PatentScrapBulkResponse, PatentScrapResult, scrap_patent_async, scrap_patent_bulk_async
17
+ from serp import PATENT_ID_CORE, SerpQuery, SerpResults, query_arxiv, query_bing_search, query_brave_search, query_ddg_search, query_google_patents, query_google_scholar
18
  from ops import OPSBulkResponse, OPSNotConfigured, ops_scrap_patent, ops_scrap_patent_bulk, ops_search, token_manager as ops_token_manager
19
  from utils import log_gathered_exceptions
20
  from mcp_server import mount_mcp_server
21
 
22
+ # Anchored version of serp.py's PATENT_ID_CORE: validates a whole
23
+ # user-supplied patent id rather than finding one inside free text. Rejects
24
+ # obviously-malformed input (e.g. containing "/" or "?") with a clean 422
25
+ # instead of it reaching Google Patents/OPS as a confusing request.
26
+ PatentId = Annotated[str, StringConstraints(pattern=rf"^{PATENT_ID_CORE}$")]
27
+
28
  logging.basicConfig(
29
  level=logging.INFO,
30
  format='[%(asctime)s][%(levelname)s][%(filename)s:%(lineno)d]: %(message)s',
 
241
 
242
 
243
  @scrap_router.get("/scrap_patent/{patent_id}")
244
+ async def scrap_patent(patent_id: PatentId) -> PatentScrapResult:
245
  """Scraps the specified patent from Google Patents.
246
 
247
  Falls back to the EPO OPS API (which covers patents missing from Google
 
280
 
281
  class ScrapPatentsRequest(BaseModel):
282
  """Request model for scrapping multiple patents."""
283
+ patent_ids: list[PatentId] = Field(...,
284
+ description="List of patent IDs to scrap")
285
 
286
 
287
  @scrap_router.post("/scrap_patents_bulk", response_model=PatentScrapBulkResponse)
 
302
 
303
 
304
  @ops_router.get("/scrap_patent/{patent_id}")
305
+ async def ops_get_patent(patent_id: PatentId) -> PatentScrapResult:
306
  """Retrieves a patent (biblio + abstract + claims + description) via EPO OPS."""
307
  if not ops_token_manager.configured:
308
  raise HTTPException(
serp.py CHANGED
@@ -124,8 +124,14 @@ async def query_google_scholar(browser: Browser, q: str, n_results: int = 10):
124
  return await _extract_google_scholar_results(page, n_results)
125
 
126
 
127
- # regex to locate a patent id, e.g. "US11930446B2" or "EP4760514A1"
128
- PATENT_ID_REGEX = r"\b[A-Z]{2}\d{6,}(?:[A-Z]\d?)?\b"
 
 
 
 
 
 
129
 
130
 
131
  async def _extract_google_patents_results(page: Page, n_results: int) -> list[dict]:
 
124
  return await _extract_google_scholar_results(page, n_results)
125
 
126
 
127
+ # A patent id's shape, e.g. "US11930446B2" or "EP4760514A1": a two-letter
128
+ # country code, at least 6 digits, and an optional kind-code suffix (a
129
+ # letter plus 0-2 digits). PATENT_ID_REGEX finds one inside free text (used
130
+ # to pull ids out of scraped search results); app.py reuses the same core
131
+ # pattern, anchored, to validate a user-supplied patent id - one definition
132
+ # so the two can't drift apart.
133
+ PATENT_ID_CORE = r"[A-Z]{2}\d{6,}(?:[A-Z]\d?)?"
134
+ PATENT_ID_REGEX = rf"\b{PATENT_ID_CORE}\b"
135
 
136
 
137
  async def _extract_google_patents_results(page: Page, n_results: int) -> list[dict]:
tests/test_app.py CHANGED
@@ -14,6 +14,7 @@ import pytest
14
  from httpx import ASGITransport
15
 
16
  import app as app_module
 
17
  from serp import SerpQuery
18
 
19
 
@@ -146,6 +147,42 @@ async def test_search_patents_no_fallback_when_ops_not_configured(client, monkey
146
  assert data["error"] is None
147
 
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  async def test_ops_keyword_search_flattens_results(client, monkeypatch):
150
  async def fake_ops_search(client_arg, q, n):
151
  return [{"title": f"OPS {q}", "body": "", "href": "", "id": "1"}]
 
14
  from httpx import ASGITransport
15
 
16
  import app as app_module
17
+ from scrap import PatentScrapResult
18
  from serp import SerpQuery
19
 
20
 
 
147
  assert data["error"] is None
148
 
149
 
150
+ # ------------------------------- patent_id validation -------------------------------
151
+
152
+
153
+ async def test_scrap_patent_rejects_malformed_patent_id(client):
154
+ resp = await client.get("/scrap/scrap_patent/not-a-patent-id")
155
+ assert resp.status_code == 422
156
+
157
+
158
+ async def test_scrap_patent_accepts_well_formed_patent_id(client, monkeypatch):
159
+ async def fake_scrap(client_arg, url):
160
+ return PatentScrapResult(title="Widget apparatus")
161
+
162
+ monkeypatch.setattr(app_module, "scrap_patent_async", fake_scrap)
163
+
164
+ resp = await client.get("/scrap/scrap_patent/US11930446B2")
165
+
166
+ assert resp.status_code == 200
167
+ assert resp.json()["title"] == "Widget apparatus"
168
+
169
+
170
+ async def test_ops_get_patent_rejects_malformed_patent_id(client):
171
+ resp = await client.get("/ops/scrap_patent/not-a-patent-id")
172
+ assert resp.status_code == 422
173
+
174
+
175
+ async def test_scrap_patents_bulk_rejects_malformed_id_in_list(client):
176
+ resp = await client.post(
177
+ "/scrap/scrap_patents_bulk", json={"patent_ids": ["US11930446B2", "garbage!!"]})
178
+ assert resp.status_code == 422
179
+
180
+
181
+ async def test_ops_scrap_patents_bulk_rejects_malformed_id_in_list(client):
182
+ resp = await client.post("/ops/scrap_patents_bulk", json={"patent_ids": ["garbage!!"]})
183
+ assert resp.status_code == 422
184
+
185
+
186
  async def test_ops_keyword_search_flattens_results(client, monkeypatch):
187
  async def fake_ops_search(client_arg, q, n):
188
  return [{"title": f"OPS {q}", "body": "", "href": "", "id": "1"}]