-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
1150 lines (992 loc) · 37.8 KB
/
lib.rs
File metadata and controls
1150 lines (992 loc) · 37.8 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
use anchor_lang::prelude::*;
use anchor_lang::system_program::{self, Transfer};
declare_id!("Arb1tr4ryCpi11111111111111111111111111111111");
#[program]
pub mod arbitrary_cpi_call_tests {
use super::*;
// This CPI uses an unchecked program account and should trigger the lint.
pub fn unchecked_cpi(ctx: Context<UncheckedCpi>, amount: u64) -> Result<()> {
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(ctx.accounts.unchecked_program.key(), cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [arbitrary_cpi_call]
Ok(())
}
// This CPI validates the program id before invoking and should be considered safe.
pub fn validated_cpi(ctx: Context<ValidatedCpi>, amount: u64) -> Result<()> {
require_keys_eq!(
ctx.accounts.unchecked_program.key(),
system_program::ID,
CustomError::InvalidProgram
);
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(ctx.accounts.unchecked_program.key(), cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [safe_cpi_call]
Ok(())
}
// This CPI validates the program id before invoking and should be considered safe.
pub fn validated_cpi_2(
ctx: Context<ValidatedCpi>,
target_program_id: Pubkey,
amount: u64,
) -> Result<()> {
require_keys_eq!(
target_program_id,
system_program::ID,
CustomError::InvalidProgram
);
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(target_program_id, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [safe_cpi_call]
Ok(())
}
// Case 0: Arbitrary CPI target passed as parameter - unsafe
pub fn arbitrary_cpi_from_parameter(
ctx: Context<BasicTransfer>,
target_program_id: Pubkey,
amount: u64,
) -> Result<()> {
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(target_program_id, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [arbitrary_cpi_call]
Ok(())
}
// Case 1: Use constant program id (hardcoded) - safe
pub fn constant_program_id(ctx: Context<BasicTransfer>, amount: u64) -> Result<()> {
const SOME_PROGRAM_ID: Pubkey = Pubkey::new_from_array([42u8; 32]);
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(SOME_PROGRAM_ID, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [safe_cpi_call]
Ok(())
}
// Case 2: Use system program id from Anchor - safe
pub fn system_program_id(ctx: Context<BasicTransfer>, amount: u64) -> Result<()> {
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(system_program::ID, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [safe_cpi_call]
Ok(())
}
// Case 3: Use program id from one of the ctx accounts - unsafe
pub fn program_id_from_account(ctx: Context<AccountBasedTransfer>, amount: u64) -> Result<()> {
let pr = ctx.accounts.unchecked_program.key();
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let mut cpi_ctx = CpiContext::new(pr, cpi_accounts);
cpi_ctx.program_id = system_program::ID;
system_program::transfer(cpi_ctx, amount)?; // [arbitrary_cpi_call]
Ok(())
}
// Case 4: Validate user-provided program ID - safe
pub fn validated_program_id(
ctx: Context<BasicTransfer>,
target_program_id: Pubkey,
amount: u64,
) -> Result<()> {
require!(
target_program_id == system_program::ID,
CustomError::InvalidProgram
);
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(target_program_id, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [safe_cpi_call]
Ok(())
}
// Case 5: Program ID derived from account data - unsafe
pub fn program_id_from_account_data(
ctx: Context<DataBasedTransfer>,
amount: u64,
) -> Result<()> {
let bytes = ctx.accounts.inner.data.to_le_bytes();
let pr = Pubkey::new_from_array(bytes.repeat(4).try_into().unwrap_or([0u8; 32]));
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(pr, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [arbitrary_cpi_call]
Ok(())
}
// Case 6: Program ID computed from a hash - unsafe
pub fn program_id_from_hash(
ctx: Context<BasicTransfer>,
user_seed: Pubkey,
amount: u64,
) -> Result<()> {
let mut hash_bytes = [0u8; 32];
hash_bytes.copy_from_slice(&user_seed.to_bytes()[..32]);
let derived_pr = Pubkey::new_from_array(hash_bytes);
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(derived_pr, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [arbitrary_cpi_call]
Ok(())
}
// Case 7: Proper fix — explicit allowlist for CPI targets - safe
pub fn allowlist_validation(
ctx: Context<BasicTransfer>,
target_program_id: Pubkey,
amount: u64,
) -> Result<()> {
let allowed_programs = vec![system_program::ID, crate::ID];
if !allowed_programs.contains(&target_program_id) {
return err!(CustomError::InvalidProgram);
}
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(target_program_id, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [safe_cpi_call]
Ok(())
}
// Case 8: Program ID taken from PDA - unsafe
pub fn program_id_from_pda(
ctx: Context<BasicTransfer>,
seed: Vec<u8>,
amount: u64,
) -> Result<()> {
let (derived_key, _) = Pubkey::find_program_address(&[&seed], &crate::ID);
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(derived_key, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [arbitrary_cpi_call]
Ok(())
}
// Case 9: Program ID conditionally validated - unsafe (fallback path is unsafe)
pub fn conditional_validation_unsafe(
ctx: Context<BasicTransfer>,
target_program_id: Pubkey,
amount: u64,
) -> Result<()> {
if target_program_id == crate::ID {
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(target_program_id, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [safe_cpi_call]
} else {
// Fallback path still executes CPI (unsafe)
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(target_program_id, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [arbitrary_cpi_call]
}
Ok(())
}
// Case 10: Program ID loaded from account data - unsafe
pub fn program_id_from_serialized(ctx: Context<DataBasedTransfer>, amount: u64) -> Result<()> {
let mut bytes = [0u8; 32];
bytes.copy_from_slice(&ctx.accounts.inner.data.to_le_bytes());
let pr = Pubkey::new_from_array(bytes);
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(pr, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [arbitrary_cpi_call]
Ok(())
}
// Case 11: Validate user-provided program ID with require_keys_eq - safe
pub fn validated_with_require_keys_eq(
ctx: Context<BasicTransfer>,
target_program_id: Pubkey,
amount: u64,
) -> Result<()> {
require_keys_eq!(target_program_id, system_program::ID);
if target_program_id != crate::ID {
return err!(CustomError::InvalidProgram);
}
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(target_program_id, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [safe_cpi_call]
Ok(())
}
// Case 12: Direct invoke_signed with system_instruction::transfer - safe
pub fn direct_invoke_signed_with_system_transfer(
ctx: Context<DirectInvokeTransfer>,
amount: u64,
) -> Result<()> {
use anchor_lang::solana_program::program::invoke_signed;
use anchor_lang::solana_program::system_instruction::transfer;
// Create instruction (program_id is hardcoded to system_program::ID)
let instruction = transfer(&ctx.accounts.from.key(), &ctx.accounts.to.key(), amount);
// Prepare account infos
let account_infos = vec![
ctx.accounts.from.to_account_info(),
ctx.accounts.to.to_account_info(),
];
// Instruction has hardcoded system_program::ID - safe
let signer_seeds: &[&[&[u8]]] = &[];
invoke_signed(
// [safe_cpi_call]
&instruction,
&account_infos,
signer_seeds,
)?;
Ok(())
}
// Case 13: Direct invoke_signed with arbitrary instruction - unsafe
pub fn direct_invoke_signed_with_arbitrary_instruction(
ctx: Context<DirectInvokeTransfer>,
amount: u64,
) -> Result<()> {
use anchor_lang::solana_program::instruction::Instruction;
use anchor_lang::solana_program::program::invoke_signed;
// Create instruction with arbitrary program ID - unsafe
let instruction = Instruction {
program_id: ctx.accounts.unchecked_program.key(), // User-controlled program ID
accounts: vec![],
data: vec![],
};
// Prepare account infos
let account_infos = vec![
ctx.accounts.from.to_account_info(),
ctx.accounts.to.to_account_info(),
];
let signer_seeds: &[&[&[u8]]] = &[];
invoke_signed( // [arbitrary_cpi_call]
&instruction,
&account_infos,
signer_seeds,
)?;
require_keys_eq!(
ctx.accounts.unchecked_program.key(),
system_program::ID,
CustomError::InvalidProgram
);
Ok(())
}
// Case 16: CpiBuilder pattern with validated account (like clayno-solana-staking)
pub fn cpi_builder_with_validated_account(
ctx: Context<CpiBuilderAccounts>,
amount: u64,
) -> Result<()> {
use anchor_spl::token::Token;
// This is safe - token_program is validated by Anchor's account constraints
let cpi_accounts = anchor_spl::token::Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
authority: ctx.accounts.authority.to_account_info(),
};
anchor_spl::token::transfer(
CpiContext::new(ctx.accounts.token_program.key(), cpi_accounts),
amount,
)?; // [safe_cpi_call]
Ok(())
}
// Case 17: CpiBuilder pattern with unchecked account - unsafe
pub fn cpi_builder_with_unchecked_account(
ctx: Context<CpiBuilderUnchecked>,
amount: u64,
) -> Result<()> {
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
// Unsafe - unchecked_program is not validated
let cpi_ctx = CpiContext::new(ctx.accounts.unchecked_program.key(), cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [arbitrary_cpi_call]
Ok(())
}
// Case 18: Conditional token program selection (like raydium-clmm)
pub fn conditional_token_program_safe(
ctx: Context<ConditionalTokenProgram>,
amount: u64,
) -> Result<()> {
let mut token_program_info = ctx.accounts.token_program.to_account_info();
// Validate owner before using
if *ctx.accounts.from.owner == ctx.accounts.token_program_2022.key() {
token_program_info = ctx.accounts.token_program_2022.to_account_info();
}
let cpi_accounts = anchor_spl::token::Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
authority: ctx.accounts.authority.to_account_info(),
};
anchor_spl::token::transfer(
CpiContext::new(token_program_info.key(), cpi_accounts),
amount,
)?; // [safe_cpi_call]
Ok(())
}
// Case 19: Direct invoke_signed with hardcoded program ID (like damm-v2)
pub fn direct_invoke_signed_hardcoded(
ctx: Context<DirectInvokeTransfer>,
amount: u64,
) -> Result<()> {
use anchor_lang::solana_program::program::invoke_signed;
use anchor_lang::solana_program::system_instruction::transfer;
let instruction = transfer(&ctx.accounts.from.key(), &ctx.accounts.to.key(), amount);
let account_infos = vec![
ctx.accounts.from.to_account_info(),
ctx.accounts.to.to_account_info(),
ctx.accounts.system_program.to_account_info(),
];
let signer_seeds: &[&[&[u8]]] = &[];
invoke_signed(&instruction, &account_infos, signer_seeds)?; // [safe_cpi_call]
Ok(())
}
// Case 20: Direct invoke_signed with program ID from account data - unsafe
pub fn direct_invoke_signed_from_account_data(
ctx: Context<DirectInvokeFromData>,
amount: u64,
) -> Result<()> {
use anchor_lang::solana_program::instruction::Instruction;
use anchor_lang::solana_program::program::invoke_signed;
// Read program ID from account data - unsafe
let program_id = Pubkey::try_from(&ctx.accounts.config_account.data.borrow()[0..32])
.map_err(|_| CustomError::InvalidData)?;
let instruction = Instruction {
program_id,
accounts: vec![],
data: vec![],
};
let account_infos = vec![ctx.accounts.config_account.to_account_info()];
let signer_seeds: &[&[&[u8]]] = &[];
invoke_signed(&instruction, &account_infos, signer_seeds)?; // [arbitrary_cpi_call]
Ok(())
}
// Case 21: CPI with program ID from PDA derivation - potentially unsafe
pub fn cpi_with_pda_derived_program(
ctx: Context<PdaDerivedProgram>,
amount: u64,
) -> Result<()> {
let (program_pda, _bump) = Pubkey::find_program_address(
&[b"program", ctx.accounts.authority.key.as_ref()],
&ctx.program_id,
);
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
// Unsafe - PDA-derived program ID could be manipulated
let cpi_ctx = CpiContext::new(program_pda, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [arbitrary_cpi_call]
Ok(())
}
// Case 22: CPI with program ID validated against whitelist - safe
pub fn cpi_with_whitelist_validation(
ctx: Context<WhitelistValidation>,
amount: u64,
) -> Result<()> {
const ALLOWED_PROGRAMS: [Pubkey; 2] = [system_program::ID, anchor_spl::token::ID];
require!(
ALLOWED_PROGRAMS.contains(&ctx.accounts.target_program.key()),
CustomError::InvalidProgram
);
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(ctx.accounts.target_program.key(), cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [safe_cpi_call]
Ok(())
}
// Case 23: CPI with program ID from account but validated - safe
pub fn cpi_with_account_validation(ctx: Context<AccountValidation>, amount: u64) -> Result<()> {
// Validate the program account owner
require_keys_eq!(
*ctx.accounts.target_program.owner,
system_program::ID,
CustomError::InvalidProgram
);
// Validate the program key matches expected
require_keys_eq!(
ctx.accounts.target_program.key(),
system_program::ID,
CustomError::InvalidProgram
);
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(ctx.accounts.target_program.key(), cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [safe_cpi_call]
Ok(())
}
// Case 24: Nested CPI call with validated program - safe
pub fn nested_cpi_with_validation(ctx: Context<NestedCpiAccounts>, amount: u64) -> Result<()> {
// First validate
require_keys_eq!(
ctx.accounts.target_program.key(),
system_program::ID,
CustomError::InvalidProgram
);
// Then make CPI call
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(ctx.accounts.target_program.key(), cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [safe_cpi_call]
// Nested CPI with same validated program
let nested_cpi_accounts = Transfer {
from: ctx.accounts.to.to_account_info(),
to: ctx.accounts.from.to_account_info(),
};
let nested_cpi_ctx =
CpiContext::new(ctx.accounts.target_program.key(), nested_cpi_accounts);
system_program::transfer(nested_cpi_ctx, amount)?; // [safe_cpi_call]
Ok(())
}
// Case 25: Helper function with individual account parameters - unsafe
pub fn helper_with_individual_accounts(ctx: Context<UncheckedCpi>, amount: u64) -> Result<()> {
// Direct call to helper with individual accounts - unsafe
cpi_call_with_account(
&ctx.accounts.from,
&ctx.accounts.to,
&ctx.accounts.unchecked_program,
amount,
)?;
Ok(())
}
// Case 26: Helper function with accounts struct - unsafe
pub fn helper_with_accounts_struct(ctx: Context<UncheckedCpi>, amount: u64) -> Result<()> {
// Direct call to helper with accounts struct - unsafe
cpi_call_with_accounts(&ctx.accounts, amount)?;
Ok(())
}
// Case 27: Helper function with validation then CPI - safe
pub fn helper_with_validation_then_cpi(
mut ctx: Context<UncheckedCpi>,
amount: u64,
) -> Result<()> {
// Validate first
checked_cpi_call_with_accounts(&mut ctx.accounts)?;
// Then make CPI call - safe because validation happened
cpi_call_with_accounts(&mut ctx.accounts, amount)?; // [safe_cpi_call]
Ok(())
}
// Case 28: Helper function with validation in nested helper - safe
pub fn helper_with_nested_validation(
mut ctx: Context<UncheckedCpi>,
amount: u64,
) -> Result<()> {
// This calls checked_cpi_call_with_accounts which validates, then makes CPI
// Should be safe because validation happens in the helper chain
nested_cpi_in_helper_function(ctx, amount)?; // [safe_cpi_call]
Ok(())
}
// Case 29: Helper function with context accounts pattern - safe
pub fn helper_with_context_accounts_pattern(
mut ctx: Context<UncheckedCpi>,
amount: u64,
) -> Result<()> {
// This validates in helper then makes CPI - safe
nested_cpi_in_helper_with_context_accounts(ctx, amount)?; // [safe_cpi_call]
Ok(())
}
// Case 30: Helper function without validation - unsafe
pub fn helper_without_validation(mut ctx: Context<UncheckedCpi>, amount: u64) -> Result<()> {
// No validation before CPI - unsafe
cpi_call_with_accounts(&mut ctx.accounts, amount)?;
Ok(())
}
// Case 31: Helper function with individual accounts and validation - safe
pub fn helper_with_individual_accounts_validated(
mut ctx: Context<UncheckedCpi>,
amount: u64,
) -> Result<()> {
// Validate first
checked_cpi_call_with_account(&mut ctx.accounts.unchecked_program)?;
// Then make CPI call with individual accounts - safe because validation happened
cpi_call_with_account_safe(
&ctx.accounts.from,
&ctx.accounts.to,
&ctx.accounts.unchecked_program,
amount,
)?;
Ok(())
}
// Case 32: Nested helper function with individual accounts pattern - safe
pub fn nested_helper_with_individual_accounts(
mut ctx: Context<UncheckedCpi>,
amount: u64,
) -> Result<()> {
// This calls a helper that validates then makes CPI with individual accounts
// Should be safe because validation happens in the helper chain
nested_cpi_call_with_individual_accounts(
&ctx.accounts.from,
&ctx.accounts.to,
&mut ctx.accounts.unchecked_program,
amount,
)?;
Ok(())
}
// Case 33: Nested helper function with individual accounts pattern - safe
pub fn nested_helper_with_function_param_account(
mut ctx: Context<UncheckedCpi>,
program: Pubkey,
amount: u64,
) -> Result<()> {
nested_cpi_call_with_function_param_account(
&ctx.accounts.from,
&ctx.accounts.to,
program,
amount,
)?;
Ok(())
}
// Case 34: Helper function with self implementation - safe
pub fn nested_helper_with_self_implementation_safe(
ctx: Context<ImplMethodAccounts>,
amount: u64,
) -> Result<()> {
require_keys_eq!(
ctx.accounts.unchecked_program.key(),
system_program::ID,
CustomError::InvalidProgram
);
ctx.accounts.cpi_call_safe(amount)?;
Ok(())
}
// Case 35: Helper function with self implementation - unsafe
pub fn nested_helper_with_self_implementation_unsafe(
ctx: Context<ImplMethodAccounts>,
amount: u64,
) -> Result<()> {
ctx.accounts.cpi_call_unsafe(amount)?;
Ok(())
}
// Case 36: Other CPI call(mint_to) with unchecked program ID - unsafe
pub fn cpi_mint_to_unchecked_program(
ctx: Context<CpiMintToAccounts>,
amount: u64,
) -> Result<()> {
let cpi_accounts = anchor_spl::token::MintTo {
mint: ctx.accounts.mint.to_account_info(),
to: ctx.accounts.to.to_account_info(),
authority: ctx.accounts.authority.to_account_info(),
};
let cpi_ctx = CpiContext::new(ctx.accounts.unchecked_program.key(), cpi_accounts);
anchor_spl::token::mint_to(cpi_ctx, amount)?; // [arbitrary_cpi_call]
Ok(())
}
// Case 37: Associated Token Program create_ata without validation - unsafe
pub fn cpi_create_ata_unchecked_program(
ctx: Context<CpiCreateAtaAccounts>,
) -> Result<()> {
use anchor_spl::associated_token::{self, Create};
let cpi_accounts = Create {
payer: ctx.accounts.payer.to_account_info(),
associated_token: ctx.accounts.associated_token.to_account_info(),
authority: ctx.accounts.authority.to_account_info(),
mint: ctx.accounts.mint.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
token_program: ctx.accounts.token_program.to_account_info(),
};
let cpi_ctx = CpiContext::new(ctx.accounts.unchecked_program.key(), cpi_accounts);
associated_token::create(cpi_ctx)?; // [arbitrary_cpi_call]
Ok(())
}
// Case 38: Associated Token Program create_ata with validated program ID - safe
pub fn cpi_create_ata_validated_program(
ctx: Context<CpiCreateAtaAccounts>,
) -> Result<()> {
use anchor_spl::associated_token::{self, Create};
require_keys_eq!(
ctx.accounts.unchecked_program.key(),
anchor_spl::associated_token::ID,
CustomError::InvalidProgram
);
let cpi_accounts = Create {
payer: ctx.accounts.payer.to_account_info(),
associated_token: ctx.accounts.associated_token.to_account_info(),
authority: ctx.accounts.authority.to_account_info(),
mint: ctx.accounts.mint.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
token_program: ctx.accounts.token_program.to_account_info(),
};
let cpi_ctx = CpiContext::new(ctx.accounts.unchecked_program.key(), cpi_accounts);
associated_token::create(cpi_ctx)?; // [safe_cpi_call]
Ok(())
}
// Case 39: Invoke CPI with unchecked program ID - unsafe
pub fn cpi_custom_program_unchecked(ctx: Context<DirectInvokeTransfer>) -> Result<()> {
use anchor_lang::solana_program::instruction::Instruction;
use anchor_lang::solana_program::program::invoke;
let instruction = Instruction {
program_id: ctx.accounts.unchecked_program.key(),
accounts: vec![],
data: vec![],
};
let account_infos = vec![ctx.accounts.unchecked_program.to_account_info()];
invoke(&instruction, &account_infos)?; // [arbitrary_cpi_call]
Ok(())
}
// Case 40: Raw invoke with validated program ID - safe
pub fn cpi_custom_program_checked(ctx: Context<DirectInvokeTransfer>) -> Result<()> {
use anchor_lang::solana_program::instruction::Instruction;
use anchor_lang::solana_program::program::invoke;
const VALIDATED_PROGRAM_ID: Pubkey = Pubkey::new_from_array([42u8; 32]); // Consider it as a constant program ID
require_keys_eq!(
ctx.accounts.unchecked_program.key(),
VALIDATED_PROGRAM_ID,
CustomError::InvalidProgram
);
let instruction = Instruction {
program_id: ctx.accounts.unchecked_program.key(),
accounts: vec![],
data: vec![],
};
let account_infos = vec![ctx.accounts.unchecked_program.to_account_info()];
invoke(&instruction, &account_infos)?; // [safe_cpi_call]
Ok(())
}
}
pub fn cpi_call_with_account<'info>(
from: &Signer<'info>,
to: &UncheckedAccount<'info>,
program: &UncheckedAccount<'info>,
amount: u64,
) -> Result<()> {
let cpi_accounts = Transfer {
from: from.to_account_info(),
to: to.to_account_info(),
};
let cpi_ctx = CpiContext::new(program.key(), cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [arbitrary_cpi_call]
Ok(())
}
/// Helper function that takes accounts struct and makes a CPI call
/// This should be flagged as unsafe when called without validation
pub fn cpi_call_with_accounts(ctx_accounts: &UncheckedCpi, amount: u64) -> Result<()> {
cpi_call_with_account(
&ctx_accounts.from,
&ctx_accounts.to,
&ctx_accounts.unchecked_program,
amount,
)?;
Ok(())
}
/// Helper function that validates the program account
/// This is safe and should be used before making CPI calls
pub fn checked_cpi_call_with_accounts(acc_mut: &mut UncheckedCpi) -> Result<()> {
checked_cpi_call_with_account(&mut acc_mut.unchecked_program)?;
Ok(())
}
pub fn checked_cpi_call_with_account(program_account: &UncheckedAccount) -> Result<()> {
require_keys_eq!(
program_account.key(),
system_program::ID,
CustomError::InvalidProgram
);
Ok(())
}
/// Nested helper function that validates then makes CPI call
/// This should be safe because validation happens before CPI
pub fn nested_cpi_in_helper_function(mut ctx: Context<UncheckedCpi>, amount: u64) -> Result<()> {
checked_cpi_call_with_accounts(&mut ctx.accounts)?;
cpi_call_with_accounts(&mut ctx.accounts, amount)?;
Ok(())
}
/// Nested helper function with context accounts pattern
/// Validates in helper then makes CPI - should be safe
pub fn nested_cpi_in_helper_with_context_accounts(
mut ctx: Context<UncheckedCpi>,
amount: u64,
) -> Result<()> {
checked_cpi_call_with_accounts(&mut ctx.accounts)?;
cpi_call_with_accounts(&mut ctx.accounts, amount)?;
Ok(())
}
pub fn cpi_call_with_account_safe<'info>(
from: &Signer<'info>,
to: &UncheckedAccount<'info>,
program: &UncheckedAccount<'info>,
amount: u64,
) -> Result<()> {
let cpi_accounts = Transfer {
from: from.to_account_info(),
to: to.to_account_info(),
};
let cpi_ctx = CpiContext::new(program.key(), cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [safe_cpi_call]
Ok(())
}
pub fn nested_cpi_call_with_individual_accounts<'info>(
from: &Signer<'info>,
to: &UncheckedAccount<'info>,
program: &mut UncheckedAccount<'info>,
amount: u64,
) -> Result<()> {
// Validate first
checked_cpi_call_with_account(program)?;
// Then make CPI call with individual accounts - safe because validation happened
cpi_call_with_account_safe(from, to, program, amount)?;
Ok(())
}
pub fn checked_cpi_call_with_function_param_account(program: Pubkey) -> Result<()> {
require_keys_eq!(program, system_program::ID, CustomError::InvalidProgram);
Ok(())
}
pub fn cpi_call_with_function_param_account<'info>(
from: &Signer<'info>,
to: &UncheckedAccount<'info>,
program: Pubkey,
amount: u64,
) -> Result<()> {
let cpi_accounts = Transfer {
from: from.to_account_info(),
to: to.to_account_info(),
};
let cpi_ctx = CpiContext::new(program, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?; // [safe_cpi_call]
Ok(())
}
pub fn nested_cpi_call_with_function_param_account<'info>(
from: &Signer<'info>,
to: &UncheckedAccount<'info>,
program: Pubkey,
amount: u64,
) -> Result<()> {
checked_cpi_call_with_function_param_account(program)?;
cpi_call_with_function_param_account(from, to, program, amount)?;
Ok(())
}
#[derive(Accounts)]
pub struct UncheckedCpi<'info> {
#[account(mut)]
pub from: Signer<'info>,
#[account(mut)]
/// CHECK: test fixture
pub to: UncheckedAccount<'info>,
/// CHECK: test fixture
pub unchecked_program: UncheckedAccount<'info>,
}
#[derive(Accounts)]
pub struct ValidatedCpi<'info> {
#[account(mut)]
pub from: Signer<'info>,
#[account(mut)]
/// CHECK: test fixture
pub to: UncheckedAccount<'info>,
/// CHECK: test fixture
pub unchecked_program: UncheckedAccount<'info>,
}
#[derive(Accounts)]
pub struct BasicTransfer<'info> {
#[account(mut)]
pub from: Signer<'info>,
#[account(mut)]
/// CHECK: test fixture
pub to: UncheckedAccount<'info>,
}
#[derive(Accounts)]
pub struct AccountBasedTransfer<'info> {
#[account(mut)]
pub from: Signer<'info>,
#[account(mut)]
/// CHECK: test fixture
pub to: UncheckedAccount<'info>,
/// CHECK: test fixture
pub unchecked_program: UncheckedAccount<'info>,
}
#[derive(Accounts)]
pub struct DataBasedTransfer<'info> {
#[account(mut)]
pub from: Signer<'info>,
#[account(mut)]
/// CHECK: test fixture
pub to: UncheckedAccount<'info>,
#[account(mut)]
pub inner: Account<'info, InnerAccount>,
}
#[derive(Accounts)]
pub struct DirectInvokeTransfer<'info> {
#[account(mut)]
pub from: Signer<'info>,
#[account(mut)]
/// CHECK: test fixture
pub to: UncheckedAccount<'info>,
/// CHECK: test fixture
pub unchecked_program: UncheckedAccount<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct CpiBuilderAccounts<'info> {
#[account(mut)]
pub from: Signer<'info>,
#[account(mut)]
pub to: AccountInfo<'info>,
pub authority: Signer<'info>,
pub token_program: Program<'info, anchor_spl::token::Token>, // Validated by Anchor
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct CpiBuilderUnchecked<'info> {
#[account(mut)]
pub from: Signer<'info>,
#[account(mut)]
pub to: AccountInfo<'info>,
/// CHECK: Unchecked program account
pub unchecked_program: UncheckedAccount<'info>,
pub system_program: Program<'info, System>,
}