Loading...
Loading...
Compare original and translation side by side
use anchor_lang::prelude::*Cargo.tomluse anchor_lang::prelude::*Cargo.tomlchecked_*checked_*undefinedundefinedprograms/vault/src/lib.rs:45-52// Show the problematic code// Show the secure alternative
**Report Summary:**
- Total findings by severity
- Critical issues first (prioritize by risk)
- Quick wins (easy fixes with high impact)
- Recommendations for testing improvementsprograms/vault/src/lib.rs:45-52// 展示有问题的代码// 展示安全的替代代码
**报告摘要:**
- 各严重等级的问题总数
- 优先展示严重问题(按风险排序)
- 快速修复项(易实施且影响大的修复)
- 测试改进建议// ✅ Account validation with constraints
#[derive(Accounts)]
pub struct SecureInstruction<'info> {
#[account(
mut,
has_one = authority, // Relationship check
seeds = [b"vault", user.key().as_ref()],
bump, // Canonical bump
)]
pub vault: Account<'info, Vault>,
pub authority: Signer<'info>, // Signer required
pub token_program: Program<'info, Token>, // Program validation
}
// ✅ Checked arithmetic
let total = balance.checked_add(amount)
.ok_or(ErrorCode::Overflow)?;// ✅ Manual account validation
if !authority.is_signer {
return Err(ProgramError::MissingRequiredSignature);
}
if vault.owner != program_id {
return Err(ProgramError::IllegalOwner);
}
// ✅ Checked arithmetic
let total = balance.checked_add(amount)
.ok_or(ProgramError::ArithmeticOverflow)?;// ✅ 带约束的账户验证
#[derive(Accounts)]
pub struct SecureInstruction<'info> {
#[account(
mut,
has_one = authority, // 关系检查
seeds = [b"vault", user.key().as_ref()],
bump, // 规范bump值
)]
pub vault: Account<'info, Vault>,
pub authority: Signer<'info>, // 需要签名者
pub token_program: Program<'info, Token>, // 程序验证
}
// ✅ 安全算术运算
let total = balance.checked_add(amount)
.ok_or(ErrorCode::Overflow)?;// ✅ 手动账户验证
if !authority.is_signer {
return Err(ProgramError::MissingRequiredSignature);
}
if vault.owner != program_id {
return Err(ProgramError::IllegalOwner);
}
// ✅ 安全算术运算
let total = balance.checked_add(amount)
.ok_or(ProgramError::ArithmeticOverflow)?;saturating_*unwrap()expect()init_if_neededchecked_*checked_addchecked_subok_or(error)?initSigner<'info>is_signerProgram<'info, T>saturating_*unwrap()expect()init_if_neededchecked_*checked_addchecked_subok_or(error)?initSigner<'info>is_signerProgram<'info, T>CpiContextCpiContextInitSpaceInitSpace