deapi-mcp 0.7.0__tar.gz → 0.7.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/CHANGELOG.md +64 -0
  2. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/PKG-INFO +3 -1
  3. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/README.md +2 -0
  4. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/auth_code_store.py +39 -18
  5. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/config.py +22 -4
  6. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/consent.py +76 -22
  7. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/deapi_client.py +21 -4
  8. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/fastmcp_auth.py +29 -4
  9. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/key_verifier.py +19 -4
  10. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/oauth_clients.py +114 -14
  11. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/oauth_endpoints.py +16 -9
  12. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/polling_manager.py +4 -1
  13. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/server_remote.py +14 -1
  14. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/tools/image.py +23 -11
  15. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/tools/video.py +12 -3
  16. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/utils.py +108 -13
  17. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/webhooks.py +2 -2
  18. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/pyproject.toml +1 -1
  19. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/.gitignore +0 -0
  20. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/LICENSE +0 -0
  21. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/SECURITY.md +0 -0
  22. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/__init__.py +0 -0
  23. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/auth.py +0 -0
  24. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/landing.py +0 -0
  25. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/middleware.py +0 -0
  26. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/observability.py +0 -0
  27. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/py.typed +0 -0
  28. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/schemas.py +0 -0
  29. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/tools/__init__.py +0 -0
  30. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/tools/_price_helpers.py +0 -0
  31. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/tools/_responses.py +0 -0
  32. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/tools/audio.py +0 -0
  33. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/tools/embedding.py +0 -0
  34. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/tools/prompt.py +0 -0
  35. {deapi_mcp-0.7.0 → deapi_mcp-0.7.1}/deapi_mcp/tools/utility.py +0 -0
@@ -3,6 +3,70 @@
3
3
  All notable changes to this project. Versions follow [Semantic Versioning](https://semver.org/);
4
4
  releases are git tags `vX.Y.Z`.
5
5
 
6
+ ## 0.7.1
7
+
8
+ Security hardening after a review of 0.7.0. No known exploit; nothing changes for a client
9
+ that worked before, except that media uploads now stop at deAPI's own limits.
10
+
11
+ ### Security
12
+ - A client ID metadata document (or a media URL) served compressed could make the server
13
+ inflate far more than the size cap, since the body was decompressed a network chunk at a
14
+ time before the cap was checked. Fetched bodies are now read raw: `Accept-Encoding:
15
+ identity` is sent and a compressed answer is refused, never inflated.
16
+ - A media fetch has an overall deadline (four times the per-read timeout), so a server that
17
+ trickles bytes cannot hold it open; proxy settings from the environment are ignored, so
18
+ the connection always goes to the address that was checked; IPv6 site-local addresses and
19
+ IPv4 addresses wrapped in 6to4 or Teredo are judged like the address inside.
20
+ - Media uploads stop at deAPI's limits: an image at 10 MB, audio at 20 MB, video at 50 MB,
21
+ and one tool call's files together at `DEAPI_MAX_UPLOAD_MB` (new, default 75, deAPI's own
22
+ request cap; it also caps each file). List parameters (`images`, `ref_images`,
23
+ `ref_videos`, `ref_audios`) take at most 16 entries. Before, one call could make the
24
+ server hold several hundred megabytes.
25
+ - The authorization codes of the sign-in screen are kept apart from those of the
26
+ pre-registered client, each with a cap of 10,000 (was one shared cap of 1,000). Anyone can
27
+ have a pre-registered code issued, so a flood of them could push out a code a person had
28
+ just signed in for.
29
+ - A redirect URI's host must be a plain DNS name (ASCII, an IDN in `xn--` form) or an IP
30
+ literal, without user info; the same holds for a metadata document's host. A `;` in the
31
+ host could add a directive to the sign-in page's CSP header, and a host outside Latin-1
32
+ failed with a 500. Look-alike hosts in other scripts cannot appear on the sign-in screen.
33
+ An IPv6 zone (`%...`) is refused too. A dynamically registered `client_id` issued by 0.7.0
34
+ with a redirect URI these rules now refuse stops working; that client registers again.
35
+ - Wrong-key limit on the sign-in screen: an attempt is counted before the key is checked and
36
+ given back when the key is good, so concurrent attempts no longer get past the limit; an
37
+ IPv6 address counts as its /64. The sign-in screen has key-check slots of its own, so a
38
+ flood of keys on `/token` or `/mcp` cannot make it answer "could not be checked".
39
+ - Metadata documents: one fetch per `client_id` at a time (everyone signing in with that
40
+ client meanwhile shares it), and at most 4 at once for one site (waiting up to 2 seconds
41
+ for a slot), so one party's slow hosts cannot take every fetch slot.
42
+ - Only requests to `/mcp` have their bearer token checked with deAPI. Authentication runs on
43
+ every route, so a token sent elsewhere (a metrics scraper's, for example) used to be sent
44
+ to deAPI as if it were an API key.
45
+ - The shared HTTP client keeps no cookies. It serves every user's requests, so a cookie set
46
+ on one user's response would have been sent with the next user's.
47
+ - A key that is not printable ASCII is refused without a check (it was let through with a
48
+ token, because the check itself failed).
49
+ - A sign-in code is refused at `/token` when deAPI has refused its key since it was issued.
50
+ - `check_job_status` sends the job id as one path segment (`../`, `?` and `#` are escaped).
51
+ - A status read that fails (a 404 for another account's job id, an API error) no longer stops
52
+ the internal webhook from waking the call that is waiting on that job.
53
+ - Two log lines quote the client's `response_type` and `code_challenge_method` with `repr`,
54
+ so they cannot forge a log line. A failure to create tokens no longer returns its error
55
+ message to the client, and an unexpected error while verifying a token logs only its type.
56
+ - `DEAPI_WEBHOOK_SECRET` is a `SecretStr`: it no longer appears in a `repr` of the settings.
57
+ Read it with `config.secret_value()`.
58
+ - A warning at startup when `DEAPI_JWT_SECRET_KEY` is shorter than 32 characters or an
59
+ example value.
60
+
61
+ ### Upgrade notes
62
+ - Sign-in codes move to their own Redis keys (`<prefix>signin_oauth_code:*`). During a
63
+ rolling upgrade from 0.7.0, a sign-in finished on one version and redeemed on the other
64
+ fails with `invalid_grant`; the client signs in again. Codes live 10 minutes.
65
+ - A media URL whose server sends the body compressed (for example an object stored with
66
+ `Content-Encoding: gzip`) is refused instead of inflated.
67
+ - Redirect hosts with an underscore are refused: a CSP host source cannot name them, so the
68
+ sign-in form could never have redirected there anyway.
69
+
6
70
  ## 0.7.0
7
71
 
8
72
  ### Added
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: deapi-mcp
3
- Version: 0.7.0
3
+ Version: 0.7.1
4
4
  Summary: Production-ready MCP server for the deAPI REST API
5
5
  Project-URL: Homepage, https://deapi.ai
6
6
  Project-URL: Documentation, https://docs.deapi.ai/execution-modes-and-integrations/mcp-server
@@ -342,6 +342,8 @@ DEAPI_API_VERSION=v2
342
342
 
343
343
  # HTTP Client
344
344
  DEAPI_HTTP_TIMEOUT=30.0
345
+ # Media one tool call uploads, in MB (all its files; deAPI's own request cap is 75 MB)
346
+ DEAPI_MAX_UPLOAD_MB=75
345
347
  DEAPI_MAX_RETRIES=3
346
348
 
347
349
  # Seconds one tool call waits before returning done=false and the job_id
@@ -296,6 +296,8 @@ DEAPI_API_VERSION=v2
296
296
 
297
297
  # HTTP Client
298
298
  DEAPI_HTTP_TIMEOUT=30.0
299
+ # Media one tool call uploads, in MB (all its files; deAPI's own request cap is 75 MB)
300
+ DEAPI_MAX_UPLOAD_MB=75
299
301
  DEAPI_MAX_RETRIES=3
300
302
 
301
303
  # Seconds one tool call waits before returning done=false and the job_id
@@ -13,7 +13,7 @@ import hashlib
13
13
  import json
14
14
  import logging
15
15
  import time
16
- from typing import Any, Protocol, cast
16
+ from typing import Any, Literal, Protocol, cast
17
17
 
18
18
  import redis.asyncio as aioredis
19
19
  from redis.asyncio.retry import Retry
@@ -193,15 +193,27 @@ def build_redis_client(
193
193
  return aioredis.Redis(**kwargs)
194
194
 
195
195
 
196
- _store: AuthCodeStore | None = None
196
+ # Two pools, each with its own cap. Anyone can fill the default pool (the pre-registered
197
+ # client gets a code on a bare GET /authorize); a sign-in code needs a key deAPI accepted.
198
+ # Kept apart, a flood of the first never evicts a code a user just signed in for.
199
+ CodePool = Literal["default", "sign_in"]
197
200
 
198
- AUTHORIZATION_CODE_MAX_ENTRIES = 1000
201
+ AUTHORIZATION_CODE_MAX_ENTRIES = 10000
202
+ SIGN_IN_CODE_MAX_ENTRIES = 10000
199
203
 
204
+ _stores: dict[str, AuthCodeStore] = {}
205
+ _redis: aioredis.Redis | None = None
200
206
 
201
- def get_auth_code_store() -> AuthCodeStore:
202
- global _store
203
- if _store is None:
204
- if settings.redis_host:
207
+
208
+ def get_auth_code_store(pool: CodePool = "default") -> AuthCodeStore:
209
+ global _redis
210
+ store = _stores.get(pool)
211
+ if store is not None:
212
+ return store
213
+ cap = SIGN_IN_CODE_MAX_ENTRIES if pool == "sign_in" else AUTHORIZATION_CODE_MAX_ENTRIES
214
+ prefix = settings.redis_prefix + ("signin_" if pool == "sign_in" else "")
215
+ if settings.redis_host:
216
+ if _redis is None:
205
217
  logger.info(
206
218
  "OAuth code store: Redis %s:%s db=%s prefix=%s",
207
219
  settings.redis_host,
@@ -209,18 +221,27 @@ def get_auth_code_store() -> AuthCodeStore:
209
221
  settings.redis_db,
210
222
  settings.redis_prefix,
211
223
  )
212
- _store = RedisAuthCodeStore(
213
- build_redis_client(settings), settings.redis_prefix, AUTHORIZATION_CODE_MAX_ENTRIES
214
- )
215
- else:
224
+ _redis = build_redis_client(settings)
225
+ store = RedisAuthCodeStore(_redis, prefix, cap)
226
+ else:
227
+ if not _stores:
216
228
  logger.warning(
217
229
  "OAuth code store: in-memory (DEAPI_REDIS_HOST unset). Authorization codes are "
218
230
  "per-process, so this is only safe for a single-replica deployment"
219
231
  )
220
- _store = InMemoryAuthCodeStore(AUTHORIZATION_CODE_MAX_ENTRIES)
221
- return _store
222
-
223
-
224
- def reset_auth_code_store(store: AuthCodeStore | None = None) -> None:
225
- global _store
226
- _store = store
232
+ store = InMemoryAuthCodeStore(cap)
233
+ _stores[pool] = store
234
+ return store
235
+
236
+
237
+ def reset_auth_code_store(
238
+ store: AuthCodeStore | None = None, sign_in: AuthCodeStore | None = None
239
+ ) -> None:
240
+ """Replace the stores (tests); None means build the default again on next use."""
241
+ global _redis
242
+ _stores.clear()
243
+ _redis = None
244
+ if store is not None:
245
+ _stores["default"] = store
246
+ if sign_in is not None:
247
+ _stores["sign_in"] = sign_in
@@ -6,6 +6,11 @@ from pydantic import AliasChoices, BaseModel, Field, SecretStr, field_validator,
6
6
  from pydantic_settings import BaseSettings, SettingsConfigDict
7
7
 
8
8
 
9
+ def secret_value(value: SecretStr | str | None) -> str | None:
10
+ """The plain text of a secret setting (a plain string is taken as it is)."""
11
+ return value.get_secret_value() if isinstance(value, SecretStr) else value
12
+
13
+
9
14
  class PollingConfig(BaseModel):
10
15
  """Configuration for smart adaptive polling based on job type."""
11
16
 
@@ -85,6 +90,15 @@ class Settings(BaseSettings):
85
90
  description="Seconds one tool call waits for its job before returning the job_id to resume with",
86
91
  )
87
92
 
93
+ # Media a tool call uploads, in megabytes: the files of one call together, and the cap
94
+ # on any single file. deAPI refuses a request body over 75 MB, so more is never useful;
95
+ # lower it to bound the memory one call can take.
96
+ max_upload_mb: int = Field(
97
+ default=75,
98
+ ge=1,
99
+ description="Most media one tool call uploads, in MB (all its files together)",
100
+ )
101
+
88
102
  # Plain API key check (deapi_mcp/key_verifier.py). A key sent as a bearer token
89
103
  # or as the OAuth client_secret is checked once against the API (cached), so a
90
104
  # wrong key is refused with 401 at connect instead of failing every tool call.
@@ -136,9 +150,12 @@ class Settings(BaseSettings):
136
150
  default=None,
137
151
  description="Public HTTPS URL of this server's /webhooks/deapi route, as the API must reach it",
138
152
  )
139
- webhook_secret: str | None = Field(
153
+ webhook_secret: SecretStr | None = Field(
140
154
  default=None,
141
- description="Key the API signs callbacks with (HMAC-SHA256). 32-255 characters",
155
+ description=(
156
+ "Key the API signs callbacks with (HMAC-SHA256). 32-255 characters. A SecretStr "
157
+ "so it never reaches a repr; read it with secret_value()"
158
+ ),
142
159
  )
143
160
  webhook_fallback_poll_interval: float = Field(
144
161
  default=15.0,
@@ -228,9 +245,10 @@ class Settings(BaseSettings):
228
245
  raise ValueError("DEAPI_WEBHOOKS_INTERNAL is set but DEAPI_WEBHOOK_RECEIVER_URL is not")
229
246
  if not self.webhook_receiver_url.lower().startswith("https://"):
230
247
  raise ValueError("DEAPI_WEBHOOK_RECEIVER_URL must use https (the API requires it)")
231
- if not self.webhook_secret:
248
+ secret = secret_value(self.webhook_secret)
249
+ if not secret:
232
250
  raise ValueError("DEAPI_WEBHOOKS_INTERNAL is set but DEAPI_WEBHOOK_SECRET is not")
233
- if not 32 <= len(self.webhook_secret) <= 255:
251
+ if not 32 <= len(secret) <= 255:
234
252
  raise ValueError("DEAPI_WEBHOOK_SECRET must be 32-255 characters (API rule)")
235
253
  return self
236
254
 
@@ -24,6 +24,7 @@ The key is never logged and never echoed back into the page.
24
24
  import base64
25
25
  import hashlib
26
26
  import hmac
27
+ import ipaddress
27
28
  import json
28
29
  import logging
29
30
  import os
@@ -129,9 +130,37 @@ def verify_request(token: Optional[str]) -> Optional[dict[str, Any]]:
129
130
  # --- failed key attempts -----------------------------------------------------------------------
130
131
 
131
132
 
133
+ _REFUND_SCRIPT = """
134
+ local v = redis.call('GET', KEYS[1])
135
+ if v and tonumber(v) > 0 then return redis.call('DECR', KEYS[1]) end
136
+ return 0
137
+ """
138
+
139
+
140
+ def _limit_subject(ip: str) -> str:
141
+ """Who an attempt is counted against: the address, or its /64 for IPv6.
142
+
143
+ One IPv6 subscriber usually holds a whole /64, so counting single addresses would
144
+ give them as many tries as they have addresses.
145
+ """
146
+ try:
147
+ address = ipaddress.ip_address(ip)
148
+ except ValueError:
149
+ return ip
150
+ if isinstance(address, ipaddress.IPv6Address):
151
+ if address.ipv4_mapped is not None:
152
+ return str(address.ipv4_mapped)
153
+ return str(ipaddress.IPv6Network((address, 64), strict=False))
154
+ return str(address)
155
+
156
+
132
157
  class KeyFailureLimiter:
133
158
  """Counts failed key attempts per client IP; Redis when configured, else this process.
134
159
 
160
+ An attempt is counted before the key is checked (``attempt``) and given back when the
161
+ key turns out good or could not be checked (``refund``), so a burst of concurrent
162
+ wrong keys cannot all get checked before the limit sees them.
163
+
135
164
  A Redis error falls back to the in-process count, so the sign-in screen keeps
136
165
  working (with a per-replica limit) while Redis is away.
137
166
  """
@@ -139,23 +168,35 @@ class KeyFailureLimiter:
139
168
  def __init__(self) -> None:
140
169
  self._local: dict[str, tuple[int, float]] = {}
141
170
  self._redis: Any = None
171
+ self._refund_script: Any = None
142
172
  if settings.redis_host:
143
173
  self._redis = build_redis_client(settings)
174
+ self._refund_script = self._redis.register_script(_REFUND_SCRIPT)
144
175
 
145
176
  @staticmethod
146
177
  def _bucket(ip: str) -> str:
147
- return hashlib.sha256(ip.encode()).hexdigest()[:32]
178
+ return hashlib.sha256(_limit_subject(ip).encode()).hexdigest()[:32]
148
179
 
149
180
  def _redis_key(self, ip: str) -> str:
150
181
  return f"{settings.redis_prefix}oauth_key_fail:{self._bucket(ip)}"
151
182
 
152
183
  def _local_count(self, ip: str) -> int:
153
- count, until = self._local.get(ip, (0, 0.0))
184
+ subject = _limit_subject(ip)
185
+ count, until = self._local.get(subject, (0, 0.0))
154
186
  if until <= time.time():
155
- self._local.pop(ip, None)
187
+ self._local.pop(subject, None)
156
188
  return 0
157
189
  return count
158
190
 
191
+ def _local_add(self, ip: str, delta: int) -> int:
192
+ subject = _limit_subject(ip)
193
+ count = max(0, self._local_count(ip) + delta)
194
+ until = self._local.get(subject, (0, time.time() + KEY_FAILURE_WINDOW))[1]
195
+ self._local[subject] = (count, until)
196
+ if len(self._local) > 10_000:
197
+ self._local.pop(next(iter(self._local)))
198
+ return count
199
+
159
200
  async def blocked(self, ip: str) -> bool:
160
201
  if self._redis is not None:
161
202
  try:
@@ -165,22 +206,33 @@ class KeyFailureLimiter:
165
206
  logger.warning("Key attempt limiter: Redis unavailable, counting in memory")
166
207
  return self._local_count(ip) >= MAX_KEY_FAILURES
167
208
 
168
- async def record_failure(self, ip: str) -> None:
209
+ async def record_failure(self, ip: str) -> int:
210
+ """Count one attempt against ``ip``; the count afterwards."""
169
211
  if self._redis is not None:
170
212
  try:
171
213
  key = self._redis_key(ip)
172
214
  async with self._redis.pipeline(transaction=True) as pipe:
173
215
  pipe.set(key, 0, ex=KEY_FAILURE_WINDOW, nx=True)
174
216
  pipe.incr(key)
175
- await pipe.execute()
217
+ _, count = await pipe.execute()
218
+ return int(count)
219
+ except Exception:
220
+ logger.warning("Key attempt limiter: Redis unavailable, counting in memory")
221
+ return self._local_add(ip, 1)
222
+
223
+ async def attempt(self, ip: str) -> bool:
224
+ """Count an attempt before the key is checked; False when over the limit."""
225
+ return await self.record_failure(ip) <= MAX_KEY_FAILURES
226
+
227
+ async def refund(self, ip: str) -> None:
228
+ """Give back an attempt whose key was good or could not be checked."""
229
+ if self._redis is not None:
230
+ try:
231
+ await self._refund_script(keys=[self._redis_key(ip)])
176
232
  return
177
233
  except Exception:
178
234
  logger.warning("Key attempt limiter: Redis unavailable, counting in memory")
179
- count = self._local_count(ip)
180
- until = self._local.get(ip, (0, time.time() + KEY_FAILURE_WINDOW))[1]
181
- self._local[ip] = (count + 1, until)
182
- if len(self._local) > 10_000:
183
- self._local.pop(next(iter(self._local)))
235
+ self._local_add(ip, -1)
184
236
 
185
237
 
186
238
  _limiter: Optional[KeyFailureLimiter] = None
@@ -468,16 +520,12 @@ async def submit_consent(request: Request) -> Response:
468
520
  signed = raw_request if isinstance(raw_request, str) else ""
469
521
  ip = _client_ip(request)
470
522
  limiter = get_limiter()
523
+ too_many = (
524
+ "Too many attempts with a wrong key. Wait a few minutes and try again.",
525
+ 429,
526
+ )
471
527
  if await limiter.blocked(ip):
472
- page = consent_page(
473
- client,
474
- redirect_uri,
475
- signed,
476
- csrf_cookie,
477
- "Too many attempts with a wrong key. Wait a few minutes and try again.",
478
- status_code=429,
479
- )
480
- return page
528
+ return consent_page(client, redirect_uri, signed, csrf_cookie, *too_many)
481
529
 
482
530
  key_value = form.get("api_key")
483
531
  api_key = key_value.strip() if isinstance(key_value, str) else ""
@@ -486,11 +534,17 @@ async def submit_consent(request: Request) -> Response:
486
534
  client, redirect_uri, signed, csrf_cookie, "Enter your deAPI API key.", 400
487
535
  )
488
536
 
537
+ # Counted before the check, given back unless deAPI refuses the key
538
+ if not await limiter.attempt(ip):
539
+ return consent_page(client, redirect_uri, signed, csrf_cookie, *too_many)
489
540
  status = (
490
- await get_key_verifier().verify(api_key) if settings.verify_api_keys else KeyStatus.VALID
541
+ await get_key_verifier().verify(api_key, sign_in=True)
542
+ if settings.verify_api_keys
543
+ else KeyStatus.VALID
491
544
  )
545
+ if status is not KeyStatus.INVALID:
546
+ await limiter.refund(ip)
492
547
  if status is KeyStatus.INVALID:
493
- await limiter.record_failure(ip)
494
548
  return consent_page(
495
549
  client,
496
550
  redirect_uri,
@@ -511,7 +565,7 @@ async def submit_consent(request: Request) -> Response:
511
565
 
512
566
  code = secrets.token_urlsafe(32)
513
567
  try:
514
- stored = await auth_code_store.get_auth_code_store().put(
568
+ stored = await auth_code_store.get_auth_code_store("sign_in").put(
515
569
  code,
516
570
  {
517
571
  "client_id": client.client_id,
@@ -1,7 +1,9 @@
1
1
  """HTTP client for deAPI with authentication forwarding and retry logic."""
2
2
 
3
+ import http.cookiejar
3
4
  from collections.abc import Mapping, Sequence
4
5
  from typing import IO, Any, Dict, Optional, Tuple, Union, cast
6
+ from urllib.parse import quote
5
7
 
6
8
  import httpx
7
9
 
@@ -14,7 +16,7 @@ from deapi_sdk._config import ClientConfig
14
16
  from deapi_sdk._exceptions import DeapiError as SdkError
15
17
  from deapi_sdk._exceptions import NetworkError as SdkNetworkError
16
18
 
17
- from .config import settings
19
+ from .config import secret_value, settings
18
20
  from .observability import note_api_error, note_network_error
19
21
  from .schemas import (
20
22
  BalanceResponse,
@@ -141,10 +143,24 @@ def webhook_payload_fields(
141
143
  _shared_http: Optional[httpx.AsyncClient] = None
142
144
 
143
145
 
146
+ class _NoCookies(http.cookiejar.CookieJar):
147
+ """A cookie jar that never keeps a cookie."""
148
+
149
+ def set_cookie(self, cookie: http.cookiejar.Cookie) -> None:
150
+ return None
151
+
152
+ def extract_cookies(self, response: Any, request: Any) -> None:
153
+ return None
154
+
155
+
144
156
  def _shared_http_client() -> httpx.AsyncClient:
145
157
  global _shared_http
146
158
  if _shared_http is None or _shared_http.is_closed:
147
- _shared_http = httpx.AsyncClient(timeout=httpx.Timeout(settings.http_timeout))
159
+ # No cookie jar: this one client serves every user, so a cookie one user's response
160
+ # set must never be sent with another user's request
161
+ _shared_http = httpx.AsyncClient(
162
+ timeout=httpx.Timeout(settings.http_timeout), cookies=_NoCookies()
163
+ )
148
164
  return _shared_http
149
165
 
150
166
 
@@ -305,7 +321,7 @@ class DeapiClient:
305
321
  if use_internal:
306
322
  webhook_fields = {
307
323
  "webhook_url": settings.webhook_receiver_url,
308
- "webhook_secret": settings.webhook_secret,
324
+ "webhook_secret": secret_value(settings.webhook_secret),
309
325
  }
310
326
 
311
327
  if webhook_fields:
@@ -343,7 +359,8 @@ class DeapiClient:
343
359
  """
344
360
  response_data = await self._request(
345
361
  method="GET",
346
- endpoint=f"jobs/{job_id}",
362
+ # Quoted whole: an id is one path segment, never "../" or a query
363
+ endpoint=f"jobs/{quote(job_id, safe='')}",
347
364
  )
348
365
  return JobStatusResponse(**response_data)
349
366
 
@@ -6,7 +6,8 @@ JWT/Bearer token authentication into the MCP server.
6
6
 
7
7
  import json
8
8
  import re
9
- from typing import Any
9
+ from contextvars import ContextVar
10
+ from typing import Any, Optional
10
11
 
11
12
  from fastmcp.server.auth import AccessToken, AuthProvider
12
13
  from starlette.routing import Route
@@ -21,6 +22,13 @@ from .oauth_endpoints import (
21
22
  )
22
23
 
23
24
  API_KEY_HEADER = b"x-api-key"
25
+ MCP_PATH_PREFIX = "/mcp"
26
+
27
+ # Whether the current request is for the MCP endpoint, set by ApiKeyHeaderMiddleware.
28
+ # Authentication runs on every route, but only /mcp takes a deAPI key: a bearer token sent
29
+ # elsewhere (a metrics scraper's, say) is never checked with deAPI. None: not set (no
30
+ # middleware in front), so every token is checked as before.
31
+ _mcp_request: ContextVar[Optional[bool]] = ContextVar("deapi_mcp_request", default=None)
24
32
 
25
33
 
26
34
  class ApiKeyHeaderMiddleware:
@@ -31,13 +39,20 @@ class ApiKeyHeaderMiddleware:
31
39
  ``Authorization: Bearer <key>`` before authentication runs, so the key goes
32
40
  through exactly the same checks (``DeapiAuthProvider.verify_token``). A request
33
41
  that carries both keeps its ``Authorization`` header untouched.
42
+
43
+ It also notes whether the request is for /mcp at all, so a token sent to any
44
+ other route is never taken for a deAPI key.
34
45
  """
35
46
 
36
47
  def __init__(self, app: Any) -> None:
37
48
  self.app = app
38
49
 
39
50
  async def __call__(self, scope: Any, receive: Any, send: Any) -> None:
40
- if scope["type"] == "http" and scope.get("path", "").startswith("/mcp"):
51
+ if scope["type"] != "http":
52
+ await self.app(scope, receive, send)
53
+ return
54
+ on_mcp = scope.get("path", "").startswith(MCP_PATH_PREFIX)
55
+ if on_mcp:
41
56
  headers = scope.get("headers") or []
42
57
  if not any(name == b"authorization" for name, _ in headers):
43
58
  key = next((value for name, value in headers if name == API_KEY_HEADER), b"")
@@ -45,7 +60,11 @@ class ApiKeyHeaderMiddleware:
45
60
  if key:
46
61
  scope = dict(scope)
47
62
  scope["headers"] = [*headers, (b"authorization", b"Bearer " + key)]
48
- await self.app(scope, receive, send)
63
+ marker = _mcp_request.set(on_mcp)
64
+ try:
65
+ await self.app(scope, receive, send)
66
+ finally:
67
+ _mcp_request.reset(marker)
49
68
 
50
69
 
51
70
  # Shown to the user by clients that surface error_description. No double quotes: it sits
@@ -163,6 +182,11 @@ class DeapiAuthProvider(AuthProvider):
163
182
  logger.error("No Authorization token provided")
164
183
  return None
165
184
 
185
+ if _mcp_request.get() is False:
186
+ # Not the MCP endpoint: no route here takes a deAPI key, so the token is left
187
+ # alone (not sent to deAPI, not counted as a rejection)
188
+ return None
189
+
166
190
  try:
167
191
  deapi_token = token # Default: use token directly
168
192
 
@@ -213,7 +237,8 @@ class DeapiAuthProvider(AuthProvider):
213
237
  )
214
238
 
215
239
  except Exception as e:
216
- logger.error(f"Token verification failed: {str(e)}")
240
+ # The type only: a message could quote part of the token
241
+ logger.error("Token verification failed: %s", type(e).__name__)
217
242
  record_auth_rejected("error")
218
243
  return None
219
244
 
@@ -18,7 +18,10 @@ per-process random secret, so the raw key is never held as a cache key:
18
18
  of the server; the verdict is cached for ``UNKNOWN_TTL`` so an outage does not
19
19
  add a probe to every request.
20
20
 
21
- Concurrent requests carrying the same key share one probe. Turn the whole check
21
+ Concurrent requests carrying the same key share one probe. A key that is not
22
+ printable ASCII cannot be sent in a header at all and is invalid without a probe.
23
+ The sign-in screen has probe slots of its own, so a flood of keys on /token or /mcp
24
+ never leaves a person signing in with "could not be checked". Turn the whole check
22
25
  off with ``DEAPI_VERIFY_API_KEYS=false``.
23
26
  """
24
27
 
@@ -27,6 +30,7 @@ import enum
27
30
  import hashlib
28
31
  import hmac
29
32
  import logging
33
+ import re
30
34
  import secrets
31
35
  import time
32
36
  from collections import OrderedDict
@@ -55,6 +59,11 @@ MAX_ENTRIES = 10_000
55
59
  # (the API then refuses each tool call), as before this check existed, instead of
56
60
  # turning the server into an amplifier of requests to the API.
57
61
  MAX_CONCURRENT_PROBES = 32
62
+ # Extra probes only the sign-in screen may use, on top of MAX_CONCURRENT_PROBES. It has a
63
+ # per-IP wrong-key limit of its own, so these cannot be flooded the same way.
64
+ RESERVED_SIGN_IN_PROBES = 8
65
+ # What a key can be: it travels in an HTTP header, so printable ASCII without spaces.
66
+ _KEY_SHAPE = re.compile(r"[!-~]+")
58
67
 
59
68
  # Monotonic clock for verdict expiry; a seam the tests replace.
60
69
  _clock = time.monotonic
@@ -130,8 +139,13 @@ class KeyVerifier:
130
139
  """True when a recent verdict says deAPI refuses ``api_key``. Never probes."""
131
140
  return self._cached(self._cache_key(api_key)) is KeyStatus.INVALID
132
141
 
133
- async def verify(self, api_key: str) -> KeyStatus:
134
- """The key's status, from the cache or from one probe shared by every caller."""
142
+ async def verify(self, api_key: str, *, sign_in: bool = False) -> KeyStatus:
143
+ """The key's status, from the cache or from one probe shared by every caller.
144
+
145
+ ``sign_in`` lets the call use the probe slots kept for the sign-in screen.
146
+ """
147
+ if not _KEY_SHAPE.fullmatch(api_key):
148
+ return KeyStatus.INVALID
135
149
  cache_key = self._cache_key(api_key)
136
150
  cached = self._cached(cache_key)
137
151
  if cached is not None:
@@ -139,7 +153,8 @@ class KeyVerifier:
139
153
 
140
154
  probe = self._inflight.get(cache_key)
141
155
  if probe is None:
142
- if len(self._inflight) >= MAX_CONCURRENT_PROBES:
156
+ limit = MAX_CONCURRENT_PROBES + (RESERVED_SIGN_IN_PROBES if sign_in else 0)
157
+ if len(self._inflight) >= limit:
143
158
  logger.warning("API key check: too many checks in flight; letting a key through")
144
159
  return KeyStatus.UNKNOWN
145
160
  probe = asyncio.ensure_future(self._probe_and_store(cache_key, api_key))