-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudit.py
More file actions
253 lines (233 loc) · 10.3 KB
/
audit.py
File metadata and controls
253 lines (233 loc) · 10.3 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
"""
Helios OS — Full System Audit
Tests every route, API, template, and static asset.
"""
import os, sys, json, time
os.chdir(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
print("=" * 70)
print(" ☀ HELIOS OS — FULL SYSTEM AUDIT")
print("=" * 70)
# ─── 1. CONFIG ────────────────────────────────────────────────────
print("\n[1] CONFIG VALIDATION")
try:
from config import HeliosConfig
HeliosConfig.validate()
print(" ✓ Config loaded & validated")
print(f" Domain: {HeliosConfig.DOMAIN}")
print(f" DB: {HeliosConfig.DATABASE_URL}")
print(f" Port: {HeliosConfig.PORT}")
print(f" Token supply: {HeliosConfig.TOKEN_TOTAL_SUPPLY:,}")
except Exception as e:
print(f" ✗ Config FAILED: {e}")
# ─── 2. APP FACTORY ──────────────────────────────────────────────
print("\n[2] APP FACTORY")
try:
from app import create_app
app = create_app()
print(" ✓ create_app() succeeded")
except Exception as e:
print(f" ✗ create_app() FAILED: {e}")
sys.exit(1)
# ─── 3. ALL REGISTERED ROUTES ────────────────────────────────────
print("\n[3] REGISTERED ROUTES")
rules = sorted([r for r in app.url_map.iter_rules() if not r.rule.startswith('/static')], key=lambda r: r.rule)
for r in rules:
methods = ','.join(sorted(r.methods - {'HEAD', 'OPTIONS'}))
print(f" {methods:6} {r.rule}")
print(f" Total: {len(rules)} routes")
# ─── 4. PAGE ROUTES ──────────────────────────────────────────────
print("\n[4] PAGE ROUTES (GET → HTML)")
page_routes = [
"/", "/dashboard", "/field", "/network", "/ask", "/protocol",
"/status", "/treasury", "/vault", "/vault/gold", "/activate",
"/metrics", "/enter", "/join", "/health"
]
ok = fail = 0
with app.test_client() as client:
for route in page_routes:
try:
r = client.get(route)
body = r.data.decode("utf-8", errors="replace")
has_nav = "<nav" in body
has_css = "helios.css" in body or route == "/health"
has_fallback = "static-fallback.js" in body or route == "/health"
size = len(r.data)
issues = []
if r.status_code != 200:
issues.append(f"HTTP {r.status_code}")
if not has_nav and route != "/health":
issues.append("NO <nav>")
if not has_css:
issues.append("NO helios.css")
if not has_fallback:
issues.append("NO fallback.js")
if 'styleshee et' in body:
issues.append("BROKEN rel=stylesheet")
if issues:
print(f" ⚠ {route:20} {size:>7}B ISSUES: {', '.join(issues)}")
fail += 1
else:
print(f" ✓ {route:20} {size:>7}B OK")
ok += 1
except Exception as e:
print(f" ✗ {route:20} ERROR: {e}")
fail += 1
print(f" Pages: {ok} OK, {fail} issues")
# ─── 5. API ROUTES ───────────────────────────────────────────────
print("\n[5] API ROUTES (GET → JSON)")
api_routes = [
"/api/health",
"/api/field/status",
"/api/metrics/all",
"/api/treasury/reserves",
"/api/token/info",
"/api/token/supply",
"/api/token/verify",
"/api/token/founder-lock",
"/api/rewards/protocol",
"/api/energy/conservation",
"/api/infra/status",
"/api/certificates/active",
]
ok = fail = 0
with app.test_client() as client:
for route in api_routes:
try:
r = client.get(route)
ct = r.headers.get("content-type", "")
is_json = "json" in ct
size = len(r.data)
issues = []
if r.status_code != 200:
issues.append(f"HTTP {r.status_code}")
if not is_json:
issues.append(f"NOT JSON ({ct})")
else:
try:
data = json.loads(r.data)
except:
issues.append("INVALID JSON")
if issues:
print(f" ⚠ {route:35} {size:>7}B {', '.join(issues)}")
fail += 1
else:
print(f" ✓ {route:35} {size:>7}B OK")
ok += 1
except Exception as e:
print(f" ✗ {route:35} ERROR: {e}")
fail += 1
print(f" APIs: {ok} OK, {fail} issues")
# ─── 6. STATIC ASSETS ────────────────────────────────────────────
print("\n[6] STATIC ASSETS")
required_static = [
"static/css/helios.css",
"static/js/static-fallback.js",
"static/js/network-viz.js",
]
for f in required_static:
path = os.path.join(os.path.dirname(__file__), f)
if os.path.exists(path):
size = os.path.getsize(path)
print(f" ✓ {f:40} {size:>7}B")
else:
print(f" ✗ {f:40} MISSING!")
# ─── 7. TEMPLATES ────────────────────────────────────────────────
print("\n[7] TEMPLATES")
template_dir = os.path.join(os.path.dirname(__file__), "templates")
templates = sorted(os.listdir(template_dir))
for t in templates:
path = os.path.join(template_dir, t)
if os.path.isfile(path):
size = os.path.getsize(path)
with open(path, "r", encoding="utf-8", errors="replace") as fh:
content = fh.read()
issues = []
if "styleshee et" in content:
issues.append("BROKEN rel=stylesheet")
if t != "base.html" and "{% extends" not in content and "{% block" not in content and "<html" not in content:
issues.append("NOT A TEMPLATE?")
status = f" ISSUES: {', '.join(issues)}" if issues else ""
print(f" {'✗' if issues else '✓'} {t:25} {size:>7}B{status}")
# ─── 8. MODELS ───────────────────────────────────────────────────
print("\n[8] MODELS (import check)")
model_dir = os.path.join(os.path.dirname(__file__), "models")
model_files = [f[:-3] for f in os.listdir(model_dir) if f.endswith(".py") and f != "__init__.py"]
for m in sorted(model_files):
try:
__import__(f"models.{m}")
print(f" ✓ models.{m}")
except Exception as e:
print(f" ✗ models.{m}: {e}")
# ─── 9. CORE MODULES ─────────────────────────────────────────────
print("\n[9] CORE MODULES (import check)")
core_dir = os.path.join(os.path.dirname(__file__), "core")
core_files = [f[:-3] for f in os.listdir(core_dir) if f.endswith(".py") and f != "__init__.py"]
for m in sorted(core_files):
try:
__import__(f"core.{m}")
print(f" ✓ core.{m}")
except Exception as e:
print(f" ✗ core.{m}: {e}")
# ─── 10. FREEZE PIPELINE ─────────────────────────────────────────
print("\n[10] FREEZE PIPELINE")
try:
from freeze import freeze
print(" ✓ freeze.py imports OK")
except Exception as e:
print(f" ✗ freeze.py import FAILED: {e}")
# Check build dir
build_dir = os.path.join(os.path.dirname(__file__), "build")
if os.path.exists(build_dir):
html_count = sum(1 for r, d, f in os.walk(build_dir) for name in f if name.endswith('.html'))
static_exists = os.path.exists(os.path.join(build_dir, "static"))
bid_path = os.path.join(build_dir, "BUILD_ID.txt")
bid = open(bid_path).read().strip() if os.path.exists(bid_path) else "NONE"
print(f" ✓ build/ exists: {html_count} HTML files, static={'YES' if static_exists else 'NO'}, BUILD_ID={bid}")
else:
print(" ⚠ No build/ directory — run freeze.py")
# ─── 11. NETLIFY CONFIG ──────────────────────────────────────────
print("\n[11] NETLIFY CONFIG")
toml_path = os.path.join(os.path.dirname(__file__), "netlify.toml")
if os.path.exists(toml_path):
with open(toml_path, "r") as f:
toml = f.read()
issues = []
if 'publish = "build"' not in toml:
issues.append("publish dir not 'build'")
if "skip_processing = true" not in toml:
issues.append("post-processing not disabled")
if 'rel="styleshee' in toml:
issues.append("broken stylesheet in toml?!")
if "/* → /index.html" in toml or "/* /index.html" in toml:
issues.append("SPA catch-all still present")
csp_line = [l for l in toml.split('\n') if 'Content-Security-Policy' in l]
if csp_line:
csp = csp_line[0]
if "'unsafe-inline'" not in csp:
issues.append("CSP missing 'unsafe-inline' for scripts or styles")
if "d3js.org" not in csp:
issues.append("CSP missing d3js.org")
if issues:
for i in issues:
print(f" ⚠ {i}")
else:
print(" ✓ netlify.toml looks correct")
else:
print(" ✗ No netlify.toml!")
# ─── 12. .ENV ────────────────────────────────────────────────────
print("\n[12] ENVIRONMENT")
env_path = os.path.join(os.path.dirname(__file__), ".env")
if os.path.exists(env_path):
with open(env_path) as f:
lines = [l.strip() for l in f if l.strip() and not l.startswith('#')]
print(f" ✓ .env exists with {len(lines)} keys")
keys_present = [l.split('=')[0] for l in lines]
for k in keys_present:
print(f" {k}")
else:
print(" ⚠ No .env file")
# ─── SUMMARY ─────────────────────────────────────────────────────
print("\n" + "=" * 70)
print(" AUDIT COMPLETE")
print("=" * 70)