zarqa 0.1.0b0__py3-none-any.whl

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 (90) hide show
  1. zarqa/__init__.py +3 -0
  2. zarqa/__main__.py +3 -0
  3. zarqa/access.py +137 -0
  4. zarqa/auth.py +438 -0
  5. zarqa/brand/__init__.py +1 -0
  6. zarqa/brand/permute.py +130 -0
  7. zarqa/brand/watch.py +320 -0
  8. zarqa/bridges/__init__.py +17 -0
  9. zarqa/bridges/common.py +148 -0
  10. zarqa/bridges/openapi.py +169 -0
  11. zarqa/bridges/server.py +109 -0
  12. zarqa/bridges/taxii.py +185 -0
  13. zarqa/bridges/web.py +109 -0
  14. zarqa/cli.py +524 -0
  15. zarqa/collect/__init__.py +1 -0
  16. zarqa/collect/collector.py +326 -0
  17. zarqa/collect/fetch.py +187 -0
  18. zarqa/collect/stix_in.py +125 -0
  19. zarqa/config.example.yaml +227 -0
  20. zarqa/config.py +341 -0
  21. zarqa/connectors/__init__.py +6 -0
  22. zarqa/connectors/audit.py +131 -0
  23. zarqa/connectors/client.py +294 -0
  24. zarqa/connectors/hub.py +293 -0
  25. zarqa/connectors/oauth.py +237 -0
  26. zarqa/connectors/policy.py +115 -0
  27. zarqa/connectors/ratelimit.py +26 -0
  28. zarqa/connectors/spec.py +184 -0
  29. zarqa/connectors/untrusted.py +29 -0
  30. zarqa/db.py +341 -0
  31. zarqa/decode.py +150 -0
  32. zarqa/demo/__init__.py +1 -0
  33. zarqa/demo/data.py +47 -0
  34. zarqa/demo/setup.py +70 -0
  35. zarqa/doctor.py +127 -0
  36. zarqa/intel/attack.py +111 -0
  37. zarqa/intel/attack_data.json +1 -0
  38. zarqa/intel/enrich.py +256 -0
  39. zarqa/intel/extract.py +230 -0
  40. zarqa/intel/injection.py +111 -0
  41. zarqa/intel/model_extract.py +227 -0
  42. zarqa/intel/pipeline.py +134 -0
  43. zarqa/intel/relevance.py +175 -0
  44. zarqa/intel/service.py +83 -0
  45. zarqa/intel/store.py +441 -0
  46. zarqa/investigate/__init__.py +1 -0
  47. zarqa/investigate/engine.py +237 -0
  48. zarqa/investigate/service.py +31 -0
  49. zarqa/investigate/store.py +92 -0
  50. zarqa/investigate/tools.py +120 -0
  51. zarqa/jobs.py +358 -0
  52. zarqa/jsonparse.py +75 -0
  53. zarqa/llm.py +28 -0
  54. zarqa/mcp_server.py +104 -0
  55. zarqa/paths.py +26 -0
  56. zarqa/privacy.py +210 -0
  57. zarqa/providers/__init__.py +193 -0
  58. zarqa/providers/anthropic.py +65 -0
  59. zarqa/providers/base.py +141 -0
  60. zarqa/providers/cli.py +86 -0
  61. zarqa/providers/demo.py +101 -0
  62. zarqa/providers/gemini.py +230 -0
  63. zarqa/providers/ollama.py +78 -0
  64. zarqa/providers/openai_compat.py +87 -0
  65. zarqa/reporting.py +162 -0
  66. zarqa/scheduler.py +60 -0
  67. zarqa/server.py +81 -0
  68. zarqa/share/__init__.py +1 -0
  69. zarqa/share/misp.py +63 -0
  70. zarqa/share/push.py +74 -0
  71. zarqa/share/stix_out.py +187 -0
  72. zarqa/share/taxii_server.py +115 -0
  73. zarqa/static/app.css +67 -0
  74. zarqa/static/app.js +447 -0
  75. zarqa/static/img/logo.svg +16 -0
  76. zarqa/static/mdi/LICENSE +20 -0
  77. zarqa/static/mdi/README.md +5 -0
  78. zarqa/static/mdi/css/icons.css +71 -0
  79. zarqa/static/mdi/fonts/icons.woff2 +0 -0
  80. zarqa/tasks.py +70 -0
  81. zarqa/templates/index.html +31 -0
  82. zarqa/templates/login.html +19 -0
  83. zarqa/web.py +495 -0
  84. zarqa-0.1.0b0.dist-info/METADATA +164 -0
  85. zarqa-0.1.0b0.dist-info/RECORD +90 -0
  86. zarqa-0.1.0b0.dist-info/WHEEL +5 -0
  87. zarqa-0.1.0b0.dist-info/entry_points.txt +2 -0
  88. zarqa-0.1.0b0.dist-info/licenses/LICENSE +202 -0
  89. zarqa-0.1.0b0.dist-info/licenses/NOTICE +17 -0
  90. zarqa-0.1.0b0.dist-info/top_level.txt +1 -0
zarqa/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ """Zarqa: an open-source, AI-powered threat intelligence tool."""
2
+
3
+ __version__ = "0.1.0-beta"
zarqa/__main__.py ADDED
@@ -0,0 +1,3 @@
1
+ from .cli import cli_main
2
+
3
+ cli_main()
zarqa/access.py ADDED
@@ -0,0 +1,137 @@
1
+ """Who is asking, and which data sources they may use and see.
2
+
3
+ A connector can list who may use it::
4
+
5
+ connectors:
6
+ - name: mail
7
+ roles: [email]
8
+ access: [insider-threat, "role:admin", "user:jdoe"] # group names, role:<role>, user:<name>
9
+
10
+ No ``access`` (the default) means everyone. Admins can use every connector.
11
+
12
+ The caller is held in a context variable: the web guard sets it per request,
13
+ and jobs started from a request inherit it (the job runner copies the context),
14
+ so an investigation runs with the permissions of whoever started it. Scheduled and
15
+ command-line work has no caller and sees everything.
16
+
17
+ An investigation that called a connector with ``access`` is *restricted* to the people
18
+ who may use those connectors: other people cannot list or open it, its lead,
19
+ reports, chat or exports, and it is left out of other people's memory.
20
+ """
21
+
22
+ from __future__ import annotations
23
+
24
+ import contextvars
25
+ from dataclasses import dataclass, field
26
+
27
+ ROLE_RANK = {"viewer": 0, "operator": 1, "admin": 2}
28
+
29
+
30
+ @dataclass(frozen=True)
31
+ class Access:
32
+ user: str = ""
33
+ role: str = "viewer"
34
+ groups: tuple[str, ...] = ()
35
+ connectors: frozenset[str] | None = None # None: every connector
36
+ restricted_names: frozenset[str] = field(default_factory=frozenset)
37
+
38
+ def to_dict(self) -> dict:
39
+ return {"user": self.user, "role": self.role, "groups": list(self.groups),
40
+ "connectors": None if self.connectors is None else sorted(self.connectors),
41
+ "restricted_names": sorted(self.restricted_names)}
42
+
43
+ @classmethod
44
+ def from_dict(cls, d: dict) -> Access:
45
+ c = d.get("connectors")
46
+ return cls(user=str(d.get("user", "")), role=str(d.get("role", "viewer")), groups=tuple(d.get("groups") or ()),
47
+ connectors=None if c is None else frozenset(c),
48
+ restricted_names=frozenset(d.get("restricted_names") or ()))
49
+
50
+ def public(self) -> dict:
51
+ return {"user": self.user, "role": self.role, "groups": list(self.groups),
52
+ "connectors": None if self.connectors is None else sorted(self.connectors)}
53
+
54
+
55
+ _current: contextvars.ContextVar[Access | None] = contextvars.ContextVar("zarqa_access", default=None)
56
+
57
+
58
+ def current() -> Access | None:
59
+ return _current.get()
60
+
61
+
62
+ def set_current(a: Access | None):
63
+ return _current.set(a)
64
+
65
+
66
+ def reset(token) -> None:
67
+ _current.reset(token)
68
+
69
+
70
+ def matches(entry: str, user: str, role: str, groups) -> bool:
71
+ e = str(entry).strip()
72
+ if e.startswith("role:"):
73
+ want = e[5:].strip().lower()
74
+ return want in ROLE_RANK and ROLE_RANK.get(role, -1) >= ROLE_RANK[want]
75
+ if e.startswith("user:"):
76
+ return bool(user) and e[5:].strip().lower() == str(user).lower()
77
+ return e in set(groups or ())
78
+
79
+
80
+ def build(specs, *, user: str, role: str, groups=()) -> Access:
81
+ """The Access for this caller over these connector specs (an iterable of ConnectorSpec)."""
82
+ specs = list(specs)
83
+ restricted = frozenset(s.name for s in specs if getattr(s, "access", None))
84
+ groups = tuple(sorted({str(g) for g in groups or () if str(g).strip()}))
85
+ if role == "admin" or not restricted:
86
+ return Access(user=user, role=role, groups=groups, connectors=None, restricted_names=restricted)
87
+ allowed = frozenset(s.name for s in specs
88
+ if not getattr(s, "access", None) or any(matches(e, user, role, groups) for e in s.access))
89
+ return Access(user=user, role=role, groups=groups, connectors=allowed, restricted_names=restricted)
90
+
91
+
92
+ def can_use(connector: str) -> bool:
93
+ a = current()
94
+ return a is None or a.connectors is None or connector in a.connectors
95
+
96
+
97
+ def can_see(restricted) -> bool:
98
+ """May the current caller see a record that used these restricted connectors?"""
99
+ a = current()
100
+ if a is None or a.connectors is None or not restricted:
101
+ return True
102
+ return set(restricted) <= set(a.connectors)
103
+
104
+
105
+ def scheduler_access(cfg) -> Access | None:
106
+ """The identity the scheduler's own work (feed pulls, watches, scheduled investigations) runs as.
107
+
108
+ scheduler:
109
+ groups: [intel-automation] # connector access lists are matched against these
110
+ role: operator
111
+
112
+ Without a ``scheduler`` section it runs with no person behind it and may use every connector
113
+ (what it reads from restricted sources is then restricted like any investigation). Saved investigations always
114
+ run as the person who saved them."""
115
+ s = ((getattr(cfg, "raw", None) or {}).get("scheduler") or {})
116
+ if not s:
117
+ return None
118
+ from .connectors.spec import parse_connectors
119
+ try:
120
+ specs = parse_connectors(list(getattr(cfg, "connectors", None) or []))
121
+ except Exception:
122
+ specs = []
123
+ role = str(s.get("role", "operator")).lower()
124
+ if role not in ROLE_RANK:
125
+ raise ValueError("scheduler.role must be viewer, operator or admin")
126
+ groups = s.get("groups") or []
127
+ if not isinstance(groups, list):
128
+ raise ValueError("scheduler.groups must be a list")
129
+ return build(specs, user=str(s.get("user", "scheduler")), role=role, groups=groups)
130
+
131
+
132
+ def run_as(a: Access | None, fn, *args, **kw):
133
+ tok = set_current(a)
134
+ try:
135
+ return fn(*args, **kw)
136
+ finally:
137
+ reset(tok)
zarqa/auth.py ADDED
@@ -0,0 +1,438 @@
1
+ """Authentication for Zarqa.
2
+
3
+ Modes (config.auth.mode):
4
+ none - no login (default; dev / behind a trusted network)
5
+ basic - HTTP Basic (simple single operator)
6
+ local - built-in login page + signed session cookie
7
+ oidc - SSO via any OpenID Connect provider. Requires the optional `authlib` extra.
8
+ proxy - trust a reverse-proxy forward-auth header (e.g. X-Forwarded-User). Zarqa must be reachable ONLY via that proxy.
9
+
10
+ Passwords: store a pbkdf2 hash (auth.password_hash) in preference to a plaintext
11
+ ZARQA_AUTH_PASSWORD. Sessions are signed with ZARQA_SECRET_KEY.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import hashlib
17
+ import hmac
18
+ import html
19
+ import logging
20
+ import secrets
21
+ import time
22
+ from urllib.parse import urlparse
23
+
24
+ from flask import Response, g, redirect, render_template, request, session
25
+
26
+ log = logging.getLogger("zarqa.auth")
27
+
28
+ _PBKDF2_ITERS = 600_000
29
+ _OPEN_PATHS = {"/login", "/auth/callback", "/logout", "/healthz"}
30
+ _LOGIN_BUCKETS: dict[str, list[float]] = {}
31
+ _LOGIN_MAX_ATTEMPTS = 10
32
+ _LOGIN_WINDOW_SEC = 60.0
33
+ _VALID_ROLES = {"viewer", "operator", "admin"}
34
+ _ADMIN_ONLY_PATHS = {"/api/config", "/api/config/preview", "/api/settings/llm", "/api/settings/llm/test", "/api/retention",
35
+ "/api/settings/connectors"}
36
+ _ADMIN_ONLY_PREFIXES = ("/api/connectors/",)
37
+ # Reads that are NOT safe for every role (full database export) are admin-only.
38
+ _ADMIN_ONLY_READ_PATHS = {"/api/backup/download", "/api/audit", "/api/audit/verify", "/api/doctor"}
39
+
40
+
41
+ _RANK = {"viewer": 0, "operator": 1, "admin": 2}
42
+
43
+
44
+ def user_groups(cfg, user: str, extra=()) -> list[str]:
45
+ """Groups from the identity provider (``extra``) plus any listed for a named account."""
46
+ users = getattr(cfg.auth, "users", None) or {}
47
+ own = (users.get(user) or {}).get("groups") or [] if user else []
48
+ out = [str(g).strip() for g in [*(extra or []), *own] if str(g).strip()]
49
+ return sorted(set(out))[:200]
50
+
51
+
52
+ def parse_groups(value) -> list[str]:
53
+ """A claim or header value: a list, or a comma/semicolon separated string."""
54
+ if isinstance(value, (list, tuple)):
55
+ return [str(v) for v in value]
56
+ if isinstance(value, str):
57
+ return [v.strip() for v in value.replace(";", ",").split(",") if v.strip()]
58
+ return []
59
+
60
+
61
+ def group_role(cfg, groups) -> str:
62
+ """Highest role mapped from the user's groups by auth.group_roles, or ''."""
63
+ gr = getattr(cfg.auth, "group_roles", None) or {}
64
+ roles = [gr[g] for g in groups or () if gr.get(g) in _VALID_ROLES]
65
+ return max(roles, key=lambda r: _RANK[r]) if roles else ""
66
+
67
+
68
+ def resolve_user_role(cfg, user: str, groups=()) -> str:
69
+ """Resolve the effective RBAC role ('viewer', 'operator', or 'admin') for an
70
+ authenticated user. Order: the named account's role, auth.roles, the highest role
71
+ from auth.group_roles, the proxy role header, auth.default_role. Returns '' when
72
+ auth.require_group_role is on and none of the user's groups has a role."""
73
+ users = getattr(cfg.auth, "users", None) or {}
74
+ if user and user in users and str(users[user].get("role", "")).lower() in _VALID_ROLES:
75
+ return str(users[user]["role"]).lower()
76
+ roles_map = getattr(cfg.auth, "roles", None) or {}
77
+ if user and user in roles_map:
78
+ r = str(roles_map[user]).lower()
79
+ if r in _VALID_ROLES:
80
+ return r
81
+ gr = group_role(cfg, groups)
82
+ if gr:
83
+ return gr
84
+ if getattr(cfg.auth, "group_roles", None) and getattr(cfg.auth, "require_group_role", False):
85
+ return ""
86
+ from flask import has_request_context
87
+ hdr_role = (request.headers.get("X-ZARQA-Role") or "").strip().lower() if has_request_context() else ""
88
+ if cfg.auth.mode == "proxy" and hdr_role in _VALID_ROLES:
89
+ return hdr_role
90
+ default_role = str(getattr(cfg.auth, "default_role", "admin") or "admin").lower()
91
+ return default_role if default_role in _VALID_ROLES else "admin"
92
+
93
+
94
+ def check_rbac_permission(role: str, method: str, path: str, args=None) -> tuple[bool, str]:
95
+ """Enforce 3-tier RBAC:
96
+ - viewer: read-only GET/HEAD/OPTIONS + stateless PDF export (/api/report/pdf)
97
+ - operator: can run investigations and jobs; cannot change settings or connectors
98
+ - admin: full control
99
+ """
100
+ r = (role or "admin").lower()
101
+ m = (method or "GET").upper()
102
+ if path == "/mcp":
103
+ return True, "" # tool-level role checks happen in the MCP handler
104
+ args = args or {}
105
+ # Admin-only reads: checked BEFORE the general "GET is read-only" rule.
106
+ sensitive_read = path in _ADMIN_ONLY_READ_PATHS or (
107
+ path == "/api/doctor" and str(args.get("runner", "")).lower() in ("1", "true", "yes"))
108
+ if sensitive_read and r != "admin":
109
+ return False, f"Forbidden: {path} is restricted to the admin role."
110
+ if m in ("GET", "HEAD", "OPTIONS") or path in _OPEN_PATHS or path == "/api/report/pdf":
111
+ return True, ""
112
+ if r == "viewer":
113
+ return False, "Forbidden: role 'viewer' is read-only and cannot start investigations or modify state."
114
+ if r == "operator" and (path in _ADMIN_ONLY_PATHS or (m == "POST" and path.startswith(_ADMIN_ONLY_PREFIXES))):
115
+ return False, f"Forbidden: role 'operator' cannot modify administrative endpoint {path}."
116
+ return True, ""
117
+
118
+
119
+ def _check_login_rate_limit(ip: str) -> bool:
120
+ """Return True if the IP is within the login rate limit, False if throttled."""
121
+ now = time.monotonic()
122
+ bucket = [t for t in _LOGIN_BUCKETS.get(ip, []) if now - t < _LOGIN_WINDOW_SEC]
123
+ if len(bucket) >= _LOGIN_MAX_ATTEMPTS:
124
+ _LOGIN_BUCKETS[ip] = bucket
125
+ return False
126
+ bucket.append(now)
127
+ _LOGIN_BUCKETS[ip] = bucket
128
+ return True
129
+
130
+
131
+ def _origin_matches_request(cfg) -> bool:
132
+ """Validate Origin/Referer header on state-changing cookie-authenticated requests."""
133
+ origin = request.headers.get("Origin") or ""
134
+ if not origin:
135
+ return True
136
+ try:
137
+ o_host = urlparse(origin).netloc.lower()
138
+ except Exception:
139
+ return False
140
+ allowed_hosts = {request.host.lower()}
141
+ if cfg.external_url:
142
+ try:
143
+ allowed_hosts.add(urlparse(cfg.external_url).netloc.lower())
144
+ except Exception:
145
+ pass
146
+ return bool(o_host and o_host in allowed_hosts)
147
+
148
+
149
+ # ---- password hashing ----
150
+ def hash_password(pw: str, iters: int = _PBKDF2_ITERS) -> str:
151
+ salt = secrets.token_bytes(16)
152
+ dk = hashlib.pbkdf2_hmac("sha256", pw.encode(), salt, iters)
153
+ return f"pbkdf2_sha256${iters}${salt.hex()}${dk.hex()}"
154
+
155
+
156
+ def _check_hash(stored: str, pw: str) -> bool:
157
+ try:
158
+ algo, iters, salt_hex, hash_hex = stored.split("$", 3)
159
+ if algo != "pbkdf2_sha256":
160
+ return False
161
+ dk = hashlib.pbkdf2_hmac("sha256", pw.encode(), bytes.fromhex(salt_hex), int(iters))
162
+ return hmac.compare_digest(dk.hex(), hash_hex)
163
+ except Exception:
164
+ return False
165
+
166
+
167
+ _VERIFIED: dict[str, float] = {} # digest(user, password, stored hash) -> expiry; avoids re-hashing per request
168
+ _VERIFIED_TTL = 300.0
169
+
170
+
171
+ def verify_user(cfg, user: str, pw: str) -> bool:
172
+ """Named accounts in auth.users first, then the single auth.username account.
173
+
174
+ Successful checks are remembered for 5 minutes as a digest of (user, password, stored hash), so
175
+ HTTP Basic requests do not each pay for a full PBKDF2 run. Changing the password changes the digest.
176
+ """
177
+ users = getattr(cfg.auth, "users", None) or {}
178
+ stored = str(users[user].get("password_hash", "")) if user in users else (cfg.auth.password_hash or "")
179
+ key = hashlib.sha256(f"{user}\0{pw}\0{stored}".encode()).hexdigest()
180
+ now = time.monotonic()
181
+ if _VERIFIED.get(key, 0) > now:
182
+ return True
183
+ if user in users:
184
+ ok = _check_hash(stored, pw)
185
+ else:
186
+ ok = hmac.compare_digest(user or "", cfg.auth.username) and _verify_password(cfg, pw)
187
+ if ok:
188
+ if len(_VERIFIED) > 1000:
189
+ _VERIFIED.clear()
190
+ _VERIFIED[key] = now + _VERIFIED_TTL
191
+ return ok
192
+
193
+
194
+ def _verify_password(cfg, pw: str) -> bool:
195
+ if cfg.auth.password_hash:
196
+ return _check_hash(cfg.auth.password_hash, pw)
197
+ if cfg.auth.password:
198
+ return hmac.compare_digest(pw, cfg.auth.password)
199
+ return False
200
+
201
+
202
+ def _user_allowed(cfg, user: str) -> bool:
203
+ allow = cfg.auth.allowed_users
204
+ return (not allow) or (user in allow)
205
+
206
+
207
+ def get_config_for_access(cfg):
208
+ """The live config (connectors can change at runtime), falling back to the app's own."""
209
+ try:
210
+ from .config import get_config
211
+ return get_config()
212
+ except Exception:
213
+ return cfg
214
+
215
+
216
+ def _local_user() -> str:
217
+ import os
218
+ return os.getenv("ZARQA_LOCAL_USER", "") or "local"
219
+
220
+
221
+ def init_auth(app, cfg) -> None:
222
+ mode = cfg.auth.mode
223
+
224
+ app.secret_key = cfg.auth.secret_key or secrets.token_hex(32)
225
+ app.config.update(
226
+ SESSION_COOKIE_HTTPONLY=True,
227
+ SESSION_COOKIE_SAMESITE="Lax",
228
+ SESSION_COOKIE_NAME="zarqa_session",
229
+ )
230
+ if mode in ("local", "oidc") and not cfg.auth.secret_key:
231
+ log.warning("auth.mode=%s but ZARQA_SECRET_KEY is unset; using an ephemeral "
232
+ "key (sessions drop on restart). Set ZARQA_SECRET_KEY.", mode)
233
+
234
+ oauth = None
235
+ if mode == "oidc":
236
+ try:
237
+ from authlib.integrations.flask_client import OAuth # optional dep
238
+ except ImportError as err:
239
+ raise RuntimeError("auth.mode=oidc requires the 'oidc' extra: pip install 'zarqa[oidc]'") from err
240
+ o = cfg.auth.oidc
241
+ if not (o.get("issuer") and o.get("client_id")):
242
+ raise RuntimeError("auth.mode=oidc requires auth.oidc.issuer and client_id")
243
+ oauth = OAuth(app)
244
+ oauth.register(
245
+ name="zarqa_sso",
246
+ client_id=o["client_id"],
247
+ client_secret=o["client_secret"],
248
+ server_metadata_url=o["issuer"].rstrip("/") + "/.well-known/openid-configuration",
249
+ client_kwargs={"scope": o.get("scopes", "openid email profile")},
250
+ )
251
+
252
+ @app.before_request
253
+ def _guard():
254
+ p = request.path
255
+ g.zarqa_role = "admin"
256
+ g.zarqa_user = _local_user()
257
+ g.zarqa_groups = []
258
+ if p.startswith("/taxii2/") and request.method == "GET":
259
+ from .share.taxii_server import token_ok
260
+ if token_ok(cfg):
261
+ g.zarqa_role, g.zarqa_user, g.zarqa_groups = "viewer", "taxii-token", []
262
+ return None
263
+ if p == "/mcp":
264
+ import os
265
+ mc = (cfg.raw or {}).get("mcp") or {}
266
+ tok = os.getenv(str(mc.get("token_env", "")), "") if mc.get("token_env") else ""
267
+ got = request.headers.get("Authorization", "")
268
+ if tok and got.startswith("Bearer ") and hmac.compare_digest(got[7:].strip(), tok):
269
+ role = str(mc.get("token_role", "viewer")).lower()
270
+ g.zarqa_role = role if role in _VALID_ROLES else "viewer"
271
+ g.zarqa_user = "mcp-token"
272
+ g.zarqa_groups = parse_groups(mc.get("token_groups") or [])
273
+ return None
274
+ if mode == "none":
275
+ return None
276
+ if p.startswith("/static/") or p in _OPEN_PATHS:
277
+ return None
278
+
279
+ if mode == "basic":
280
+ a = request.authorization
281
+ if a and verify_user(cfg, a.username or "", a.password or ""):
282
+ groups = user_groups(cfg, a.username or "")
283
+ role = resolve_user_role(cfg, a.username or "", groups)
284
+ if not role:
285
+ return Response("Forbidden: none of your groups has a role here.", 403)
286
+ g.zarqa_role = role
287
+ g.zarqa_user = a.username
288
+ g.zarqa_groups = groups
289
+ allowed, reason = check_rbac_permission(role, request.method, p, request.args)
290
+ if not allowed:
291
+ return Response(reason, 403)
292
+ return None
293
+ return Response("Zarqa authentication required", 401,
294
+ {"WWW-Authenticate": 'Basic realm="Zarqa"'})
295
+
296
+ if mode == "proxy":
297
+ # Anti-forgery: only a trusted proxy IP may assert the identity header.
298
+ # Fail CLOSED: with no trusted-proxy list, anyone could send the
299
+ # header and log in as any user, so refuse every request instead.
300
+ if not cfg.auth.proxy_trusted_ips:
301
+ return Response("Forbidden: auth.mode=proxy requires auth.proxy_trusted_ips "
302
+ "(the reverse proxy's IP). Refusing to trust the identity header.", 403)
303
+ # Check the REAL TCP peer, not request.remote_addr: ProxyFix rewrites
304
+ # REMOTE_ADDR from the X-Forwarded-For header, which a direct client
305
+ # can forge. ProxyFix preserves the true socket peer under
306
+ # environ["werkzeug.proxy_fix.orig"]["REMOTE_ADDR"]; if ProxyFix did
307
+ # not run, REMOTE_ADDR is still the real peer.
308
+ _orig = request.environ.get("werkzeug.proxy_fix.orig") or {}
309
+ real_peer = _orig.get("REMOTE_ADDR") or request.environ.get("REMOTE_ADDR")
310
+ if real_peer not in cfg.auth.proxy_trusted_ips:
311
+ return Response("Forbidden: request did not arrive via a trusted proxy.", 403)
312
+ user = request.headers.get(cfg.auth.proxy_header, "")
313
+ if user and _user_allowed(cfg, user):
314
+ hdr = cfg.auth.proxy_groups_header
315
+ groups = user_groups(cfg, user, parse_groups(request.headers.get(hdr, "")) if hdr else [])
316
+ role = resolve_user_role(cfg, user, groups)
317
+ if not role:
318
+ return Response("Forbidden: none of your groups has a role here.", 403)
319
+ g.zarqa_role = role
320
+ g.zarqa_user = user
321
+ g.zarqa_groups = groups
322
+ allowed, reason = check_rbac_permission(role, request.method, p, request.args)
323
+ if not allowed:
324
+ return Response(reason, 403)
325
+ return None
326
+ return Response("Forbidden: missing or disallowed proxy auth header.", 403)
327
+
328
+ # local / oidc -> session
329
+ user = session.get("user")
330
+ if user:
331
+ if request.method in ("POST", "PUT", "DELETE", "PATCH") and not _origin_matches_request(cfg):
332
+ return Response("Forbidden: cross-origin state-changing request blocked (CSRF guard).", 403)
333
+ groups = user_groups(cfg, str(user), session.get("groups") or [])
334
+ role = session.get("role") or resolve_user_role(cfg, str(user), groups)
335
+ if not role:
336
+ session.clear()
337
+ return Response("Forbidden: none of your groups has a role here.", 403)
338
+ g.zarqa_role = role
339
+ g.zarqa_user = str(user)
340
+ g.zarqa_groups = groups
341
+ allowed, reason = check_rbac_permission(role, request.method, p, request.args)
342
+ if not allowed:
343
+ return Response(reason, 403)
344
+ return None
345
+ if p.startswith("/api/"):
346
+ return Response("Authentication required.", 401)
347
+ return redirect("/login")
348
+
349
+ @app.before_request
350
+ def _set_access():
351
+ """Runs after _guard let the request through: hold who is asking for this request (zarqa/access.py)."""
352
+ from .access import build, set_current
353
+ from .connectors.spec import parse_connectors
354
+ try:
355
+ specs = parse_connectors(list(get_config_for_access(cfg).connectors))
356
+ except Exception:
357
+ specs = []
358
+ a = build(specs, user=str(getattr(g, "zarqa_user", "") or ""), role=getattr(g, "zarqa_role", "viewer"),
359
+ groups=getattr(g, "zarqa_groups", []) or [])
360
+ g.zarqa_access = a
361
+ g.zarqa_access_token = set_current(a)
362
+
363
+ @app.teardown_request
364
+ def _reset_access(_exc=None):
365
+ from .access import reset
366
+ tok = g.pop("zarqa_access_token", None)
367
+ if tok is not None:
368
+ try:
369
+ reset(tok)
370
+ except ValueError:
371
+ pass
372
+
373
+ @app.route("/login", methods=["GET", "POST"])
374
+ def login():
375
+ if mode == "none":
376
+ return redirect("/")
377
+ if mode == "oidc":
378
+ base = (cfg.external_url or request.url_root).rstrip("/")
379
+ redirect_uri = base + "/auth/callback"
380
+ return oauth.zarqa_sso.authorize_redirect(redirect_uri)
381
+ # local (and basic, though basic never redirects here)
382
+ if request.method == "POST":
383
+ client_ip = request.remote_addr or "unknown"
384
+ if not _check_login_rate_limit(client_ip):
385
+ return Response("Too many login attempts. Please wait 60 seconds.", 429)
386
+ u = request.form.get("username", "")
387
+ pw = request.form.get("password", "")
388
+ if verify_user(cfg, u, pw):
389
+ session["user"] = u
390
+ return redirect("/")
391
+ return render_template("login.html", error="Invalid credentials.",
392
+ instance=cfg.instance_name), 401
393
+ return render_template("login.html", error=None, instance=cfg.instance_name)
394
+
395
+ @app.route("/auth/callback")
396
+ def auth_callback():
397
+ if mode != "oidc":
398
+ return redirect("/")
399
+ def _fail(msg: str):
400
+ safe_msg = html.escape(str(msg or ""))
401
+ return Response(
402
+ f"<!doctype html><meta charset=utf-8><title>Zarqa sign-in</title>"
403
+ f"<body style='background:#090b10;color:#f1f5f9;font-family:Inter,sans-serif;"
404
+ f"display:flex;min-height:100vh;align-items:center;justify-content:center;text-align:center'>"
405
+ f"<div><h2 style='color:#ef4444'>Sign-in failed</h2><p style='color:#94a3b8'>{safe_msg}</p>"
406
+ f"<a href='/login' style='color:#ef4444'>Try again</a></div></body>", 400,
407
+ {"Content-Type": "text/html; charset=utf-8"})
408
+
409
+ err = request.args.get("error")
410
+ if err:
411
+ desc = request.args.get("error_description", err)
412
+ log.warning("OIDC provider returned an error: %s (%s)", err, desc)
413
+ return _fail(desc)
414
+ try:
415
+ token = oauth.zarqa_sso.authorize_access_token()
416
+ info = token.get("userinfo") or {}
417
+ except Exception as exc:
418
+ log.warning("OIDC callback exchange failed: %s", exc)
419
+ return _fail("Could not complete sign-in. Please try again.")
420
+ claim = cfg.auth.oidc.get("username_claim", "email")
421
+ user = info.get(claim) or info.get("email") or info.get("preferred_username") or info.get("sub")
422
+ if not user or not _user_allowed(cfg, user):
423
+ return Response("Forbidden: your account is not permitted.", 403)
424
+ groups = parse_groups(info.get(cfg.auth.oidc.get("groups_claim", "groups")))
425
+ if not resolve_user_role(cfg, str(user), user_groups(cfg, str(user), groups)):
426
+ return Response("Forbidden: none of your groups has a role here.", 403)
427
+ session["user"] = user
428
+ session["groups"] = groups[:200]
429
+ return redirect("/")
430
+
431
+ @app.route("/logout")
432
+ def logout():
433
+ session.clear()
434
+ return redirect("/login" if mode != "none" else "/")
435
+
436
+ @app.route("/healthz")
437
+ def healthz():
438
+ return {"ok": True}
@@ -0,0 +1 @@
1
+ """Brand monitoring: lookalike domains of your own, found, checked and tracked."""