Repository navigation
Expand file tree
/
Copy pathidl_validator.py
More file actions
1187 lines (994 loc) · 39.2 KB
/
Copy pathidl_validator.py
File metadata and controls
1187 lines (994 loc) · 39.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Anchor IDL Security Validator
Validates Anchor IDL JSON files for security issues including:
- Missing signer constraints on authority/admin accounts
- Missing mutability constraints on writable accounts
- PDA seed validation
- Missing has_one constraints for ownership enforcement
- CPI tracing and program ID verification
Example:
>>> from idl_validator import validate_idl
>>> findings = validate_idl("target/idl/my_program.json")
>>> for finding in findings:
... print(f"{finding['severity']}: {finding['description']}")
"""
from __future__ import annotations
import json
import os
import re
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Set
from logger import get_logger
from exceptions import CounterscarpValidationError, CounterscarpAnalysisError
from path_security import sanitize_cli_path
logger = get_logger(__name__)
@dataclass
class IDLAccount:
"""Represents an account in an Anchor IDL.
Attributes:
name: Account name.
is_mut: Whether the account is mutable.
is_signer: Whether the account is a signer.
is_optional: Whether the account is optional.
docs: Documentation strings for the account.
pda: PDA configuration if applicable.
relations: List of has_one relations.
"""
name: str
is_mut: bool = False
is_signer: bool = False
is_optional: bool = False
docs: List[str] = field(default_factory=list)
pda: Optional[Dict[str, Any]] = None
relations: List[str] = field(default_factory=list)
@dataclass
class IDLInstruction:
"""Represents an instruction in an Anchor IDL.
Attributes:
name: Instruction name.
accounts: List of accounts required by the instruction.
args: List of instruction arguments.
docs: Documentation strings for the instruction.
"""
name: str
accounts: List[IDLAccount] = field(default_factory=list)
args: List[Dict[str, Any]] = field(default_factory=list)
docs: List[str] = field(default_factory=list)
@dataclass
class IDLProgram:
"""Represents a parsed Anchor IDL program.
Attributes:
name: Program name.
version: Program version.
instructions: List of program instructions.
accounts: List of account types.
types: List of custom types.
events: List of event types.
errors: List of error codes.
metadata: Additional program metadata.
"""
name: str
version: str
instructions: List[IDLInstruction] = field(default_factory=list)
accounts: List[Dict[str, Any]] = field(default_factory=list)
types: List[Dict[str, Any]] = field(default_factory=list)
events: List[Dict[str, Any]] = field(default_factory=list)
errors: List[Dict[str, Any]] = field(default_factory=list)
metadata: Dict[str, Any] = field(default_factory=dict)
class IDLParser:
"""Parser for Anchor IDL JSON files."""
@staticmethod
def parse(idl_path: str) -> IDLProgram:
"""Parse an Anchor IDL JSON file.
Args:
idl_path: Path to the IDL JSON file.
Returns:
Parsed IDLProgram object.
Raises:
CounterscarpValidationError: If the IDL file is invalid or cannot
be parsed.
"""
safe_idl_path = sanitize_cli_path(idl_path, allowed_suffixes={".json"})
idl_path = str(safe_idl_path)
logger.debug(f"Parsing IDL file: {idl_path}")
if not safe_idl_path.exists():
raise CounterscarpValidationError(
f"IDL file not found: {idl_path}",
details={"path": idl_path}
)
try:
data = json.loads(safe_idl_path.read_text(encoding="utf-8"))
except json.JSONDecodeError as e:
raise CounterscarpValidationError(
f"Invalid JSON in IDL file: {e}",
details={"path": idl_path, "error": str(e)}
) from e
except IOError as e:
raise CounterscarpValidationError(
f"Failed to read IDL file: {e}",
details={"path": idl_path, "error": str(e)}
) from e
# Parse basic program info
name = data.get('name', 'unknown')
version = data.get('version', '0.0.0')
metadata = data.get('metadata', {})
# Parse instructions
instructions = []
for instr_data in data.get('instructions', []):
instruction = IDLParser._parse_instruction(instr_data)
instructions.append(instruction)
# Parse accounts, types, events, errors
accounts = data.get('accounts', [])
types = data.get('types', [])
events = data.get('events', [])
errors = data.get('errors', [])
program = IDLProgram(
name=name,
version=version,
instructions=instructions,
accounts=accounts,
types=types,
events=events,
errors=errors,
metadata=metadata
)
logger.debug(f"Successfully parsed IDL for program: {name} v{version}")
logger.debug(f" Instructions: {len(instructions)}")
logger.debug(f" Accounts: {len(accounts)}")
logger.debug(f" Types: {len(types)}")
return program
@staticmethod
def _parse_instruction(data: Dict[str, Any]) -> IDLInstruction:
"""Parse an instruction from IDL data.
Args:
data: Raw instruction data from IDL.
Returns:
Parsed IDLInstruction object.
"""
name = data.get('name', 'unknown')
docs = data.get('docs', [])
args = data.get('args', [])
accounts = []
for acc_data in data.get('accounts', []):
account = IDLParser._parse_account(acc_data)
accounts.append(account)
return IDLInstruction(
name=name,
accounts=accounts,
args=args,
docs=docs
)
@staticmethod
def _parse_account(data: Dict[str, Any]) -> IDLAccount:
"""Parse an account from IDL data.
Args:
data: Raw account data from IDL.
Returns:
Parsed IDLAccount object.
"""
name = data.get('name', 'unknown')
is_mut = data.get('isMut', False)
is_signer = data.get('isSigner', False)
is_optional = data.get('isOptional', False)
docs = data.get('docs', [])
pda = data.get('pda')
relations = data.get('relations', [])
return IDLAccount(
name=name,
is_mut=is_mut,
is_signer=is_signer,
is_optional=is_optional,
docs=docs,
pda=pda,
relations=relations
)
class ConstraintValidator:
"""Validates account constraints in IDL instructions."""
# Account name patterns that typically require signer
SIGNER_KEYWORDS = {
'authority', 'admin', 'owner', 'signer', 'payer', 'creator',
'initializer', 'updater', 'manager', 'controller', 'fee_payer'
}
# Account name patterns that typically indicate mutable state
MUTABILITY_KEYWORDS = {
'state', 'account', 'data', 'config', 'settings', 'vault',
'pool', 'reserve', 'balance', 'position', 'metadata'
}
@staticmethod
def validate_signer_constraints(
instruction: IDLInstruction
) -> List[Dict[str, Any]]:
"""Validate signer constraints for an instruction.
Detects accounts that should be signers based on naming conventions
but are not marked as such in the IDL.
Args:
instruction: The instruction to validate.
Returns:
List of findings for missing signer constraints.
"""
findings = []
for account in instruction.accounts:
# Skip if already marked as signer
if account.is_signer:
continue
# Skip optional accounts (might be passed as None)
if account.is_optional:
continue
# Check if account name suggests it should be a signer
account_lower = account.name.lower()
should_be_signer = any(
keyword in account_lower
for keyword in ConstraintValidator.SIGNER_KEYWORDS
)
if should_be_signer:
findings.append({
"rule_id": "IDL_MISSING_SIGNER",
"severity": "HIGH",
"description": (
f"Account '{account.name}' in instruction "
f"'{instruction.name}' has a name suggesting it "
f"should be a signer, but 'isSigner' is not set. "
f"This may allow unauthorized transactions."
),
"file": "",
"line_no": 0,
"instruction": instruction.name,
"account": account.name,
"fix_suggestion": (
f"Add 'isSigner: true' to account '{account.name}'"
)
})
return findings
@staticmethod
def validate_mutability_constraints(
instruction: IDLInstruction
) -> List[Dict[str, Any]]:
"""Validate mutability constraints for an instruction.
Detects accounts that are likely to be mutated based on instruction
naming patterns but are not marked as mutable.
Args:
instruction: The instruction to validate.
Returns:
List of findings for potentially missing mutability constraints.
"""
findings: List[Dict[str, Any]] = []
# Instructions that typically mutate state
mutating_prefixes = (
'initialize', 'init', 'create', 'update', 'modify', 'set',
'change', 'deposit', 'withdraw', 'transfer', 'mint', 'burn',
'swap', 'claim', 'stake', 'unstake', 'vote', 'execute'
)
instruction_lower = instruction.name.lower()
is_likely_mutating = instruction_lower.startswith(mutating_prefixes)
if not is_likely_mutating:
return findings
for account in instruction.accounts:
# Skip if already mutable
if account.is_mut:
continue
# Skip signer accounts (often read-only authorities)
if account.is_signer:
continue
# Skip program accounts
if 'program' in account.name.lower():
continue
# Check if account name suggests mutable state
account_lower = account.name.lower()
suggests_state = any(
keyword in account_lower
for keyword in ConstraintValidator.MUTABILITY_KEYWORDS
)
if suggests_state:
findings.append({
"rule_id": "IDL_MISSING_MUTABILITY",
"severity": "MEDIUM",
"description": (
f"Account '{account.name}' in instruction "
f"'{instruction.name}' may need 'isMut: true'. "
f"Instruction name suggests state mutation, but "
f"account is marked read-only."
),
"file": "",
"line_no": 0,
"instruction": instruction.name,
"account": account.name,
"fix_suggestion": (
f"Add 'isMut: true' to account '{account.name}'"
)
})
return findings
@staticmethod
def validate_pda_constraints(
instruction: IDLInstruction
) -> List[Dict[str, Any]]:
"""Validate PDA constraints for an instruction.
Checks that PDA accounts have proper seeds and bump constraints.
Args:
instruction: The instruction to validate.
Returns:
List of findings for PDA constraint issues.
"""
findings = []
for account in instruction.accounts:
# Check if account has PDA configuration
if account.pda is None:
continue
pda = account.pda
# Check for missing seeds
seeds = pda.get('seeds', [])
if not seeds:
findings.append({
"rule_id": "IDL_PDA_MISSING_SEEDS",
"severity": "CRITICAL",
"description": (
f"PDA account '{account.name}' in instruction "
f"'{instruction.name}' has no seeds defined. "
f"PDAs must have at least one seed for derivation."
),
"file": "",
"line_no": 0,
"instruction": instruction.name,
"account": account.name,
"fix_suggestion": (
"Define PDA seeds in the account constraints"
)
})
# Check for missing bump
has_bump = 'bump' in pda or any(
seed.get('kind') == 'bump' for seed in seeds
)
if not has_bump:
findings.append({
"rule_id": "IDL_PDA_MISSING_BUMP",
"severity": "HIGH",
"description": (
f"PDA account '{account.name}' in instruction "
f"'{instruction.name}' may be missing bump "
f"constraint. Explicit bump is recommended for "
f"deterministic PDA derivation."
),
"file": "",
"line_no": 0,
"instruction": instruction.name,
"account": account.name,
"fix_suggestion": "Add bump constraint to PDA definition"
})
# Validate seed types
for seed in seeds:
seed_kind = seed.get('kind', '')
if seed_kind == 'const' and not seed.get('value'):
findings.append({
"rule_id": "IDL_PDA_EMPTY_CONST_SEED",
"severity": "MEDIUM",
"description": (
f"PDA account '{account.name}' has empty "
f"const seed. This may be unintentional."
),
"file": "",
"line_no": 0,
"instruction": instruction.name,
"account": account.name,
"fix_suggestion": (
"Verify const seed value is intentional"
)
})
return findings
@staticmethod
def validate_has_one_constraints(
instruction: IDLInstruction
) -> List[Dict[str, Any]]:
"""Validate has_one constraints for ownership enforcement.
Detects accounts that may need has_one constraints for ownership
validation.
Args:
instruction: The instruction to validate.
Returns:
List of findings for missing has_one constraints.
"""
findings = []
# Find authority-like accounts
authority_accounts = [
acc for acc in instruction.accounts
if any(keyword in acc.name.lower()
for keyword in ['authority', 'owner', 'admin'])
and acc.is_signer
]
for account in instruction.accounts:
# Skip if already has relations
if account.relations:
continue
# Skip signer accounts
if account.is_signer:
continue
# Check if account name suggests ownership relationship
account_lower = account.name.lower()
ownership_indicators = [
'account', 'state', 'data', 'vault', 'pool'
]
if any(ind in account_lower for ind in ownership_indicators):
# Check if there's a matching authority account
for auth in authority_accounts:
# Simple heuristic: if account and authority share context
# e.g., "user_account" and "user_authority"
account_prefix = account_lower.split('_')[0]
auth_prefix = auth.name.lower().split('_')[0]
if account_prefix == auth_prefix:
findings.append({
"rule_id": "IDL_MISSING_HAS_ONE",
"severity": "MEDIUM",
"description": (
f"Account '{account.name}' in instruction "
f"'{instruction.name}' may need a has_one "
f"constraint to enforce ownership by "
f"'{auth.name}'. Without this, ownership "
f"validation must be done manually in "
f"the handler."
),
"file": "",
"line_no": 0,
"instruction": instruction.name,
"account": account.name,
"fix_suggestion": (
f"Add 'has_one = {auth.name}' constraint to "
f"account '{account.name}'"
)
})
return findings
class CPITracer:
"""Traces Cross-Program Invocations (CPI) in IDL."""
# Known program IDs for common Solana programs
KNOWN_PROGRAMS = {
'token': 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
'token_2022': 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb',
'associated_token': 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL',
'system': '11111111111111111111111111111111',
'metadata': 'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s',
}
@staticmethod
def detect_cpi_accounts(
instruction: IDLInstruction
) -> List[Dict[str, Any]]:
"""Detect accounts that may be used for CPI.
Identifies instruction accounts that appear to be program accounts
or token accounts used in CPI calls.
Args:
instruction: The instruction to analyze.
Returns:
List of findings for potential CPI security issues.
"""
findings = []
for account in instruction.accounts:
account_lower = account.name.lower()
# Detect program accounts
if 'program' in account_lower:
findings.append({
"rule_id": "IDL_CPI_PROGRAM_ACCOUNT",
"severity": "INFO",
"description": (
f"Account '{account.name}' in instruction "
f"'{instruction.name}' appears to be a program "
f"account. Ensure program ID is verified before CPI."
),
"file": "",
"line_no": 0,
"instruction": instruction.name,
"account": account.name,
"fix_suggestion": (
"Verify program ID matches expected before invocation"
)
})
# Detect token-related CPI
token_keywords = ['token_program', 'token']
if any(keyword in account_lower for keyword in token_keywords):
if not account.is_signer and not account.is_mut:
findings.append({
"rule_id": "IDL_CPI_TOKEN_PROGRAM",
"severity": "LOW",
"description": (
f"Token program account '{account.name}' "
f"detected in instruction '{instruction.name}'. "
f"Ensure proper token program ID verification."
),
"file": "",
"line_no": 0,
"instruction": instruction.name,
"account": account.name,
"fix_suggestion": (
"Verify token_program.key == &token::ID"
)
})
return findings
@staticmethod
def build_cpi_flow_matrix(
program: IDLProgram
) -> Dict[str, List[Dict[str, Any]]]:
"""Build a CPI flow matrix for the program.
Maps which instructions may call which programs based on
account usage patterns.
Args:
program: The parsed IDL program.
Returns:
Dictionary mapping instruction names to potential CPI targets.
"""
cpi_flow = {}
for instruction in program.instructions:
targets = []
for account in instruction.accounts:
account_lower = account.name.lower()
# Map program accounts to potential CPI targets
if 'token_program' in account_lower:
targets.append({
"program": "token",
"account": account.name,
"program_id": CPITracer.KNOWN_PROGRAMS.get(
'token', ''
),
"risk": "Verify program ID before CPI"
})
elif 'system_program' in account_lower:
targets.append({
"program": "system",
"account": account.name,
"program_id": CPITracer.KNOWN_PROGRAMS.get(
'system', ''
),
"risk": "Low - system program is standard"
})
elif 'associated_token' in account_lower:
targets.append({
"program": "associated_token",
"account": account.name,
"program_id": CPITracer.KNOWN_PROGRAMS.get(
'associated_token', ''
),
"risk": "Verify program ID before CPI"
})
elif ('metadata' in account_lower and
'program' in account_lower):
targets.append({
"program": "metadata",
"account": account.name,
"program_id": CPITracer.KNOWN_PROGRAMS.get(
'metadata', ''
),
"risk": "Verify program ID before CPI"
})
if targets:
cpi_flow[instruction.name] = targets
return cpi_flow
class AccountPermissionMatrix:
"""Generates account permission matrices for IDL programs."""
@staticmethod
def generate_matrix(program: IDLProgram) -> Dict[str, Any]:
"""Generate a permission matrix for all instructions.
Creates a structured matrix showing:
- Each instruction × which accounts it requires
- Access level per account (read/write/signer)
- PDA derivation info
Args:
program: The parsed IDL program.
Returns:
Dictionary containing the permission matrix.
"""
matrix: Dict[str, Any] = {
"program_name": program.name,
"program_version": program.version,
"instructions": {},
"account_summary": {}
}
# Collect all unique accounts across instructions
all_accounts: Set[str] = set()
account_usage: Dict[str, Dict[str, Any]] = {}
for instruction in program.instructions:
instr_matrix: Dict[str, Any] = {
"accounts": {},
"signer_required": False,
"mutable_accounts": [],
"pda_accounts": []
}
for account in instruction.accounts:
all_accounts.add(account.name)
# Track account usage
if account.name not in account_usage:
account_usage[account.name] = {
"used_by": [],
"is_signer_count": 0,
"is_mut_count": 0,
"is_pda": account.pda is not None
}
account_usage[account.name]["used_by"].append(instruction.name)
if account.is_signer:
account_usage[account.name]["is_signer_count"] += 1
instr_matrix["signer_required"] = True
if account.is_mut:
account_usage[account.name]["is_mut_count"] += 1
instr_matrix["mutable_accounts"].append(account.name)
if account.pda is not None:
instr_matrix["pda_accounts"].append({
"name": account.name,
"seeds": account.pda.get('seeds', []),
"bump": account.pda.get('bump')
})
# Determine access level
if account.is_signer and account.is_mut:
access_level = "signer+write"
elif account.is_signer:
access_level = "signer"
elif account.is_mut:
access_level = "write"
else:
access_level = "read"
instr_matrix["accounts"][account.name] = {
"access_level": access_level,
"is_optional": account.is_optional,
"is_pda": account.pda is not None,
"relations": account.relations
}
matrix["instructions"][instruction.name] = instr_matrix
matrix["account_summary"] = account_usage
matrix["total_accounts"] = len(all_accounts)
matrix["total_instructions"] = len(program.instructions)
return matrix
@staticmethod
def print_matrix(matrix: Dict[str, Any]) -> str:
"""Format the permission matrix as a readable string.
Args:
matrix: The permission matrix dictionary.
Returns:
Formatted string representation.
"""
lines = []
lines.append("=" * 80)
prog_name = matrix['program_name']
prog_ver = matrix['program_version']
lines.append(f"Account Permission Matrix: {prog_name} v{prog_ver}")
lines.append("=" * 80)
lines.append("")
# Summary
lines.append(f"Total Instructions: {matrix['total_instructions']}")
lines.append(f"Total Unique Accounts: {matrix['total_accounts']}")
lines.append("")
# Per-instruction breakdown
lines.append("-" * 80)
lines.append("Instruction Account Requirements:")
lines.append("-" * 80)
for instr_name, instr_data in matrix["instructions"].items():
lines.append(f"\n {instr_name}:")
if instr_data["signer_required"]:
lines.append(" [SIGNER REQUIRED]")
for acc_name, acc_data in instr_data["accounts"].items():
access = acc_data["access_level"]
optional = " (optional)" if acc_data["is_optional"] else ""
pda = " [PDA]" if acc_data["is_pda"] else ""
lines.append(f" - {acc_name}: {access}{optional}{pda}")
lines.append("")
lines.append("-" * 80)
lines.append("Account Usage Summary:")
lines.append("-" * 80)
for acc_name, usage in matrix["account_summary"].items():
used_count = len(usage["used_by"])
signer_pct = (
usage["is_signer_count"] / used_count * 100
) if used_count > 0 else 0
mut_pct = (
usage["is_mut_count"] / used_count * 100
) if used_count > 0 else 0
pda_marker = " [PDA]" if usage["is_pda"] else ""
lines.append(f"\n {acc_name}{pda_marker}:")
lines.append(f" Used by: {used_count} instruction(s)")
lines.append(f" As signer: {signer_pct:.0f}%")
lines.append(f" As mutable: {mut_pct:.0f}%")
lines.append("")
lines.append("=" * 80)
return "\n".join(lines)
def validate_idl(
idl_path: str,
program_source_dir: Optional[str] = None,
validate_constraints: bool = True,
trace_cpi: bool = True
) -> List[Dict[str, Any]]:
"""Validate an Anchor IDL file for security issues.
Main entry point for IDL validation. Parses the IDL file and runs
all security checks.
Args:
idl_path: Path to the IDL JSON file.
program_source_dir: Optional path to Rust source for cross-referencing.
validate_constraints: Whether to run constraint validation.
trace_cpi: Whether to run CPI tracing.
Returns:
List of findings as dictionaries.
Raises:
CounterscarpAnalysisError: If validation fails unexpectedly.
"""
logger.info(f"Starting IDL validation: {idl_path}")
findings = []
try:
# Parse the IDL
program = IDLParser.parse(idl_path)
# Run constraint validation
if validate_constraints:
logger.debug("Running constraint validation...")
for instruction in program.instructions:
# Signer constraints
findings.extend(
ConstraintValidator.validate_signer_constraints(
instruction
)
)
# Mutability constraints
findings.extend(
ConstraintValidator.validate_mutability_constraints(
instruction
)
)
# PDA constraints
findings.extend(
ConstraintValidator.validate_pda_constraints(instruction)
)
# has_one constraints
findings.extend(
ConstraintValidator.validate_has_one_constraints(
instruction
)
)
# Run CPI tracing
if trace_cpi:
logger.debug("Running CPI tracing...")
for instruction in program.instructions:
findings.extend(
CPITracer.detect_cpi_accounts(instruction)
)
# Cross-reference with source if provided
if program_source_dir and os.path.exists(program_source_dir):
logger.debug(
f"Cross-referencing with source: {program_source_dir}"
)
source_findings = _cross_reference_source(
program, program_source_dir
)
findings.extend(source_findings)
# Add file path to all findings
for finding in findings:
finding["file"] = idl_path
logger.info(
f"IDL validation complete. Found {len(findings)} issue(s)."
)
except CounterscarpValidationError:
raise
except Exception as e:
raise CounterscarpAnalysisError(
f"IDL validation failed: {e}",
details={"path": idl_path, "error": str(e)}
) from e
return findings
def _cross_reference_source(
program: IDLProgram,
source_dir: str
) -> List[Dict[str, Any]]:
"""Cross-reference IDL with Rust source code.
Args:
program: The parsed IDL program.
source_dir: Path to the Rust source directory.
Returns:
List of additional findings from source cross-reference.
"""
safe_source_dir = sanitize_cli_path(
source_dir,
must_exist=True,
expect_file=False,
)
findings = []
# Find all Rust files
rust_files = [
p for p in safe_source_dir.rglob("*.rs")
if "target" not in p.parts
]
# Build a map of instruction handlers
instruction_handlers: Dict[str, str] = {}
for rs_file in rust_files:
try:
safe_rs_file = sanitize_cli_path(str(rs_file), allowed_suffixes={".rs"})
content = safe_rs_file.read_text(encoding="utf-8")
# Look for instruction handlers (pub fn <instruction_name>)
for instruction in program.instructions:
pattern = rf'pub\s+fn\s+{re.escape(instruction.name)}\s*\('
if re.search(pattern, content):
instruction_handlers[instruction.name] = str(rs_file)
except IOError:
continue
# Check for missing handlers
for instruction in program.instructions:
if instruction.name not in instruction_handlers:
findings.append({
"rule_id": "IDL_MISSING_HANDLER",
"severity": "HIGH",
"description": (
f"No handler found for instruction "
f"'{instruction.name}'. Expected a function with "
f"signature 'pub fn {instruction.name}(...)'"
),
"file": "",
"line_no": 0,
"instruction": instruction.name,
"account": "",
"fix_suggestion": (
f"Implement handler function for '{instruction.name}'"
)
})
logger.debug(
f"Found handlers for {len(instruction_handlers)}/"
f"{len(program.instructions)} instructions"
)
return findings
def find_idl_files(project_root: str) -> List[str]:
"""Find IDL files in common Anchor project locations.
Searches for IDL JSON files in:
- target/idl/
- idl/
- Project root
Args:
project_root: Path to the project root directory.