Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comments to identify insecure, recommended, and secure patterns in 0-signer-authorization #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion programs/0-signer-authorization/insecure/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use anchor_lang::prelude::*;

// Insecure: `authority` is of type `AccountInfo` without any checks to ensure it's a signer.
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod signer_authorization_insecure {
use super::*;
Expand All @@ -13,5 +15,5 @@ pub mod signer_authorization_insecure {

#[derive(Accounts)]
pub struct LogMessage<'info> {
authority: AccountInfo<'info>,
authority: AccountInfo<'info>, // No check for signer authority.
}
3 changes: 2 additions & 1 deletion programs/0-signer-authorization/recommended/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anchor_lang::prelude::*;

// Recommended: Changing `authority` to type `Signer` ensures the account has signer privileges.
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
Expand All @@ -14,5 +15,5 @@ pub mod signer_authorization_recommended {

#[derive(Accounts)]
pub struct LogMessage<'info> {
authority: Signer<'info>,
authority: Signer<'info>, // Ensures that `authority` is a valid signer.
}
5 changes: 3 additions & 2 deletions programs/0-signer-authorization/secure/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anchor_lang::prelude::*;

// Secure: Explicitly checks if `authority` is a signer before proceeding.
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
Expand All @@ -8,7 +9,7 @@ pub mod signer_authorization_secure {

pub fn log_message(ctx: Context<LogMessage>) -> ProgramResult {
if !ctx.accounts.authority.is_signer {
return Err(ProgramError::MissingRequiredSignature);
return Err(ProgramError::MissingRequiredSignature); // Secure: Throws an error if `authority` is not a signer.
}
msg!("GM {}", ctx.accounts.authority.key().to_string());
Ok(())
Expand All @@ -17,5 +18,5 @@ pub mod signer_authorization_secure {

#[derive(Accounts)]
pub struct LogMessage<'info> {
authority: AccountInfo<'info>,
authority: AccountInfo<'info>, // Type `AccountInfo` allows additional checks.
}