-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
1472 lines (1254 loc) · 56.6 KB
/
server.py
File metadata and controls
1472 lines (1254 loc) · 56.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
WebFetch MCP Server
Drop-in MCP replacement for built-in WebFetch tools, with support for domain-scoped
custom HTTP headers (e.g. provider-specific authentication tokens), retry logic,
configurable timeouts, per-domain proxies, and flexible output formats.
Configuration is loaded from a YAML file (WEBFETCH_CONFIG env var) or falls
back to the legacy WEBFETCH_HEADERS / WEBFETCH_OUTPUT environment variables.
"""
import asyncio
import datetime
import ipaddress
import json
import logging
import os
import re
import ssl
import time
from pathlib import Path
from urllib.parse import urlparse
import httpx
from dotenv import load_dotenv
from mcp.server.fastmcp import FastMCP
load_dotenv(Path(__file__).parent / ".env")
mcp = FastMCP("webfetch")
_log = logging.getLogger(__name__)
# Dedicated audit logger. Enabled by setting WEBFETCH_AUDIT_LOG=1 at startup.
# Each record is emitted as a single JSON line on the "webfetch.audit" logger,
# making it easy to pipe into a SIEM or structured log aggregator.
_audit_log = logging.getLogger("webfetch.audit")
_AUDIT_ENABLED: bool = os.getenv("WEBFETCH_AUDIT_LOG", "").strip() in ("1", "true", "yes")
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
_VALID_OUTPUT_FORMATS = frozenset({"raw", "markdown", "trafilatura", "json", "lighthtml"})
# Default response-body cap (10 MB). Prevents OOM when a remote server returns
# an unexpectedly large payload. Pass max_bytes=-1 to disable explicitly.
_DEFAULT_MAX_BYTES = 10 * 1024 * 1024
_VALID_SANITIZE_MODES = frozenset({"flag", "strip"})
_VALID_BOT_BLOCK_MODES = frozenset({"report", "retry"})
# Hard-coded fallback patterns (never overwritten).
# _INJECTION_PATTERNS is replaced at startup by _load_injection_patterns().
_INJECTION_PATTERNS_FALLBACK: list[re.Pattern] = [
re.compile(r"ignore\s+all\s+previous\s+instructions?", re.I),
re.compile(r"you\s+are\s+now\s+(?:a|an)\s+\w+", re.I),
re.compile(r"act\s+as\s+(?:a|an)\s+\w+", re.I),
re.compile(r"disregard\s+(?:all\s+)?(?:previous|prior)\s+instructions?", re.I),
re.compile(r"new\s+instructions?:\s*\n", re.I),
re.compile(r"system\s+prompt\s*:", re.I),
re.compile(r"<\|(?:system|user|assistant)\|>", re.I),
]
_INJECTION_PATTERNS: list[re.Pattern] = _INJECTION_PATTERNS_FALLBACK[:]
# Populated at startup by _load_injection_patterns(); parallel to _INJECTION_PATTERNS.
_INJECTION_PATTERN_METADATA: list[dict] = []
_PATTERNS_FILE = Path(__file__).parent / "patterns" / "prompt_injection.json"
def _load_injection_patterns() -> tuple[list[re.Pattern], list[dict]]:
"""Load prompt-injection patterns from patterns/prompt_injection.json.
Falls back to the 7 hardcoded patterns if the file is absent, unreadable,
or structurally invalid. Patterns whose 'enabled' field is false are skipped.
Returns (compiled_patterns, metadata_list) in lock-step order.
"""
fallback = _INJECTION_PATTERNS_FALLBACK[:]
try:
with open(_PATTERNS_FILE, encoding="utf-8") as fh:
raw = json.load(fh)
except FileNotFoundError:
_log.info("Injection pattern file not found (%s); using defaults", _PATTERNS_FILE)
return fallback, []
except (json.JSONDecodeError, OSError) as exc:
_log.warning("Cannot load injection patterns from %s: %s; using defaults", _PATTERNS_FILE, exc)
return fallback, []
entries = raw.get("patterns")
if not isinstance(entries, list):
_log.warning("Injection pattern file missing 'patterns' list; using defaults")
return fallback, []
flag_map = {"I": re.I, "M": re.M, "S": re.S, "X": re.X}
compiled: list[re.Pattern] = []
meta: list[dict] = []
for entry in entries:
if not isinstance(entry, dict):
continue
if not entry.get("enabled", True):
continue
pat_str = entry.get("pattern")
if not pat_str:
continue
flags = 0
for f in entry.get("flags", ["I"]):
flags |= flag_map.get(f.upper(), 0)
try:
compiled.append(re.compile(pat_str, flags))
meta.append(entry)
except re.error as exc:
_log.warning("Skipping invalid injection pattern %r: %s", pat_str, exc)
if not compiled:
_log.warning("No valid injection patterns loaded from file; using defaults")
return fallback, []
_log.info("Loaded %d injection patterns from %s", len(compiled), _PATTERNS_FILE)
return compiled, meta
_BOT_BLOCK_STATUS_CODES = frozenset({403, 429, 503})
_BOT_BLOCK_HEADER_SIGNALS = {"cf-ray", "cf-mitigated"}
_BOT_BLOCK_BODY_PATTERNS = [
re.compile(r"cloudflare", re.I),
re.compile(r"captcha", re.I),
re.compile(r"access\s+denied", re.I),
re.compile(r"please\s+verify", re.I),
re.compile(r"bot\s+detection", re.I),
re.compile(r"are\s+you\s+human", re.I),
]
_CHROME_UA = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/123.0.0.0 Safari/537.36"
)
_DEFAULT_GLOBAL: dict = {
"headers": {},
"output_format": "trafilatura",
"timeout": 30.0,
"retry": {"attempts": 1, "backoff": 2.0},
"proxy": None,
"extract_metadata": False,
"sanitize_content": False,
"bot_block_detection": False,
"css_selector": None,
"allowed_domains": [],
"denied_domains": [],
"tls_verify": True,
"tls_ca_bundle": None,
"tls_min_version": None,
"render_js": False,
}
# ---------------------------------------------------------------------------
# Audit logging
# ---------------------------------------------------------------------------
def _emit_audit_event(event: dict) -> None:
"""Emit a structured JSON audit record if audit logging is enabled.
Each call appends a UTC timestamp and logs a single JSON line to the
``webfetch.audit`` logger. Downstream operators can attach a file
handler or forward to a SIEM by configuring Python logging externally.
"""
if not _AUDIT_ENABLED:
return
event["ts"] = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
_audit_log.info(json.dumps(event, ensure_ascii=False))
# ---------------------------------------------------------------------------
# TLS helpers
# ---------------------------------------------------------------------------
_VALID_TLS_VERSIONS = {"1.2", "1.3"}
def _build_ssl_context(
ca_bundle: str | None,
min_version: str | None,
verify: bool,
) -> ssl.SSLContext | bool:
"""Build an ssl.SSLContext for httpx from TLS config settings.
Returns a configured ``ssl.SSLContext`` when any TLS option is set,
or the plain ``verify`` boolean otherwise (httpx default behaviour).
"""
if not verify:
_log.warning("TLS certificate verification is DISABLED — do not use in production")
return False
if ca_bundle is None and min_version is None:
return True # use httpx default (system trust store, no version pin)
ctx = ssl.create_default_context(cafile=ca_bundle)
if min_version == "1.2":
ctx.minimum_version = ssl.TLSVersion.TLSv1_2
elif min_version == "1.3":
ctx.minimum_version = ssl.TLSVersion.TLSv1_3
return ctx
# ---------------------------------------------------------------------------
# SSRF protection
# ---------------------------------------------------------------------------
# RFC-1918 private ranges, loopback, link-local (AWS metadata endpoint), and
# IPv6 loopback / unique-local. Requests resolving to any of these are blocked
# unless the operator explicitly configures an allowlist that includes the host.
_PRIVATE_IP_RANGES = [
ipaddress.ip_network("127.0.0.0/8"), # loopback IPv4
ipaddress.ip_network("10.0.0.0/8"), # RFC-1918 class A
ipaddress.ip_network("172.16.0.0/12"), # RFC-1918 class B
ipaddress.ip_network("192.168.0.0/16"), # RFC-1918 class C
ipaddress.ip_network("169.254.0.0/16"), # link-local / AWS metadata service
ipaddress.ip_network("0.0.0.0/8"), # "this" network
ipaddress.ip_network("::1/128"), # IPv6 loopback
ipaddress.ip_network("fc00::/7"), # IPv6 unique-local (fc00:: and fd00::)
ipaddress.ip_network("fe80::/10"), # IPv6 link-local
]
_ALLOWED_SCHEMES = frozenset({"http", "https"})
# Hostnames that are always blocked regardless of how they resolve.
_BLOCKED_HOSTNAMES = frozenset({"localhost", "ip6-localhost", "ip6-loopback"})
def _validate_url(url: str, allowed_domains: list, denied_domains: list) -> None:
"""Raise ValueError if *url* is disallowed.
Checks performed (in order):
1. URL scheme must be http or https.
2. Hostname must not be empty.
3. Hostname must not be in the blocked-hostname list.
4. If the hostname is a bare IP address it must not fall within any
private / reserved range.
5. If *denied_domains* is non-empty, the hostname must not match any entry.
6. If *allowed_domains* is non-empty, the hostname must match at least one
entry (suffix match, same logic as domain-header resolution).
"""
parsed = urlparse(url)
scheme = (parsed.scheme or "").lower()
if scheme not in _ALLOWED_SCHEMES:
raise ValueError(
f"Disallowed URL scheme {scheme!r}. Only http and https are permitted."
)
host = (parsed.hostname or "").lower()
if not host:
raise ValueError("URL has no hostname.")
if host in _BLOCKED_HOSTNAMES:
raise ValueError(f"Disallowed host {host!r}: loopback/localhost is not permitted.")
try:
addr = ipaddress.ip_address(host)
for net in _PRIVATE_IP_RANGES:
if addr in net:
raise ValueError(
f"Disallowed IP address {host!r}: falls within reserved range {net}."
)
except ValueError as exc:
if "Disallowed" in str(exc):
raise
# host is a domain name — not an IP literal; continue with domain checks
if denied_domains:
for entry in denied_domains:
entry_lower = entry.lower()
if host == entry_lower or host.endswith("." + entry_lower):
raise ValueError(
f"Host {host!r} is in the denied_domains list ({entry!r})."
)
if allowed_domains:
for entry in allowed_domains:
entry_lower = entry.lower()
if host == entry_lower or host.endswith("." + entry_lower):
return # host is explicitly allowed
raise ValueError(
f"Host {host!r} is not in the allowed_domains list."
)
# ---------------------------------------------------------------------------
# Config loading
# ---------------------------------------------------------------------------
def _load_config() -> dict:
"""
Load configuration from a YAML file or fall back to environment variables.
Priority:
1. Hardcoded defaults
2. Environment variables (WEBFETCH_HEADERS, WEBFETCH_OUTPUT) — legacy
3. YAML file pointed to by WEBFETCH_CONFIG — fully overrides env vars
Returns a dict with keys "global" and "domains".
"""
yaml_path_str = os.getenv("WEBFETCH_CONFIG", "")
if yaml_path_str:
return _load_yaml_config(yaml_path_str)
return _load_env_config()
def _load_yaml_config(path_str: str) -> dict:
"""Load and validate config from a YAML file."""
import yaml # lazy import: only needed when YAML config is used
path = Path(path_str)
if not path.exists():
raise RuntimeError(f"WEBFETCH_CONFIG file not found: {path}")
try:
with open(path, encoding="utf-8") as fh:
raw = yaml.safe_load(fh)
except yaml.YAMLError as exc:
raise RuntimeError(f"Invalid YAML in {path}: {exc}") from exc
if not isinstance(raw, dict):
raise RuntimeError(f"YAML config must be a mapping, got {type(raw).__name__}")
return _normalise_config(raw)
def _normalise_config(raw: dict) -> dict:
"""Validate and normalise a raw YAML/dict config into the canonical form."""
config: dict = {"global": dict(_DEFAULT_GLOBAL), "domains": {}}
config["global"]["retry"] = dict(_DEFAULT_GLOBAL["retry"])
raw_global = raw.get("global", {})
if not isinstance(raw_global, dict):
raise RuntimeError("'global' section must be a mapping")
_merge_domain_section(config["global"], raw_global, context="global")
raw_domains = raw.get("domains", {})
if not isinstance(raw_domains, dict):
raise RuntimeError("'domains' section must be a mapping")
for domain_key, domain_val in raw_domains.items():
if domain_val is None:
domain_val = {}
if not isinstance(domain_val, dict):
raise RuntimeError(f"Domain entry {domain_key!r} must be a mapping")
section: dict = {}
_merge_domain_section(section, domain_val, context=f"domains.{domain_key}")
config["domains"][domain_key] = section
return config
def _merge_domain_section(target: dict, source: dict, *, context: str) -> None:
"""Copy recognised fields from *source* into *target*, validating each."""
if "headers" in source:
val = source["headers"]
if not isinstance(val, dict):
raise RuntimeError(f"'{context}.headers' must be a mapping")
target["headers"] = {str(k).lower(): str(v) for k, v in val.items()}
if "output_format" in source:
val = source["output_format"]
if val not in _VALID_OUTPUT_FORMATS:
raise RuntimeError(
f"'{context}.output_format' is {val!r}; "
f"must be one of {sorted(_VALID_OUTPUT_FORMATS)}"
)
target["output_format"] = val
if "timeout" in source:
val = source["timeout"]
try:
target["timeout"] = float(val)
except (TypeError, ValueError):
raise RuntimeError(f"'{context}.timeout' must be a number, got {val!r}")
if "proxy" in source:
val = source["proxy"]
target["proxy"] = str(val) if val is not None else None
if "retry" in source:
val = source["retry"]
if not isinstance(val, dict):
raise RuntimeError(f"'{context}.retry' must be a mapping")
retry: dict = {}
if "attempts" in val:
try:
retry["attempts"] = int(val["attempts"])
except (TypeError, ValueError):
raise RuntimeError(
f"'{context}.retry.attempts' must be an integer"
)
if "backoff" in val:
try:
retry["backoff"] = float(val["backoff"])
except (TypeError, ValueError):
raise RuntimeError(
f"'{context}.retry.backoff' must be a number"
)
target["retry"] = retry
if "extract_metadata" in source:
val = source["extract_metadata"]
if not isinstance(val, bool):
raise RuntimeError(
f"'{context}.extract_metadata' must be a boolean, got {type(val).__name__!r}"
)
target["extract_metadata"] = val
if "sanitize_content" in source:
val = source["sanitize_content"]
if val is not False and val not in _VALID_SANITIZE_MODES:
raise RuntimeError(
f"'{context}.sanitize_content' is {val!r}; "
f"must be false or one of {sorted(_VALID_SANITIZE_MODES)}"
)
target["sanitize_content"] = val
if "bot_block_detection" in source:
val = source["bot_block_detection"]
if val is not False and val not in _VALID_BOT_BLOCK_MODES:
raise RuntimeError(
f"'{context}.bot_block_detection' is {val!r}; "
f"must be false or one of {sorted(_VALID_BOT_BLOCK_MODES)}"
)
target["bot_block_detection"] = val
if "render_js" in source:
val = source["render_js"]
if not isinstance(val, bool):
raise RuntimeError(
f"'{context}.render_js' must be a boolean, got {type(val).__name__!r}"
)
target["render_js"] = val
if "css_selector" in source:
val = source["css_selector"]
target["css_selector"] = str(val) if val is not None else None
if "tls_verify" in source:
val = source["tls_verify"]
if not isinstance(val, bool):
raise RuntimeError(
f"'{context}.tls_verify' must be a boolean, got {type(val).__name__!r}"
)
target["tls_verify"] = val
if "tls_ca_bundle" in source:
val = source["tls_ca_bundle"]
target["tls_ca_bundle"] = str(val) if val is not None else None
if "tls_min_version" in source:
val = source["tls_min_version"]
if val not in _VALID_TLS_VERSIONS:
raise RuntimeError(
f"'{context}.tls_min_version' is {val!r}; "
f"must be one of {sorted(_VALID_TLS_VERSIONS)}"
)
target["tls_min_version"] = val
for list_key in ("allowed_domains", "denied_domains"):
if list_key in source:
val = source[list_key]
if not isinstance(val, list) or not all(isinstance(x, str) for x in val):
raise RuntimeError(
f"'{context}.{list_key}' must be a list of strings, got {val!r}"
)
target[list_key] = [str(v).lower() for v in val]
def _load_env_config() -> dict:
"""Build the canonical config dict from legacy environment variables."""
config: dict = {
"global": {
"headers": {},
"output_format": "raw",
"timeout": 30.0,
"retry": {"attempts": 1, "backoff": 2.0},
"proxy": None,
},
"domains": {},
}
# --- WEBFETCH_HEADERS ---
raw_headers = os.getenv("WEBFETCH_HEADERS", "")
if raw_headers:
try:
header_cfg = json.loads(raw_headers)
if not isinstance(header_cfg, dict):
raise ValueError("WEBFETCH_HEADERS must be a JSON object")
except (json.JSONDecodeError, ValueError) as exc:
raise RuntimeError(f"Invalid WEBFETCH_HEADERS: {exc}") from exc
if "*" in header_cfg:
val = header_cfg["*"]
if not isinstance(val, dict):
raise RuntimeError("WEBFETCH_HEADERS: value for '*' must be a JSON object")
config["global"]["headers"] = {str(k).lower(): str(v) for k, v in val.items()}
for key, val in header_cfg.items():
if key == "*":
continue
if not isinstance(val, dict):
raise RuntimeError(f"WEBFETCH_HEADERS: value for {key!r} must be a JSON object")
config["domains"].setdefault(key, {})["headers"] = {str(k).lower(): str(v) for k, v in val.items()}
# --- WEBFETCH_OUTPUT ---
raw_output = os.getenv("WEBFETCH_OUTPUT", "")
if raw_output:
try:
output_cfg = json.loads(raw_output)
if not isinstance(output_cfg, dict):
raise ValueError("WEBFETCH_OUTPUT must be a JSON object")
except (json.JSONDecodeError, ValueError) as exc:
raise RuntimeError(f"Invalid WEBFETCH_OUTPUT: {exc}") from exc
for key, fmt in output_cfg.items():
if fmt not in _VALID_OUTPUT_FORMATS:
raise RuntimeError(
f"Invalid output format {fmt!r} for key {key!r}. "
f"Must be one of: {sorted(_VALID_OUTPUT_FORMATS)}"
)
if key == "*":
config["global"]["output_format"] = fmt
else:
config["domains"].setdefault(key, {})["output_format"] = fmt
# --- WEBFETCH_ALLOWED_DOMAINS / WEBFETCH_DENIED_DOMAINS ---
for env_key, cfg_key in (
("WEBFETCH_ALLOWED_DOMAINS", "allowed_domains"),
("WEBFETCH_DENIED_DOMAINS", "denied_domains"),
):
raw_domains_val = os.getenv(env_key, "")
if raw_domains_val:
entries = [d.strip().lower() for d in raw_domains_val.split(",") if d.strip()]
config["global"][cfg_key] = entries
# --- WEBFETCH_SELECTORS ---
raw_selectors = os.getenv("WEBFETCH_SELECTORS", "")
if raw_selectors:
try:
selector_cfg = json.loads(raw_selectors)
if not isinstance(selector_cfg, dict):
raise ValueError("WEBFETCH_SELECTORS must be a JSON object")
except (json.JSONDecodeError, ValueError) as exc:
raise RuntimeError(f"Invalid WEBFETCH_SELECTORS: {exc}") from exc
for key, sel in selector_cfg.items():
if key == "*":
config["global"]["css_selector"] = str(sel)
else:
config["domains"].setdefault(key, {})["css_selector"] = str(sel)
# --- WEBFETCH_RENDER_JS ---
raw_render_js = os.getenv("WEBFETCH_RENDER_JS", "").strip().lower()
if raw_render_js in ("1", "true", "yes"):
config["global"]["render_js"] = True
return config
# Load once at startup
try:
_CONFIG: dict = _load_config()
except RuntimeError as _startup_exc:
_log.critical("webfetch: fatal config error — %s", _startup_exc)
raise SystemExit(1) from _startup_exc
_log.info(
"webfetch startup: %d domain(s) configured (source: %s)",
len(_CONFIG["domains"]),
"YAML" if os.getenv("WEBFETCH_CONFIG") else "env",
)
_INJECTION_PATTERNS, _INJECTION_PATTERN_METADATA = _load_injection_patterns()
# ---------------------------------------------------------------------------
# Domain-matching resolution helpers
# ---------------------------------------------------------------------------
def _matching_domain_keys(hostname: str, domains: dict) -> list[str]:
"""Return domain keys that match *hostname*, sorted shortest-first."""
matches = [
key for key in domains
if hostname == key or hostname.endswith("." + key)
]
matches.sort(key=len)
return matches
def _resolve_headers(hostname: str, extra_headers: dict[str, str] | None) -> dict[str, str]:
"""
Build the final headers dict for a given hostname.
Merge order (later entries win):
1. Global headers
2. Most-specific matching domain headers
3. Per-call extra_headers
"""
headers: dict[str, str] = {}
headers.update(_CONFIG["global"].get("headers", {}))
for key in _matching_domain_keys(hostname, _CONFIG["domains"]):
headers.update(_CONFIG["domains"][key].get("headers", {}))
if extra_headers:
headers.update({k.lower(): v for k, v in extra_headers.items()})
return headers
def _resolve_output_format(hostname: str, per_call_format: str | None) -> str:
"""
Determine the effective output format.
Precedence (later wins):
1. Global config output_format
2. Most-specific matching domain output_format
3. per_call_format (None = don't override)
"""
fmt = _CONFIG["global"].get("output_format", "raw")
for key in _matching_domain_keys(hostname, _CONFIG["domains"]):
domain_fmt = _CONFIG["domains"][key].get("output_format")
if domain_fmt is not None:
fmt = domain_fmt
if per_call_format is not None:
fmt = per_call_format
return fmt
def _resolve_timeout(hostname: str) -> float:
"""Return the effective timeout (seconds) for *hostname*."""
timeout = _CONFIG["global"].get("timeout", 30.0)
for key in _matching_domain_keys(hostname, _CONFIG["domains"]):
domain_timeout = _CONFIG["domains"][key].get("timeout")
if domain_timeout is not None:
timeout = domain_timeout
return float(timeout)
def _resolve_proxy(hostname: str) -> str | None:
"""Return the effective proxy URL for *hostname*, or None."""
proxy = _CONFIG["global"].get("proxy")
for key in _matching_domain_keys(hostname, _CONFIG["domains"]):
domain = _CONFIG["domains"][key]
if "proxy" in domain:
proxy = domain["proxy"]
return proxy
def _resolve_retry(hostname: str) -> dict:
"""Return the effective retry config for *hostname*."""
global_retry = _CONFIG["global"].get("retry", {})
attempts = global_retry.get("attempts", 1)
backoff = global_retry.get("backoff", 2.0)
for key in _matching_domain_keys(hostname, _CONFIG["domains"]):
domain_retry = _CONFIG["domains"][key].get("retry", {})
if "attempts" in domain_retry:
attempts = domain_retry["attempts"]
if "backoff" in domain_retry:
backoff = domain_retry["backoff"]
return {"attempts": int(attempts), "backoff": float(backoff)}
def _resolve_extract_metadata(hostname: str) -> bool:
"""Return effective extract_metadata flag for *hostname*."""
val = _CONFIG["global"].get("extract_metadata", False)
for key in _matching_domain_keys(hostname, _CONFIG["domains"]):
domain = _CONFIG["domains"][key]
if "extract_metadata" in domain:
val = domain["extract_metadata"]
return bool(val)
def _resolve_sanitize_content(hostname: str) -> str | bool:
"""Return effective sanitize_content mode for *hostname* (False, 'flag', or 'strip')."""
val: str | bool = _CONFIG["global"].get("sanitize_content", False)
for key in _matching_domain_keys(hostname, _CONFIG["domains"]):
domain = _CONFIG["domains"][key]
if "sanitize_content" in domain:
val = domain["sanitize_content"]
return val
def _resolve_bot_block_detection(hostname: str) -> str | bool:
"""Return effective bot_block_detection mode for *hostname* (False, 'report', or 'retry')."""
val: str | bool = _CONFIG["global"].get("bot_block_detection", False)
for key in _matching_domain_keys(hostname, _CONFIG["domains"]):
domain = _CONFIG["domains"][key]
if "bot_block_detection" in domain:
val = domain["bot_block_detection"]
return val
def _resolve_tls_config(hostname: str) -> tuple[bool, str | None, str | None]:
"""Return (tls_verify, ca_bundle, min_version) for *hostname*."""
verify: bool = _CONFIG["global"].get("tls_verify", True)
ca_bundle: str | None = _CONFIG["global"].get("tls_ca_bundle")
min_version: str | None = _CONFIG["global"].get("tls_min_version")
for key in _matching_domain_keys(hostname, _CONFIG["domains"]):
domain = _CONFIG["domains"][key]
if "tls_verify" in domain:
verify = domain["tls_verify"]
if "tls_ca_bundle" in domain:
ca_bundle = domain["tls_ca_bundle"]
if "tls_min_version" in domain:
min_version = domain["tls_min_version"]
return verify, ca_bundle, min_version
def _resolve_allowed_denied_domains() -> tuple[list[str], list[str]]:
"""Return (allowed_domains, denied_domains) from global config."""
allowed: list[str] = _CONFIG["global"].get("allowed_domains", [])
denied: list[str] = _CONFIG["global"].get("denied_domains", [])
return allowed, denied
def _resolve_css_selector(hostname: str, per_call_selector: str | None) -> str | None:
"""Return the effective CSS selector for *hostname*, or None.
Precedence (later wins):
1. Global config css_selector
2. Most-specific matching domain css_selector
3. per_call_selector (None = don't override)
"""
val: str | None = _CONFIG["global"].get("css_selector")
for key in _matching_domain_keys(hostname, _CONFIG["domains"]):
domain = _CONFIG["domains"][key]
if "css_selector" in domain:
val = domain["css_selector"]
if per_call_selector is not None:
val = per_call_selector
return val
def _resolve_render_js(hostname: str, per_call: bool | None) -> bool:
"""Return True if headless browser rendering is enabled for *hostname*.
Precedence (later wins):
1. Global config render_js
2. Most-specific matching domain render_js
3. per_call (None = don't override)
"""
val: bool = _CONFIG["global"].get("render_js", False)
for key in _matching_domain_keys(hostname, _CONFIG["domains"]):
domain = _CONFIG["domains"][key]
if "render_js" in domain:
val = domain["render_js"]
if per_call is not None:
val = per_call
return bool(val)
# ---------------------------------------------------------------------------
# Output formatting
# ---------------------------------------------------------------------------
def _apply_output_format(content: str, fmt: str) -> str:
"""
Convert *content* (raw response body) to the requested output format.
Formats:
"raw" — return as-is
"text" — regex tag-strip (internal alias for extract_text=True)
"markdown" — convert full HTML to Markdown via markdownify
"trafilatura" — extract main content as Markdown via trafilatura;
falls back to raw if extraction fails
"json" — pretty-print JSON body; falls back to raw if not valid JSON
"""
if fmt == "raw":
return content
if fmt == "text":
return _extract_text(content)
if fmt == "markdown":
import markdownify # lazy import
return markdownify.markdownify(content, strip=["script", "style"])
if fmt == "trafilatura":
import trafilatura # lazy import
extracted = trafilatura.extract(content, output_format="markdown")
if extracted is None:
_log.warning("trafilatura returned None; falling back to raw HTML")
return content
return extracted
if fmt == "json":
try:
parsed = json.loads(content)
return json.dumps(parsed, indent=2, ensure_ascii=False)
except json.JSONDecodeError:
_log.warning("output_format=json but response is not valid JSON; returning raw")
return content
if fmt == "lighthtml":
return _apply_lighthtml(content)
_log.error("Unknown output format %r; returning raw content", fmt)
return content
def _apply_lighthtml(html: str) -> str:
"""
Return a lightweight version of *html* with all noise stripped.
Removes:
- <style> tags (and their content)
- Inline style attributes
- All <script> tags EXCEPT type="application/ld+json"
- HTML comments
- All tag attributes (class, id, data-*, aria-*, etc.)
JSON-LD <script> blocks are kept with only their ``type`` attribute.
Falls back to raw HTML if BeautifulSoup is unavailable or raises.
"""
try:
from bs4 import BeautifulSoup, Comment # lazy import
soup = BeautifulSoup(html, "html.parser")
# Remove <style> blocks
for tag in soup.find_all("style"):
tag.decompose()
# Remove <script> blocks except JSON-LD
for tag in soup.find_all("script"):
if str(tag.get("type", "")).lower() != "application/ld+json":
tag.decompose()
# Remove HTML comments
for comment in soup.find_all(string=lambda text: isinstance(text, Comment)):
comment.extract()
# Strip all attributes; restore type="application/ld+json" on kept scripts
for tag in soup.find_all(True):
if tag.name and tag.name.lower() == "script":
tag.attrs = {"type": "application/ld+json"}
else:
tag.attrs = {}
return str(soup)
except Exception as exc:
_log.warning("lighthtml apply failed (%s); returning raw HTML", exc)
return html
def _extract_text(html: str) -> str:
"""Strip HTML tags and collapse whitespace."""
text = re.sub(r"<script[^>]*>.*?</script>", " ", html, flags=re.DOTALL | re.I)
text = re.sub(r"<style[^>]*>.*?</style>", " ", text, flags=re.DOTALL | re.I)
text = re.sub(r"<!--.*?-->", " ", text, flags=re.DOTALL)
text = re.sub(r"<!\[CDATA\[.*?\]\]>", " ", text, flags=re.DOTALL)
text = re.sub(r"<[^>]+>", " ", text)
text = re.sub(r"\s+", " ", text)
return text.strip()
def _apply_css_selector(html: str, selector: str) -> tuple[str, bool]:
"""Extract elements matching *selector* from *html* and return them as HTML.
All matching elements are concatenated. Falls back to the original HTML
if no elements match or if BeautifulSoup raises an exception.
Returns a ``(html, matched)`` tuple where *matched* is True only when at
least one element was found and extracted.
"""
try:
from bs4 import BeautifulSoup # lazy import
soup = BeautifulSoup(html, "html.parser")
elements = soup.select(selector)
if not elements:
_log.warning("css_selector %r matched nothing; using full HTML", selector)
return html, False
return "\n".join(str(el) for el in elements), True
except Exception as exc:
_log.warning("css_selector apply failed (%s); using full HTML", exc)
return html, False
def _extract_trafilatura_metadata(raw_html: str) -> str | None:
"""Extract title/author/date/sitename from HTML via trafilatura.
Returns a formatted markdown block, or None if no metadata was found.
"""
try:
import trafilatura # lazy import
meta = trafilatura.extract_metadata(raw_html)
if meta is None:
return None
parts = []
if meta.title:
parts.append(f"**Title:** {meta.title}")
if meta.author:
parts.append(f"**Author:** {meta.author}")
if meta.date:
parts.append(f"**Date:** {meta.date}")
if meta.sitename:
parts.append(f"**Source:** {meta.sitename}")
return "\n".join(parts) if parts else None
except Exception as exc:
_log.warning("trafilatura metadata extraction failed: %s", exc)
return None
def _sanitize_content(content: str, mode: str) -> tuple[str, list[str]]:
"""Scan *content* for prompt-injection patterns.
Returns ``(content, matched_patterns)`` where *content* is optionally
modified (in ``"strip"`` mode) and *matched_patterns* lists the regex
patterns that fired.
Detection is performed against a NFKD-normalized copy of the content so
that Unicode homoglyphs (e.g. Cyrillic 'і' vs Latin 'i') and zero-width
characters do not bypass pattern matching. In 'strip' mode the substitution
is applied to the original (non-normalised) content to preserve legitimate
accented characters.
"""
import unicodedata
content_norm = unicodedata.normalize("NFKD", content)
matched: list[str] = []
for pat in _INJECTION_PATTERNS:
if pat.search(content_norm):
matched.append(pat.pattern)
if mode == "strip":
content = pat.sub("[REMOVED]", content)
return content, matched
def _severity_for_pattern(pattern_str: str) -> str:
"""Return the severity level for a matched pattern string.
Looks up *pattern_str* in *_INJECTION_PATTERN_METADATA*.
Returns ``'medium'`` as the default when metadata is unavailable
(e.g. fallback mode) or when the severity field is absent.
"""
for entry in _INJECTION_PATTERN_METADATA:
if entry.get("pattern") == pattern_str:
return entry.get("severity", "medium")
return "medium"
def _detect_bot_block(status_code: int, resp_headers: dict, body: str) -> str | None:
"""Return a reason string if a bot-block or paywall is detected, else None.
Checks status code, response headers, and the first 8 KB of the body.
"""
reasons: list[str] = []
if status_code in _BOT_BLOCK_STATUS_CODES:
reasons.append(f"HTTP {status_code}")
for sig in _BOT_BLOCK_HEADER_SIGNALS:
if sig in resp_headers:
reasons.append(f"header:{sig}")
snippet = body[:8192]
for pat in _BOT_BLOCK_BODY_PATTERNS:
if pat.search(snippet):
reasons.append(f"body:{pat.pattern!r}")
break # one body signal is sufficient
return ", ".join(reasons) if reasons else None
# ---------------------------------------------------------------------------
# Header validation
# ---------------------------------------------------------------------------
_INVALID_HEADER_RE = re.compile(r"[\r\n\x00]")
# Headers that must not be overridden by callers to prevent HTTP request
# smuggling and host-header injection attacks.
_FORBIDDEN_HEADERS = frozenset({
"host",
"content-length",
"transfer-encoding",
"connection",
"upgrade",
"te",
"trailers",
})
def _validate_headers(headers: dict[str, str]) -> None:
"""Raise ValueError if any header is forbidden or contains control characters."""
for name, value in headers.items():
if name.lower() in _FORBIDDEN_HEADERS:
raise ValueError(
f"Header {name!r} cannot be set by callers "
f"(potential HTTP request smuggling vector)."
)
if _INVALID_HEADER_RE.search(name):
raise ValueError(f"Invalid header name contains control character: {name!r}")
if _INVALID_HEADER_RE.search(str(value)):
raise ValueError(f"Invalid value for header {name!r} contains control character")
# ---------------------------------------------------------------------------
# Headless browser fetch
# ---------------------------------------------------------------------------
async def _fetch_with_browser(