File size: 64,103 Bytes
3ccdcc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
"""Opt-in WebUI extension hooks.

This module intentionally provides a small, self-hosted extension surface:
configured same-origin script/style injection plus sandboxed static file serving.
It is disabled by default and never executes or fetches third-party URLs.
"""

import html
import json
import math
import logging
import os
import re
import threading
from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Tuple
from urllib.parse import unquote, urlsplit
import hashlib
import io
import time
import zipfile
from urllib.request import HTTPRedirectHandler, build_opener, urlopen

from api.helpers import _security_headers, j

_log = logging.getLogger(__name__)

# Sane bound on configured URLs — real extensions ship 1-3 files. Higher values
# typically indicate a misconfiguration (one giant unsplit string, or a runaway
# generator script that wrote an env-var template without filtering). Capping
# avoids rendering tens of thousands of <script> tags into every page load.
_MAX_URL_LIST = 32

# Keep extension manifests small and auditable. The manifest is a convenience for
# bundling static assets, not a package manager or dependency lockfile.
_MAX_MANIFEST_BYTES = 64 * 1024

# Tracks rejected URL strings we've already warned about so a misconfigured env
# var doesn't spam the log on every request that re-reads it.
_warned_urls: set = set()


class _ManifestTooLarge(ValueError):
    pass


class ExtensionToggleError(Exception):
    """Sanitized extension mutation error safe to return to the browser."""

    def __init__(self, message: str, status: int = 400):
        super().__init__(message)
        self.status = status


class ExtensionInstallError(Exception):
    """Sanitized extension install/uninstall error safe to return to the browser."""

    def __init__(self, message: str, status: int = 400):
        super().__init__(message)
        self.status = status


EXTENSION_ROUTE_PREFIX = "/extensions/"
_EXTENSION_DIR_ENV = "HERMES_WEBUI_EXTENSION_DIR"
_EXTENSION_SCRIPT_URLS_ENV = "HERMES_WEBUI_EXTENSION_SCRIPT_URLS"
_EXTENSION_STYLESHEET_URLS_ENV = "HERMES_WEBUI_EXTENSION_STYLESHEET_URLS"
_EXTENSION_MANIFEST_ENV = "HERMES_WEBUI_EXTENSION_MANIFEST"
_ALLOWED_ASSET_PREFIXES = ("/extensions/", "/static/")
_SIDECAR_WARNING_SOURCE = "manifest:sidecars"
_DEFAULT_SIDECAR_HEALTH_PATH = "/health"
_LOOPBACK_SIDECAR_HOSTS = {"127.0.0.1", "localhost", "::1"}
_EXTENSION_STATE_FILENAME = "extension-overrides.json"
_MAX_EXTENSION_STATE_BYTES = 32 * 1024
_MAX_DISABLED_EXTENSION_IDS = 512
_EXTENSION_ID_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$")
_EXTENSION_SETTINGS_KEY_RE = re.compile(r"^[A-Za-z][A-Za-z0-9._-]{0,63}$")
_EXTENSION_STATE_WARNING_SOURCE = "extension_state"
_EXTENSION_STATE_LOCK = threading.Lock()
_EXTENSION_SETTING_TYPES = {"boolean", "string", "number", "integer", "enum"}

_GALLERY_INSTALL_STATE_FILENAME = "extension-install-manifest.json"
_MAX_INSTALL_MANIFEST_BYTES = 128 * 1024
_MAX_GALLERY_INSTALLED_IDS = 256
_MAX_ZIP_DOWNLOAD_BYTES = 32 * 1024 * 1024
_REGISTRY_URL = "https://hermes-webui.github.io/hermes-webui-extensions/registry.json"
_REGISTRY_ALLOWED_DOWNLOAD_HOSTS = frozenset({"hermes-webui.github.io"})
_REGISTRY_CACHE: dict = {}
_REGISTRY_LOCK = threading.Lock()
_REGISTRY_TTL_SECONDS = 300


class _AllowlistRedirectHandler(HTTPRedirectHandler):
    """Reject redirects to hosts not in the download allowlist."""

    def redirect_request(self, req, fp, code, msg, headers, newurl):
        parsed = urlsplit(newurl)
        if parsed.scheme != "https" or parsed.hostname not in _REGISTRY_ALLOWED_DOWNLOAD_HOSTS:
            raise ExtensionInstallError("Download redirected to disallowed host")
        return super().redirect_request(req, fp, code, msg, headers, newurl)


def _safe_download(url: str, max_bytes: int, timeout: int = 30) -> bytes:
    """Download from an allowlisted host, rejecting cross-host redirects."""
    opener = build_opener(_AllowlistRedirectHandler)
    resp = opener.open(url, timeout=timeout)
    try:
        return resp.read(max_bytes + 1)
    finally:
        resp.close()


_EXTENSION_MIME = {
    "css": "text/css",
    "js": "application/javascript",
    "html": "text/html",
    "svg": "image/svg+xml",
    "png": "image/png",
    "jpg": "image/jpeg",
    "jpeg": "image/jpeg",
    "ico": "image/x-icon",
    "gif": "image/gif",
    "webp": "image/webp",
    "woff": "font/woff",
    "woff2": "font/woff2",
    "ttf": "font/ttf",
    "otf": "font/otf",
    "wasm": "application/wasm",
}
_TEXT_MIME_TYPES = {"text/css", "application/javascript", "text/html", "image/svg+xml", "text/plain"}


def _default_extension_root() -> Path:
    """WebUI-managed default extension directory under the state dir.

    Used when ``HERMES_WEBUI_EXTENSION_DIR`` is unset so one-click gallery
    install works out of the box on a single-user self-hosted instance with no
    environment setup. It lives alongside sessions/settings in the WebUI-owned
    state dir, which is a different trust domain from "a user-writable directory
    on a shared box" — the loaded code still runs with full session authority,
    so the trust model is unchanged (see docs/EXTENSIONS.md).
    """
    return _extension_state_dir() / "extensions"


def _extension_root() -> Optional[Path]:
    """Return the active extension directory, or None when none is available.

    Resolution order:
    1. ``HERMES_WEBUI_EXTENSION_DIR`` when set — must be an existing directory,
       otherwise None (the admin owns that path; we never auto-create it).
    2. Otherwise the WebUI-managed default (``STATE_DIR/extensions``) when it
       already exists. The first gallery install creates it on demand
       (see ``_writable_extension_root``); until then this stays None and the
       UI reports the same "nothing installed yet" state as before.
    """
    raw = os.getenv(_EXTENSION_DIR_ENV, "").strip()
    if raw:
        root = Path(raw).expanduser().resolve()
        if not root.exists() or not root.is_dir():
            return None
        return root
    default_root = _default_extension_root()
    try:
        if default_root.is_dir() and not default_root.is_symlink():
            return default_root.resolve()
    except OSError:
        return None
    return None


def _writable_extension_root() -> Optional[Path]:
    """Resolve the extension root for writes, bootstrapping the managed default.

    When ``HERMES_WEBUI_EXTENSION_DIR`` is set we use it as-is (the admin owns
    it; it must already exist). When unset we create and return the
    WebUI-managed default so a fresh install can install an extension with zero
    configuration — plug and play.
    """
    raw = os.getenv(_EXTENSION_DIR_ENV, "").strip()
    if raw:
        return _extension_root()
    default_root = _default_extension_root()
    try:
        default_root.mkdir(parents=True, exist_ok=True)
    except OSError:
        return None
    try:
        if default_root.is_symlink() or not default_root.is_dir():
            return None
        return default_root.resolve()
    except OSError:
        return None


def _extension_root_status() -> Tuple[Optional[Path], bool, bool]:
    """Return (root, configured, valid) without exposing the configured path.

    With no ``HERMES_WEBUI_EXTENSION_DIR`` the WebUI-managed default is always
    available as an install target, so ``configured`` is True (extensions are
    no longer "not configured" out of the box). ``valid`` reflects whether that
    managed directory currently exists — it is created on the first install.
    """
    raw = os.getenv(_EXTENSION_DIR_ENV, "").strip()
    if raw:
        root = Path(raw).expanduser().resolve()
        if not root.exists() or not root.is_dir():
            return None, True, False
        return root, True, True
    root = _extension_root()
    return root, True, root is not None


def _new_diagnostics() -> Dict[str, Any]:
    return {"warnings": []}


def _add_diagnostic_warning(
    diagnostics: Optional[Dict[str, Any]], code: str, source: str
) -> None:
    """Record a sanitized diagnostic warning.

    Warnings intentionally carry only stable codes and coarse sources. They never
    include filesystem paths, raw environment values, or rejected URL strings.
    """
    if diagnostics is None:
        return
    warnings = diagnostics.setdefault("warnings", [])
    if not isinstance(warnings, list):
        return
    warning = {"code": code, "source": source}
    if warning not in warnings:
        warnings.append(warning)


def _valid_extension_id(value: object) -> bool:
    return isinstance(value, str) and bool(_EXTENSION_ID_RE.fullmatch(value.strip()))


def _extension_state_dir() -> Path:
    """Return the WebUI-managed state directory for extension overrides."""
    try:
        from api.config import STATE_DIR

        return Path(STATE_DIR)
    except Exception:
        return Path(os.getenv("HERMES_WEBUI_STATE_DIR", str(Path.home() / ".hermes" / "webui"))).expanduser()


def _extension_state_file() -> Path:
    return _extension_state_dir() / _EXTENSION_STATE_FILENAME


def _empty_extension_state() -> Dict[str, Any]:
    return {"version": 1, "disabled_extensions": []}


def _load_extension_state(diagnostics: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
    """Load UI-managed extension overrides, failing safe without path leaks."""
    state_file = _extension_state_file()
    try:
        if not state_file.exists() or not state_file.is_file():
            return _empty_extension_state()
        with state_file.open("rb") as fh:
            raw = fh.read(_MAX_EXTENSION_STATE_BYTES + 1)
        if len(raw) > _MAX_EXTENSION_STATE_BYTES:
            _add_diagnostic_warning(
                diagnostics, "extension_state_oversized", _EXTENSION_STATE_WARNING_SOURCE
            )
            return _empty_extension_state()
        parsed = json.loads(raw.decode("utf-8"))
    except (OSError, UnicodeDecodeError, json.JSONDecodeError, RecursionError):
        _add_diagnostic_warning(
            diagnostics, "extension_state_unreadable", _EXTENSION_STATE_WARNING_SOURCE
        )
        return _empty_extension_state()
    if not isinstance(parsed, dict):
        _add_diagnostic_warning(
            diagnostics, "extension_state_invalid", _EXTENSION_STATE_WARNING_SOURCE
        )
        return _empty_extension_state()
    disabled_raw = parsed.get("disabled_extensions", [])
    if not isinstance(disabled_raw, list):
        _add_diagnostic_warning(
            diagnostics, "extension_state_invalid", _EXTENSION_STATE_WARNING_SOURCE
        )
        return _empty_extension_state()
    disabled: List[str] = []
    seen: Set[str] = set()
    invalid = False
    for value in disabled_raw:
        if not _valid_extension_id(value):
            invalid = True
            continue
        ext_id = str(value).strip()
        if ext_id in seen:
            continue
        seen.add(ext_id)
        disabled.append(ext_id)
        if len(disabled) >= _MAX_DISABLED_EXTENSION_IDS:
            _add_diagnostic_warning(
                diagnostics, "extension_state_truncated", _EXTENSION_STATE_WARNING_SOURCE
            )
            break
    if invalid:
        _add_diagnostic_warning(
            diagnostics, "extension_state_invalid_entries", _EXTENSION_STATE_WARNING_SOURCE
        )
    return {"version": 1, "disabled_extensions": disabled}


def _write_extension_state(state: Dict[str, Any]) -> None:
    """Persist extension overrides with an atomic same-directory replace."""
    disabled_raw = state.get("disabled_extensions", [])
    disabled: List[str] = []
    seen: Set[str] = set()
    if isinstance(disabled_raw, list):
        for value in disabled_raw:
            if not _valid_extension_id(value):
                continue
            ext_id = str(value).strip()
            if ext_id in seen:
                continue
            seen.add(ext_id)
            disabled.append(ext_id)
            if len(disabled) >= _MAX_DISABLED_EXTENSION_IDS:
                break
    payload = {"version": 1, "disabled_extensions": disabled}
    target = _extension_state_file()
    target.parent.mkdir(parents=True, exist_ok=True)
    tmp = target.with_name(f".{target.name}.{os.getpid()}.{threading.get_ident()}.tmp")
    data = json.dumps(payload, ensure_ascii=False, indent=2).encode("utf-8")
    try:
        with tmp.open("wb") as fh:
            fh.write(data)
            fh.flush()
            os.fsync(fh.fileno())
        os.replace(tmp, target)
    finally:
        try:
            if tmp.exists():
                tmp.unlink()
        except OSError:
            pass


def _fully_unquote_path(path: str) -> str:
    """Decode percent-encoding until stable so encoded dot-segments cannot hide.

    Iterates up to 10 times so even quadruple-encoded inputs like
    ``%2525252e%2525252e`` collapse to literal ``..`` and are rejected by
    the segment-level safety check downstream. URL strings stabilize in
    fewer than 5 iterations in practice; the cap is defensive.
    """
    previous = path
    for _ in range(10):
        current = unquote(previous)
        if current == previous:
            return current
        previous = current
    return previous


def _is_safe_asset_url(value: str) -> bool:
    """Allow only same-origin extension/static asset URLs.

    External schemes, protocol-relative URLs, fragments, arbitrary API paths, and
    encoded traversal are rejected so enabling extensions does not require
    loosening the CSP.
    """
    if not value or any(ch in value for ch in ('\x00', '\r', '\n', '"', "'", "<", ">", "\\")):
        return False
    parsed = urlsplit(value)
    if parsed.scheme or parsed.netloc or parsed.fragment:
        return False

    decoded_path = _fully_unquote_path(parsed.path)
    if not any(decoded_path.startswith(prefix) for prefix in _ALLOWED_ASSET_PREFIXES):
        return False

    for prefix in _ALLOWED_ASSET_PREFIXES:
        if decoded_path.startswith(prefix):
            return _is_safe_relative_path(decoded_path[len(prefix) :])
    return False


def _warn_rejected_url(value: str, source: str) -> None:
    if value in _warned_urls:
        return
    _warned_urls.add(value)
    _log.warning(
        "Rejected extension URL %r from %s (not a same-origin "
        "/extensions/ or /static/ path, or contains unsafe chars)",
        value, source,
    )


def _append_safe_asset_url(
    urls: List[str],
    value: str,
    source: str,
    *,
    dedupe: bool = True,
    diagnostics: Optional[Dict[str, Any]] = None,
) -> bool:
    """Append a validated URL while preserving order and the global cap.

    Returns False when the caller should stop accumulating entries for this list.
    Manifest paths dedupe by default, while env-only lists preserve their legacy
    behavior unless they are appending after manifest-provided assets.
    """
    value = value.strip() if isinstance(value, str) else ""
    if not value:
        return True
    if not _is_safe_asset_url(value):
        _warn_rejected_url(value, source)
        _add_diagnostic_warning(diagnostics, "asset_url_rejected", source)
        return True
    if dedupe and value in urls:
        return True
    if len(urls) >= _MAX_URL_LIST:
        if source not in _warned_urls:
            _warned_urls.add(source)
            _log.warning(
                "Extension URL list %s truncated at %d entries",
                source, _MAX_URL_LIST,
            )
        _add_diagnostic_warning(diagnostics, "asset_url_list_truncated", source)
        return False
    urls.append(value)
    return True


def _read_url_list(
    env_name: str,
    existing: Optional[List[str]] = None,
    *,
    diagnostics: Optional[Dict[str, Any]] = None,
) -> List[str]:
    raw = os.getenv(env_name, "")
    urls = list(existing or [])
    # Preserve legacy env-only behavior: duplicate env URLs injected twice before
    # manifests existed. When a manifest seeds the list, dedupe appended env URLs
    # so bundle manifests and explicit overrides do not double-load an asset.
    dedupe = existing is not None
    for item in raw.split(","):
        if not _append_safe_asset_url(
            urls, item, env_name, dedupe=dedupe, diagnostics=diagnostics
        ):
            break
    return urls


def _manifest_path_with_status(root: Path) -> Tuple[Optional[Path], str]:
    raw = os.getenv(_EXTENSION_MANIFEST_ENV, "").strip()
    if not raw:
        return None, "not_configured"
    if raw.startswith(("/", "~")):
        _log.warning("Rejected extension manifest path from %s", _EXTENSION_MANIFEST_ENV)
        return None, "invalid_path"
    rel = _fully_unquote_path(raw)
    if not _is_safe_relative_path(rel):
        _log.warning("Rejected extension manifest path from %s", _EXTENSION_MANIFEST_ENV)
        return None, "invalid_path"
    manifest = (root / rel).resolve()
    try:
        manifest.relative_to(root)
    except ValueError:
        _log.warning("Rejected extension manifest path from %s", _EXTENSION_MANIFEST_ENV)
        return None, "invalid_path"
    return manifest, "configured"


def _manifest_path(root: Path) -> Optional[Path]:
    manifest, _ = _manifest_path_with_status(root)
    return manifest


def _manifest_asset_url(value: object, asset_base: str = "") -> str:
    """Normalize a manifest asset entry to the existing same-origin URL format."""
    if not isinstance(value, str):
        return ""
    item = value.strip()
    if not item:
        return ""
    parsed = urlsplit(item)
    if parsed.scheme or parsed.netloc or item.startswith("//"):
        return item
    # Manifests are meant to make bundled local assets less noisy to list, so
    # bare relative paths resolve under /extensions/. Absolute same-origin paths
    # are still allowed and go through the same validator as env-configured URLs.
    if item.startswith("/"):
        return item
    base = asset_base.strip("/")
    rel = f"{base}/{item}" if base else item
    return EXTENSION_ROUTE_PREFIX + rel


def _manifest_asset_value_with_base(value: object, asset_base: str) -> object:
    """Rewrite a manifest asset value so it remains relative to its manifest file."""
    if not isinstance(value, str):
        return value
    item = value.strip()
    if not item:
        return item
    parsed = urlsplit(item)
    if parsed.scheme or parsed.netloc or item.startswith("//") or item.startswith("/"):
        return item
    base = asset_base.strip("/")
    return f"{base}/{item}" if base else item


def _copy_manifest_entry_with_asset_base(entry: Dict[str, object], asset_base: str) -> Dict[str, object]:
    copied = dict(entry)
    for key in ("scripts", "stylesheets"):
        values = copied.get(key)
        if isinstance(values, list):
            copied[key] = [_manifest_asset_value_with_base(value, asset_base) for value in values]
    return copied


def _manifest_entry_text(entry: Dict[str, object], key: str) -> str:
    value = entry.get(key)
    if not isinstance(value, str):
        return ""
    return value.strip()

def _manifest_entry_storage_owned(entry: Dict[str, object]) -> bool:
    permissions = entry.get("permissions")
    if not isinstance(permissions, dict):
        return False
    storage = permissions.get("storage")
    return isinstance(storage, dict) and storage.get("owned") is True

def _settings_text(value: object, *, max_len: int = 160) -> str:
    if not isinstance(value, str):
        return ""
    text = value.strip()
    return text[:max_len]

def _normalize_enum_options(options: object) -> Optional[List[Dict[str, str]]]:
    if not isinstance(options, list) or not options:
        return None
    normalized: List[Dict[str, str]] = []
    seen: Set[str] = set()
    for option in options:
        if isinstance(option, str):
            value = option.strip()
            label = value
        elif isinstance(option, dict):
            raw_value = option.get("value")
            if not isinstance(raw_value, str):
                return None
            value = raw_value.strip()
            label = _settings_text(option.get("label")) or value
        else:
            return None
        if not value or value in seen:
            return None
        seen.add(value)
        normalized.append({"value": value, "label": label})
    return normalized

_SETTINGS_DEFAULT_MISSING = object()

def _normalize_settings_default(field_type: str, raw_default: object, options: Optional[List[Dict[str, str]]] = None) -> Tuple[bool, object]:
    if field_type == "boolean":
        if raw_default is _SETTINGS_DEFAULT_MISSING:
            return True, False
        return (True, raw_default) if isinstance(raw_default, bool) else (False, None)
    if field_type == "string":
        if raw_default is _SETTINGS_DEFAULT_MISSING:
            return True, ""
        return (True, raw_default) if isinstance(raw_default, str) else (False, None)
    if field_type == "number":
        if raw_default is _SETTINGS_DEFAULT_MISSING:
            return True, 0
        return (
            (True, raw_default)
            if isinstance(raw_default, (int, float)) and not isinstance(raw_default, bool) and math.isfinite(raw_default)
            else (False, None)
        )
    if field_type == "integer":
        if raw_default is _SETTINGS_DEFAULT_MISSING:
            return True, 0
        return (True, raw_default) if isinstance(raw_default, int) and not isinstance(raw_default, bool) else (False, None)
    if field_type == "enum" and options:
        values = [option["value"] for option in options]
        if raw_default is _SETTINGS_DEFAULT_MISSING:
            return True, values[0]
        return (True, raw_default) if isinstance(raw_default, str) and raw_default in values else (False, None)
    return False, None

def _settings_schema_values(raw_schema: object) -> List[object]:
    if isinstance(raw_schema, list):
        return raw_schema
    if isinstance(raw_schema, dict) and isinstance(raw_schema.get("fields"), list):
        return raw_schema["fields"]
    return []

def _sanitize_settings_schema(entry: Dict[str, object]) -> List[Dict[str, object]]:
    if not _manifest_entry_storage_owned(entry):
        return []
    fields: List[Dict[str, object]] = []
    seen_keys: Set[str] = set()
    for raw_field in _settings_schema_values(entry.get("settings_schema")):
        if not isinstance(raw_field, dict):
            continue
        if raw_field.get("sensitive") is True:
            continue
        key = raw_field.get("key")
        if not isinstance(key, str):
            continue
        key = key.strip()
        if not _EXTENSION_SETTINGS_KEY_RE.fullmatch(key):
            continue
        field_type = raw_field.get("type")
        if not isinstance(field_type, str):
            continue
        field_type = field_type.strip().lower()
        if field_type not in _EXTENSION_SETTING_TYPES:
            continue
        options = _normalize_enum_options(raw_field.get("options")) if field_type == "enum" else None
        if field_type == "enum" and options is None:
            continue
        ok, default = _normalize_settings_default(field_type, raw_field.get("default", _SETTINGS_DEFAULT_MISSING), options)
        if not ok:
            continue
        if key in seen_keys:
            continue
        seen_keys.add(key)
        field: Dict[str, object] = {
            "key": key,
            "type": field_type,
            "label": _settings_text(raw_field.get("label")) or key,
            "description": _settings_text(raw_field.get("description"), max_len=300),
            "default": default,
        }
        if options is not None:
            field["options"] = options
        fields.append(field)
    return fields


def _normalize_loopback_sidecar_origin(value: object) -> Optional[str]:
    """Return a canonical loopback origin or None when unsafe.

    Only browser-addressable loopback HTTP(S) origins are accepted. The returned
    value is rebuilt from parsed components so rejected raw input is never echoed
    into diagnostics.
    """
    if not isinstance(value, str):
        return None
    origin = value.strip()
    if not origin or any(
        ch in origin for ch in ("\x00", "\r", "\n", '"', "'", "<", ">", "\\")
    ):
        return None
    parsed = urlsplit(origin)
    if parsed.scheme not in ("http", "https") or not parsed.netloc:
        return None
    if parsed.username or parsed.password:
        return None
    if parsed.path or parsed.query or parsed.fragment:
        return None
    host = (parsed.hostname or "").lower()
    if host not in _LOOPBACK_SIDECAR_HOSTS:
        return None
    try:
        port = parsed.port
    except ValueError:
        return None
    display_host = f"[{host}]" if ":" in host else host
    return f"{parsed.scheme}://{display_host}{':' + str(port) if port is not None else ''}"


def _normalize_sidecar_health_path(value: object) -> Optional[str]:
    """Return a safe sidecar health path, or None when unsafe.

    Health paths are same-origin paths relative to the validated sidecar origin.
    Queries are rejected even though they are not cross-origin: health checks are
    diagnostics, and query strings often accidentally carry tokens.
    """
    if not isinstance(value, str):
        return None
    path = value.strip()
    if not path or not path.startswith("/") or path.startswith("//"):
        return None
    if any(ch in path for ch in ("\x00", "\r", "\n", '"', "'", "<", ">", "\\")):
        return None
    parsed = urlsplit(path)
    if parsed.scheme or parsed.netloc or parsed.query or parsed.fragment:
        return None
    decoded_path = _fully_unquote_path(parsed.path)
    if any(ch in decoded_path for ch in ("\x00", "\r", "\n", '"', "'", "<", ">", "\\")):
        return None
    # #4612 (Codex gate): the raw query/fragment ban above runs BEFORE percent-
    # decoding, so an encoded delimiter (e.g. "/health%3Ftoken=abc" -> "?token=abc"
    # or "/health%23frag" -> "#frag") would survive into the probed URL despite the
    # documented query/fragment ban. Re-reject "?" and "#" on the decoded path.
    if any(ch in decoded_path for ch in ("?", "#")):
        return None
    if any(ch.isspace() for ch in decoded_path):
        return None
    if not decoded_path.startswith("/") or decoded_path.startswith("//"):
        return None
    segments = decoded_path.split("/")[1:]
    if not segments:
        return None
    for segment in segments:
        if not segment or segment in (".", ".."):
            return None
    return decoded_path


def _sidecar_from_manifest_entry(
    entry: Dict[str, object], diagnostics: Optional[Dict[str, Any]] = None
) -> Optional[Dict[str, str]]:
    raw = entry.get("sidecar")
    if raw is None:
        return None
    if not isinstance(raw, dict):
        _add_diagnostic_warning(diagnostics, "sidecar_invalid", _SIDECAR_WARNING_SOURCE)
        return None
    if raw.get("type") != "loopback":
        _add_diagnostic_warning(
            diagnostics, "sidecar_type_unsupported", _SIDECAR_WARNING_SOURCE
        )
        return None
    origin = _normalize_loopback_sidecar_origin(raw.get("origin"))
    if origin is None:
        _add_diagnostic_warning(
            diagnostics, "sidecar_origin_rejected", _SIDECAR_WARNING_SOURCE
        )
        return None
    if "health_path" in raw:
        health_path = _normalize_sidecar_health_path(raw.get("health_path"))
        if health_path is None:
            # Missing health_path defaults to /health; an explicitly invalid path
            # rejects the sidecar so the browser does not probe a declaration the
            # administrator needs to fix.
            _add_diagnostic_warning(
                diagnostics, "sidecar_health_path_rejected", _SIDECAR_WARNING_SOURCE
            )
            return None
    else:
        health_path = _DEFAULT_SIDECAR_HEALTH_PATH
    sidecar_id = _manifest_entry_text(entry, "id")
    name = _manifest_entry_text(entry, "name")
    return {
        "id": sidecar_id,
        "name": name,
        "type": "loopback",
        "origin": origin,
        "health_path": health_path,
        "health_url": f"{origin}{health_path}",
    }


def _manifest_extension_entries(manifest: object) -> List[Tuple[str, int, Dict[str, object]]]:
    extension_entries: object = []
    if isinstance(manifest, dict):
        extension_entries = manifest.get("extensions", [])
    elif isinstance(manifest, list):
        extension_entries = manifest
    entries: List[Tuple[str, int, Dict[str, object]]] = []
    if isinstance(extension_entries, list):
        for index, extension in enumerate(extension_entries):
            if isinstance(extension, dict):
                entries.append((f"manifest.extensions[{index}]", index, extension))
    return entries


def _iter_manifest_entries(
    manifest: object, disabled_ids: Optional[Set[str]] = None
) -> List[Tuple[str, object]]:
    disabled_ids = disabled_ids or set()
    entries: List[Tuple[str, object]] = []
    if isinstance(manifest, dict):
        entries.append(("manifest", manifest))
    for source, _index, extension in _manifest_extension_entries(manifest):
        if extension.get("enabled", True) is False:
            continue
        ext_id = _manifest_entry_text(extension, "id")
        if _valid_extension_id(ext_id) and ext_id in disabled_ids:
            continue
        entries.append((source, extension))
    return entries


def _entry_asset_values(entry: Dict[str, object], key: str) -> List[object]:
    values = entry.get(key, [])
    return values if isinstance(values, list) else []


def _read_manifest_text(manifest_file: Path) -> str:
    with manifest_file.open("rb") as fh:
        data = fh.read(_MAX_MANIFEST_BYTES + 1)
    if len(data) > _MAX_MANIFEST_BYTES:
        raise _ManifestTooLarge("manifest too large")
    return data.decode("utf-8")


def _empty_manifest_status(path_status: str) -> Dict[str, Any]:
    return {
        "configured": path_status != "not_configured",
        "loaded": False,
        "status": path_status,
        "_asset_base": "",
        "entry_count": 0,
        "script_count": 0,
        "stylesheet_count": 0,
        "sidecar_count": 0,
    }


def _manifest_asset_base(root: Path, manifest_file: Path) -> str:
    try:
        rel_parent = manifest_file.parent.relative_to(root).as_posix()
    except ValueError:
        return ""
    return "" if rel_parent == "." else rel_parent


def _gallery_installed_runtime_manifest(
    root: Path, diagnostics: Optional[Dict[str, Any]] = None
) -> Optional[Dict[str, object]]:
    """Build a runtime manifest from gallery-installed extension manifests."""
    install_manifest = _load_install_manifest()
    installed = install_manifest.get("installed", {})
    if not isinstance(installed, dict):
        return None
    entries: List[Dict[str, object]] = []
    for ext_id in sorted(installed):
        if not _valid_extension_id(ext_id):
            continue
        manifest_file = root / ext_id / "manifest.json"
        try:
            if not manifest_file.exists() or not manifest_file.is_file():
                _add_diagnostic_warning(diagnostics, "gallery_manifest_missing", "gallery")
                continue
            manifest = json.loads(_read_manifest_text(manifest_file))
        except _ManifestTooLarge:
            _add_diagnostic_warning(diagnostics, "gallery_manifest_oversized", "gallery")
            continue
        except json.JSONDecodeError:
            _add_diagnostic_warning(diagnostics, "gallery_manifest_malformed", "gallery")
            continue
        except RecursionError:
            _add_diagnostic_warning(diagnostics, "gallery_manifest_too_deeply_nested", "gallery")
            continue
        except (OSError, UnicodeDecodeError):
            _add_diagnostic_warning(diagnostics, "gallery_manifest_unreadable", "gallery")
            continue
        asset_base = ext_id
        if isinstance(manifest, dict):
            top_entry: Dict[str, object] = {"id": ext_id}
            for key in ("name", "enabled", "scripts", "stylesheets", "sidecar", "permissions", "settings_schema"):
                if key in manifest:
                    top_entry[key] = manifest[key]
            if any(
                key in top_entry
                for key in ("scripts", "stylesheets", "sidecar", "permissions", "settings_schema")
            ):
                entries.append(_copy_manifest_entry_with_asset_base(top_entry, asset_base))
        for _source, _index, entry in _manifest_extension_entries(manifest):
            copied = _copy_manifest_entry_with_asset_base(entry, asset_base)
            if not _valid_extension_id(copied.get("id")):
                copied["id"] = ext_id
            entries.append(copied)
    if not entries:
        return None
    return {"extensions": entries}


def _load_manifest_with_status(
    root: Path, diagnostics: Optional[Dict[str, Any]] = None
) -> Tuple[Optional[object], Dict[str, Any]]:
    """Load the configured manifest once, returning sanitized status/warnings."""
    manifest_file, path_status = _manifest_path_with_status(root)
    manifest_status = _empty_manifest_status(path_status)
    if manifest_file is None:
        if path_status == "invalid_path":
            _add_diagnostic_warning(diagnostics, "manifest_invalid_path", "manifest")
        elif path_status == "not_configured":
            manifest = _gallery_installed_runtime_manifest(root, diagnostics)
            if manifest is not None:
                manifest_status.update(
                    {
                        "loaded": True,
                        "status": "gallery_installed",
                        "_asset_base": "",
                    }
                )
                return manifest, manifest_status
        return None, manifest_status
    try:
        if not manifest_file.exists() or not manifest_file.is_file():
            _log.warning("Configured extension manifest was not found")
            manifest_status["status"] = "missing"
            _add_diagnostic_warning(diagnostics, "manifest_missing", "manifest")
            return None, manifest_status
        manifest = json.loads(_read_manifest_text(manifest_file))
        manifest_status.update(
            {
                "loaded": True,
                "status": "loaded",
                "_asset_base": _manifest_asset_base(root, manifest_file),
            }
        )
        return manifest, manifest_status
    except _ManifestTooLarge:
        _log.warning("Configured extension manifest exceeds %d bytes", _MAX_MANIFEST_BYTES)
        manifest_status["status"] = "oversized"
        _add_diagnostic_warning(diagnostics, "manifest_oversized", "manifest")
    except json.JSONDecodeError:
        _log.warning("Configured extension manifest is not valid JSON")
        manifest_status["status"] = "malformed"
        _add_diagnostic_warning(diagnostics, "manifest_malformed", "manifest")
    except RecursionError:
        # A <=64KB but deeply-nested manifest makes json.loads exceed the
        # interpreter recursion limit. Without this, the RecursionError escapes
        # into the app-shell route and every page load 503s. Fail safe.
        _log.warning("Configured extension manifest is too deeply nested")
        manifest_status["status"] = "too_deeply_nested"
        _add_diagnostic_warning(diagnostics, "manifest_too_deeply_nested", "manifest")
    except (OSError, UnicodeDecodeError):
        _log.warning("Configured extension manifest could not be read")
        manifest_status["status"] = "unreadable"
        _add_diagnostic_warning(diagnostics, "manifest_unreadable", "manifest")
    return None, manifest_status


def _manifest_extension_state(
    manifest: object, disabled_ids: Set[str], diagnostics: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
    """Return sanitized per-extension state for manifest extension entries."""
    extension_entries: List[Dict[str, Any]] = []
    known_ids: Set[str] = set()
    manifest_disabled_ids: Set[str] = set()
    seen_ids: Set[str] = set()
    invalid_seen = False
    duplicate_seen = False
    for _source, _index, entry in _manifest_extension_entries(manifest):
        raw_id = _manifest_entry_text(entry, "id")
        if not _valid_extension_id(raw_id):
            invalid_seen = True
            continue
        ext_id = raw_id.strip()
        if ext_id in seen_ids:
            duplicate_seen = True
            continue
        seen_ids.add(ext_id)
        known_ids.add(ext_id)
        name = _manifest_entry_text(entry, "name")
        manifest_enabled = entry.get("enabled", True) is not False
        user_disabled = ext_id in disabled_ids
        can_toggle = manifest_enabled
        effective_enabled = manifest_enabled and not user_disabled
        if not manifest_enabled:
            manifest_disabled_ids.add(ext_id)
        settings_schema = _sanitize_settings_schema(entry)
        extension_entries.append(
            {
                "id": ext_id,
                "name": name or ext_id,
                "manifest_enabled": manifest_enabled,
                "user_enabled": (not user_disabled) if can_toggle else False,
                "user_disabled": user_disabled,
                "effective_enabled": effective_enabled,
                "can_toggle": can_toggle,
                "reload_required": True,
                "storage_owned": _manifest_entry_storage_owned(entry),
                "settings_schema": settings_schema,
                "status": (
                    "manifest_disabled"
                    if not manifest_enabled
                    else ("user_disabled" if user_disabled else "enabled")
                ),
            }
        )
    if invalid_seen:
        _add_diagnostic_warning(diagnostics, "manifest_extension_id_invalid", "manifest:extensions")
    if duplicate_seen:
        _add_diagnostic_warning(diagnostics, "manifest_extension_id_duplicate", "manifest:extensions")
    stale_ids = sorted(disabled_ids - known_ids)
    if stale_ids:
        _add_diagnostic_warning(diagnostics, "extension_state_unknown_ids", _EXTENSION_STATE_WARNING_SOURCE)
    return {
        "extensions": extension_entries,
        "known_ids": known_ids,
        "manifest_disabled_ids": manifest_disabled_ids,
    }

def _extension_runtime_entries(
    manifest: object, disabled_ids: Optional[Set[str]] = None
) -> List[Dict[str, object]]:
    """Return enabled extension metadata injected before extension scripts run."""
    disabled_ids = disabled_ids or set()
    extensions: List[Dict[str, object]] = []
    seen_ids: Set[str] = set()
    for _source, _index, entry in _manifest_extension_entries(manifest):
        raw_id = _manifest_entry_text(entry, "id")
        if not _valid_extension_id(raw_id):
            continue
        ext_id = raw_id.strip()
        if ext_id in seen_ids:
            continue
        seen_ids.add(ext_id)
        if entry.get("enabled", True) is False or ext_id in disabled_ids:
            continue
        extensions.append(
            {
                "id": ext_id,
                "name": _manifest_entry_text(entry, "name") or ext_id,
                "storage_owned": _manifest_entry_storage_owned(entry),
                "settings_schema": _sanitize_settings_schema(entry),
            }
        )
    return extensions


def _read_manifest_urls_with_diagnostics(
    root: Path,
    diagnostics: Optional[Dict[str, Any]] = None,
    disabled_ids: Optional[Set[str]] = None,
    manifest: Optional[object] = None,
    manifest_status: Optional[Dict[str, Any]] = None,
) -> Tuple[List[str], List[str], List[Dict[str, str]], Dict[str, Any]]:
    disabled_ids = disabled_ids or set()
    if manifest is None or manifest_status is None:
        manifest, manifest_status = _load_manifest_with_status(root, diagnostics)
    if manifest is None:
        return [], [], [], manifest_status

    scripts: List[str] = []
    stylesheets: List[str] = []
    sidecars: List[Dict[str, str]] = []
    asset_base = str(manifest_status.get("_asset_base", "") or "")
    entries = _iter_manifest_entries(manifest, disabled_ids=disabled_ids)
    manifest_status["entry_count"] = len(entries)
    scripts_full = False
    stylesheets_full = False
    for _source, entry in entries:
        if not isinstance(entry, dict):
            continue
        if _source.startswith("manifest.extensions["):
            sidecar = _sidecar_from_manifest_entry(entry, diagnostics)
            if sidecar is not None:
                if len(sidecars) < _MAX_URL_LIST:
                    sidecars.append(sidecar)
                else:
                    _add_diagnostic_warning(
                        diagnostics, "sidecar_list_truncated", _SIDECAR_WARNING_SOURCE
                    )
        script_source = "manifest:scripts"
        stylesheet_source = "manifest:stylesheets"
        if not scripts_full:
            for value in _entry_asset_values(entry, "scripts"):
                if not _append_safe_asset_url(
                    scripts,
                    _manifest_asset_url(value, asset_base),
                    script_source,
                    diagnostics=diagnostics,
                ):
                    scripts_full = True
                    break
        if not stylesheets_full:
            for value in _entry_asset_values(entry, "stylesheets"):
                if not _append_safe_asset_url(
                    stylesheets,
                    _manifest_asset_url(value, asset_base),
                    stylesheet_source,
                    diagnostics=diagnostics,
                ):
                    stylesheets_full = True
                    break
    manifest_status.update(
        {
            "loaded": True,
            "status": manifest_status.get("status") or "loaded",
            "script_count": len(scripts),
            "stylesheet_count": len(stylesheets),
            "sidecar_count": len(sidecars),
        }
    )
    return scripts, stylesheets, sidecars, manifest_status


def _read_manifest_urls(
    root: Path, disabled_ids: Optional[Set[str]] = None
) -> Tuple[List[str], List[str]]:
    scripts, stylesheets, _, _ = _read_manifest_urls_with_diagnostics(
        root, disabled_ids=disabled_ids
    )
    return scripts, stylesheets


def get_extension_config() -> Dict[str, Any]:
    """Return public extension config without exposing filesystem paths."""
    root = _extension_root()
    if root is None:
        return {"enabled": False, "script_urls": [], "stylesheet_urls": []}
    state = _load_extension_state()
    disabled_ids = set(state.get("disabled_extensions") or [])
    manifest, manifest_status = _load_manifest_with_status(root)
    manifest_scripts, manifest_stylesheets, _, _ = _read_manifest_urls_with_diagnostics(
        root,
        disabled_ids=disabled_ids,
        manifest=manifest,
        manifest_status=manifest_status,
    )
    config = {
        "enabled": True,
        "script_urls": _read_url_list(
            _EXTENSION_SCRIPT_URLS_ENV, manifest_scripts or None
        ),
        "stylesheet_urls": _read_url_list(
            _EXTENSION_STYLESHEET_URLS_ENV, manifest_stylesheets or None
        ),
    }
    runtime_entries = _extension_runtime_entries(manifest, disabled_ids) if manifest is not None else []
    if runtime_entries:
        config["extensions"] = runtime_entries
    return config



def get_extension_status() -> Dict[str, Any]:
    """Return sanitized extension diagnostics for administrators."""
    diagnostics = _new_diagnostics()
    root, dir_configured, dir_valid = _extension_root_status()
    state = _load_extension_state(diagnostics)
    disabled_ids = set(state.get("disabled_extensions") or [])
    manifest_configured = bool(os.getenv(_EXTENSION_MANIFEST_ENV, "").strip())
    manifest_status: Dict[str, Any] = {
        "configured": manifest_configured,
        "loaded": False,
        "status": "extension_disabled" if manifest_configured else "not_configured",
        "entry_count": 0,
        "script_count": 0,
        "stylesheet_count": 0,
        "sidecar_count": 0,
    }
    # Only warn about an unavailable directory when the admin explicitly set
    # HERMES_WEBUI_EXTENSION_DIR to a path that is missing/not-a-dir. The
    # WebUI-managed default simply not existing yet (pre-first-install) is the
    # normal opt-in state, not a misconfiguration worth surfacing.
    env_dir_set = bool(os.getenv(_EXTENSION_DIR_ENV, "").strip())
    if env_dir_set and dir_configured and not dir_valid:
        _add_diagnostic_warning(diagnostics, "extension_dir_unavailable", "extension_dir")

    if root is None:
        return {
            "enabled": False,
            "extension_dir_configured": dir_configured,
            "extension_dir_valid": False,
            "script_urls": [],
            "stylesheet_urls": [],
            "sidecars": [],
            "counts": {
                "script_urls": 0,
                "stylesheet_urls": 0,
                "sidecars": 0,
                "manifest_extensions": 0,
                "user_disabled": 0,
            },
            "manifest": manifest_status,
            "extensions": [],
            "warnings": diagnostics["warnings"],
        }

    manifest, manifest_status = _load_manifest_with_status(root, diagnostics)
    extension_state = _manifest_extension_state(manifest, disabled_ids, diagnostics) if manifest is not None else {
        "extensions": [],
        "known_ids": set(),
        "manifest_disabled_ids": set(),
    }
    manifest_scripts, manifest_stylesheets, sidecars, manifest_status = _read_manifest_urls_with_diagnostics(
        root,
        diagnostics,
        disabled_ids=disabled_ids,
        manifest=manifest,
        manifest_status=manifest_status,
    )
    extensions = extension_state["extensions"]
    known_ids = extension_state["known_ids"]
    user_disabled_count = len(disabled_ids & known_ids)
    script_urls = _read_url_list(
        _EXTENSION_SCRIPT_URLS_ENV,
        manifest_scripts or None,
        diagnostics=diagnostics,
    )
    stylesheet_urls = _read_url_list(
        _EXTENSION_STYLESHEET_URLS_ENV,
        manifest_stylesheets or None,
        diagnostics=diagnostics,
    )
    public_manifest_status = {
        key: value for key, value in manifest_status.items() if not key.startswith("_")
    }
    return {
        "enabled": True,
        "extension_dir_configured": True,
        "extension_dir_valid": True,
        "script_urls": script_urls,
        "stylesheet_urls": stylesheet_urls,
        "sidecars": sidecars,
        "counts": {
            "script_urls": len(script_urls),
            "stylesheet_urls": len(stylesheet_urls),
            "sidecars": len(sidecars),
            "manifest_extensions": len(extensions),
            "user_disabled": user_disabled_count,
        },
        "manifest": public_manifest_status,
        "extensions": extensions,
        "gallery_installed": _load_install_manifest().get("installed", {}),
        "warnings": diagnostics["warnings"],
    }


def set_extension_user_enabled(extension_id: object, enabled: object) -> Dict[str, Any]:
    """Set the UI-managed enabled override for an installed manifest extension."""
    if not _valid_extension_id(extension_id):
        raise ExtensionToggleError("Invalid extension id", status=400)
    ext_id = str(extension_id).strip()
    if not isinstance(enabled, bool):
        raise ExtensionToggleError("enabled must be a boolean", status=400)
    root = _extension_root()
    if root is None:
        raise ExtensionToggleError("Extensions are not configured", status=404)
    with _EXTENSION_STATE_LOCK:
        diagnostics = _new_diagnostics()
        state = _load_extension_state(diagnostics)
        disabled_ids = set(state.get("disabled_extensions") or [])
        manifest, manifest_status = _load_manifest_with_status(root, diagnostics)
        if manifest is None or not manifest_status.get("loaded", False):
            raise ExtensionToggleError("Extension manifest is not loaded", status=409)
        extension_state = _manifest_extension_state(manifest, disabled_ids, diagnostics)
        known_ids: Set[str] = extension_state["known_ids"]
        manifest_disabled_ids: Set[str] = extension_state["manifest_disabled_ids"]
        if ext_id not in known_ids:
            raise ExtensionToggleError("Extension not found", status=404)
        if ext_id in manifest_disabled_ids:
            raise ExtensionToggleError("Extension is disabled by its manifest", status=409)
        if enabled:
            disabled_ids.discard(ext_id)
        else:
            disabled_ids.add(ext_id)
        _write_extension_state({"disabled_extensions": sorted(disabled_ids)})
    # Return a fresh status snapshot after the atomic write is visible. Keeping
    # the readback outside the lock avoids doing the full manifest/status parse
    # while blocking other toggles; a concurrent toggle may be reflected too,
    # which is fine because the UI re-renders from the current effective state.
    return get_extension_status()


def _install_manifest_file() -> Path:
    return _extension_state_dir() / _GALLERY_INSTALL_STATE_FILENAME


def _empty_install_manifest() -> Dict[str, Any]:
    return {"version": 1, "installed": {}}


def _load_install_manifest() -> Dict[str, Any]:
    """Load gallery install manifest, failing safe on any error."""
    mfile = _install_manifest_file()
    try:
        if not mfile.exists() or not mfile.is_file():
            return _empty_install_manifest()
        with mfile.open("rb") as fh:
            raw = fh.read(_MAX_INSTALL_MANIFEST_BYTES + 1)
        if len(raw) > _MAX_INSTALL_MANIFEST_BYTES:
            return _empty_install_manifest()
        parsed = json.loads(raw.decode("utf-8"))
    except (OSError, UnicodeDecodeError, json.JSONDecodeError, RecursionError):
        return _empty_install_manifest()
    if not isinstance(parsed, dict) or not isinstance(parsed.get("installed"), dict):
        return _empty_install_manifest()
    installed: Dict[str, Any] = {}
    for ext_id, entry in parsed["installed"].items():
        if not _valid_extension_id(ext_id):
            continue
        if not isinstance(entry, dict):
            continue
        files = entry.get("files", [])
        if not isinstance(files, list):
            continue
        installed[ext_id] = {
            "version": str(entry.get("version", "unknown")),
            "files": [f for f in files if isinstance(f, str)],
            "installed_at": str(entry.get("installed_at", "")),
        }
        if len(installed) >= _MAX_GALLERY_INSTALLED_IDS:
            break
    return {"version": 1, "installed": installed}


def _write_install_manifest(manifest: Dict[str, Any]) -> None:
    """Persist install manifest with atomic same-directory replace."""
    target = _install_manifest_file()
    target.parent.mkdir(parents=True, exist_ok=True)
    tmp = target.with_name(f".{target.name}.{os.getpid()}.{threading.get_ident()}.tmp")
    data = json.dumps(manifest, ensure_ascii=False, indent=2).encode("utf-8")
    try:
        with tmp.open("wb") as fh:
            fh.write(data)
            fh.flush()
            os.fsync(fh.fileno())
        os.replace(tmp, target)
    finally:
        try:
            if tmp.exists():
                tmp.unlink()
        except OSError:
            pass


def install_extension(id: object, download_url: object, sha256: object) -> Dict[str, Any]:
    """Download, verify, and extract a gallery extension."""
    if not _valid_extension_id(id):
        raise ExtensionInstallError("Invalid extension id")
    ext_id = str(id).strip()
    if not isinstance(download_url, str) or not download_url.startswith("https://"):
        raise ExtensionInstallError("Invalid download URL")
    parsed_url = urlsplit(download_url)
    if parsed_url.hostname not in _REGISTRY_ALLOWED_DOWNLOAD_HOSTS:
        raise ExtensionInstallError("Invalid download URL")
    if not isinstance(sha256, str) or not re.fullmatch(r"[0-9a-f]{64}", sha256):
        raise ExtensionInstallError("Invalid sha256")
    root = _writable_extension_root()
    if root is None:
        raise ExtensionInstallError("Extensions not configured", 404)
    try:
        raw_data = _safe_download(download_url, _MAX_ZIP_DOWNLOAD_BYTES)
    except ExtensionInstallError:
        raise
    except Exception as exc:
        raise ExtensionInstallError("Download failed", 502) from exc
    if len(raw_data) > _MAX_ZIP_DOWNLOAD_BYTES:
        raise ExtensionInstallError("Download too large")
    if hashlib.sha256(raw_data).hexdigest() != sha256:
        raise ExtensionInstallError("SHA-256 mismatch")
    try:
        zf = zipfile.ZipFile(io.BytesIO(raw_data))
    except zipfile.BadZipFile as exc:
        raise ExtensionInstallError("Invalid zip archive") from exc
    ext_dir = root / ext_id
    member_names = zf.namelist()
    total_uncompressed = sum(info.file_size for info in zf.infolist() if not info.is_dir())
    if total_uncompressed > _MAX_ZIP_DOWNLOAD_BYTES * 10:
        raise ExtensionInstallError("Archive uncompressed size exceeds limit")
    file_members = [n for n in member_names if n and not n.endswith("/")]
    if len(file_members) > 1024:
        raise ExtensionInstallError("Archive contains too many files")
    # Detect and strip a single top-level directory prefix matching the extension id.
    # Registry artifacts root files under <id>/ (e.g. desktop-companion/manifest.json).
    strip_prefix = ""
    candidate = ext_id + "/"
    if all(n.startswith(candidate) for n in file_members):
        strip_prefix = candidate
    def _stripped(name: str) -> str:
        if strip_prefix and name.startswith(strip_prefix):
            return name[len(strip_prefix):]
        return name
    root_resolved = root.resolve()
    ext_dir_resolved = ext_dir.resolve()
    for member_name in file_members:
        decoded = _fully_unquote_path(_stripped(member_name))
        if not decoded or not _is_safe_relative_path(decoded):
            raise ExtensionInstallError("Unsafe archive member")
        resolved = (ext_dir / decoded).resolve()
        try:
            resolved.relative_to(root_resolved)
        except ValueError as exc:
            raise ExtensionInstallError("Zip-slip detected") from exc
        try:
            resolved.relative_to(ext_dir_resolved)
        except ValueError as exc:
            raise ExtensionInstallError("Zip-slip detected") from exc
    # Determine version from extension.json or manifest.json in zip
    version = "unknown"
    for vfile in ("extension.json", "manifest.json"):
        candidate_name = strip_prefix + vfile if strip_prefix else vfile
        if candidate_name in member_names:
            try:
                mdata = json.loads(zf.read(candidate_name).decode("utf-8"))
                if isinstance(mdata, dict) and isinstance(mdata.get("version"), str):
                    version = mdata["version"]
                    break
            except Exception:
                pass
    with _EXTENSION_STATE_LOCK:
        ext_dir.mkdir(parents=True, exist_ok=True)
        if ext_dir.is_symlink():
            raise ExtensionInstallError("Extension directory is a symlink", 400)
        rollback: List[Path] = []
        try:
            for member_name in file_members:
                decoded = _fully_unquote_path(_stripped(member_name))
                dest = (ext_dir / decoded).resolve()
                dest.parent.mkdir(parents=True, exist_ok=True)
                dest.write_bytes(zf.read(member_name))
                rollback.append(dest)
        except Exception as exc:
            for path in rollback:
                try:
                    path.unlink(missing_ok=True)
                except OSError:
                    pass
            try:
                if ext_dir.exists() and not any(ext_dir.iterdir()):
                    ext_dir.rmdir()
            except OSError:
                pass
            raise ExtensionInstallError("Extraction failed", 500) from exc
        try:
            manifest = _load_install_manifest()
            from datetime import datetime, timezone
            rel_files = [p.relative_to(ext_dir_resolved).as_posix() for p in rollback]
            manifest["installed"][ext_id] = {
                "version": version,
                "files": rel_files,
                "installed_at": datetime.now(timezone.utc).isoformat(),
            }
            encoded = json.dumps(manifest, ensure_ascii=False, indent=2).encode("utf-8")
            if len(encoded) > _MAX_INSTALL_MANIFEST_BYTES:
                raise ExtensionInstallError("Install manifest would exceed size limit")
            _write_install_manifest(manifest)
        except ExtensionInstallError:
            for path in rollback:
                try:
                    path.unlink(missing_ok=True)
                except OSError:
                    pass
            try:
                if ext_dir.exists() and not any(ext_dir.iterdir()):
                    ext_dir.rmdir()
            except OSError:
                pass
            raise
        except Exception as exc:
            for path in rollback:
                try:
                    path.unlink(missing_ok=True)
                except OSError:
                    pass
            try:
                if ext_dir.exists() and not any(ext_dir.iterdir()):
                    ext_dir.rmdir()
            except OSError:
                pass
            raise ExtensionInstallError("Failed to record install", 500) from exc
    return {"installed": True, "id": ext_id, "version": version}


def uninstall_extension(id: object) -> Dict[str, Any]:
    """Remove a gallery-installed extension's files and manifest entry."""
    if not _valid_extension_id(id):
        raise ExtensionInstallError("Invalid extension id")
    ext_id = str(id).strip()
    root = _extension_root()
    if root is None:
        raise ExtensionInstallError("Extensions not configured", 404)
    with _EXTENSION_STATE_LOCK:
        manifest = _load_install_manifest()
        entry = manifest["installed"].get(ext_id)
        if entry is None:
            raise ExtensionInstallError("Extension not installed", 404)
        ext_dir = root / ext_id
        for rel_path in entry.get("files", []):
            if not _is_safe_relative_path(rel_path):
                continue
            target = (ext_dir / rel_path).resolve()
            try:
                target.relative_to(ext_dir.resolve())
            except ValueError:
                continue
            try:
                target.unlink(missing_ok=True)
            except OSError:
                pass
        # Remove empty directories bottom-up
        if ext_dir.exists():
            for dirpath in sorted(
                (d for d in ext_dir.rglob("*") if d.is_dir()),
                key=lambda p: len(p.parts),
                reverse=True,
            ):
                try:
                    if not any(dirpath.iterdir()):
                        dirpath.rmdir()
                except OSError:
                    pass
            try:
                if not any(ext_dir.iterdir()):
                    ext_dir.rmdir()
            except OSError:
                pass
        del manifest["installed"][ext_id]
        _write_install_manifest(manifest)
    return {"uninstalled": True, "id": ext_id}


def get_extension_registry() -> Dict[str, Any]:
    """Fetch the extension registry with a 5-minute TTL cache."""
    with _REGISTRY_LOCK:
        now = time.monotonic()
        cached = _REGISTRY_CACHE.get("data")
        cached_at = _REGISTRY_CACHE.get("fetched_at", 0.0)
        if cached is not None and (now - cached_at) < _REGISTRY_TTL_SECONDS:
            return {"entries": cached}
        try:
            raw = urlopen(_REGISTRY_URL, timeout=10).read(2 * 1024 * 1024)
            data = json.loads(raw.decode("utf-8"))
            if isinstance(data, list):
                entries = data
            elif isinstance(data, dict):
                entries = data.get("extensions") or data.get("entries") or []
            else:
                entries = []
            if not isinstance(entries, list):
                entries = []
            _REGISTRY_CACHE["data"] = entries
            _REGISTRY_CACHE["fetched_at"] = now
            return {"entries": entries}
        except Exception:
            return {"entries": [], "error": "registry_unavailable"}


def inject_extension_tags(index_html: str) -> str:
    """Inject configured extension tags into the app shell.

    Tags are inserted only when the extension directory is enabled. URLs are
    escaped even though they are already validated, keeping the renderer robust
    if validation rules evolve later.
    """
    config = get_extension_config()
    if not config["enabled"]:
        return index_html

    result = index_html
    stylesheet_tags = [
        '<link rel="stylesheet" href="{}">'.format(html.escape(url, quote=True))
        for url in config["stylesheet_urls"]
    ]
    script_tags = [
        '<script src="{}" defer></script>'.format(html.escape(url, quote=True))
        for url in config["script_urls"]
    ]
    runtime_config = {
        "extensions": config.get("extensions", []),
    }
    runtime_json = json.dumps(runtime_config, ensure_ascii=False, separators=(",", ":")).replace("<", "\\u003c")
    runtime_tag = (
        "<script>window.__HERMES_EXTENSION_CONFIG__={};"
        "if(window.HermesExtensionSettings)window.HermesExtensionSettings.primeFromStatus(window.__HERMES_EXTENSION_CONFIG__);"
        "</script>"
    ).format(runtime_json)

    if stylesheet_tags:
        head_marker = "</head>"
        block = "\n".join(stylesheet_tags) + "\n"
        if head_marker in result:
            result = result.replace(head_marker, block + head_marker, 1)
        else:
            result = block + result

    if runtime_config["extensions"] or script_tags:
        body_marker = "</body>"
        block = runtime_tag + "\n"
        if script_tags:
            block += "\n".join(script_tags) + "\n"
        if body_marker in result:
            result = result.replace(body_marker, block + body_marker, 1)
        else:
            result = result + "\n" + block

    return result


def _is_safe_relative_path(rel: str) -> bool:
    if not rel or "\x00" in rel or "\\" in rel:
        return False
    for segment in rel.split("/"):
        if not segment or segment in (".", "..") or segment.startswith("."):
            return False
    return True


def _not_found(handler) -> bool:
    j(handler, {"error": "not found"}, status=404)
    return True


def serve_extension_static(handler, parsed) -> bool:
    """Serve a file from the configured extension directory.

    The function always returns True for /extensions/* requests: either a file
    response or a 404. It never reveals why a request failed, which avoids
    leaking local paths or extension configuration details.
    """
    root = _extension_root()
    if root is None:
        return _not_found(handler)

    rel = unquote(parsed.path[len(EXTENSION_ROUTE_PREFIX) :])
    if not _is_safe_relative_path(rel):
        return _not_found(handler)

    static_file = (root / rel).resolve()
    try:
        static_file.relative_to(root)
    except ValueError:
        return _not_found(handler)

    if not static_file.exists() or not static_file.is_file():
        return _not_found(handler)

    ct = _EXTENSION_MIME.get(static_file.suffix.lower().lstrip("."), "text/plain")
    ct_header = "{}; charset=utf-8".format(ct) if ct in _TEXT_MIME_TYPES else ct
    try:
        raw = static_file.read_bytes()
    except OSError:
        return _not_found(handler)

    handler.send_response(200)
    handler.send_header("Content-Type", ct_header)
    handler.send_header("Cache-Control", "no-store")
    handler.send_header("Content-Length", str(len(raw)))
    _security_headers(handler)
    handler.end_headers()
    handler.wfile.write(raw)
    return True