From 19e00e5d8fa773dd753bea32685fa964cc704855 Mon Sep 17 00:00:00 2001 From: Henry-E Date: Fri, 25 Nov 2022 15:12:58 +0000 Subject: [PATCH] Don't allow the payer to be initialized as a program account (#2284) * don't allow people to initialize the payer as a program account * update changelog * accidentally commented out part of another test Co-authored-by: henrye --- CHANGELOG.md | 1 + lang/src/error.rs | 3 ++ lang/syn/src/codegen/accounts/constraints.rs | 1 + tests/misc/tests/misc/misc.ts | 16 ++++++ tests/yarn.lock | 51 +++++++++++--------- 5 files changed, 49 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c2b3e129e..1d20968908 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ The minor version will be incremented upon a breaking change and the patch versi - lang: Fix IDL `seed` generation for byte string literals. ([#2125](https://github.com/coral-xyz/anchor/pull/2125)) - ts: Update seeds inference to allow nested user defined structs within the seeds ([#2198](https://github.com/coral-xyz/anchor/pull/2198)) - event: Fix multiple event listeners with the same name. ([#2165](https://github.com/coral-xyz/anchor/pull/2165)) +- lang: Prevent the payer account from being initialized as a program account. ([#2284](https://github.com/coral-xyz/anchor/pull/2284)) ### Breaking diff --git a/lang/src/error.rs b/lang/src/error.rs index 30d5a2d2f6..096a23dfe7 100644 --- a/lang/src/error.rs +++ b/lang/src/error.rs @@ -194,6 +194,9 @@ pub enum ErrorCode { /// 4100 - The declared program id does not match actual program id #[msg("The declared program id does not match the actual program id")] DeclaredProgramIdMismatch = 4100, + /// 4101 - You cannot/should not initialize the payer account as a program account + #[msg("You cannot/should not initialize the payer account as a program account")] + TryingToInitPayerAsProgramAccount = 4101, // Deprecated /// 5000 - The API being used is deprecated and should no longer be used diff --git a/lang/syn/src/codegen/accounts/constraints.rs b/lang/syn/src/codegen/accounts/constraints.rs index 0fc1958138..52b4875aa6 100644 --- a/lang/syn/src/codegen/accounts/constraints.rs +++ b/lang/syn/src/codegen/accounts/constraints.rs @@ -835,6 +835,7 @@ pub fn generate_create_account( let cpi_context = anchor_lang::context::CpiContext::new(system_program.to_account_info(), cpi_accounts); anchor_lang::system_program::create_account(cpi_context.with_signer(&[#seeds_with_nonce]), lamports, #space as u64, #owner)?; } else { + require_keys_neq!(payer.key(), #field.key(), anchor_lang::error::ErrorCode::TryingToInitPayerAsProgramAccount); // Fund the account for rent exemption. let required_lamports = __anchor_rent .minimum_balance(#space) diff --git a/tests/misc/tests/misc/misc.ts b/tests/misc/tests/misc/misc.ts index 6aa4c7a140..977b68d4a9 100644 --- a/tests/misc/tests/misc/misc.ts +++ b/tests/misc/tests/misc/misc.ts @@ -580,6 +580,22 @@ describe("misc", () => { assert.strictEqual(account.data, 3); }); + it("Should fail when trying to init the payer as a program account", async () => { + try { + await program.rpc.testInit({ + accounts: { + data: provider.wallet.publicKey, + payer: provider.wallet.publicKey, + systemProgram: anchor.web3.SystemProgram.programId, + }, + }); + assert.fail("Transaction should fail"); + } catch (e) { + // "Error Code: TryingToInitPayerAsProgramAccount. Error Number: 4101. Error Message: You cannot/should not initialize the payer account as a program account." + assert.strictEqual(e.error.errorCode.number, 4101); + } + }); + it("Can init a random zero copy account", async () => { const data = anchor.web3.Keypair.generate(); await program.rpc.testInitZeroCopy({ diff --git a/tests/yarn.lock b/tests/yarn.lock index 9333217a14..3059337114 100644 --- a/tests/yarn.lock +++ b/tests/yarn.lock @@ -69,7 +69,7 @@ bn.js "^5.1.2" bs58 "^4.0.1" buffer-layout "^1.2.2" - camelcase "^5.3.1" + camelcase "^6.3.0" cross-fetch "^3.1.5" crypto-hash "^1.3.0" eventemitter3 "^4.0.7" @@ -99,7 +99,7 @@ snake-case "^3.0.4" toml "^3.0.0" -"@project-serum/borsh@^0.2.5": +"@project-serum/borsh@^0.2.2", "@project-serum/borsh@^0.2.5": version "0.2.5" resolved "https://registry.yarnpkg.com/@project-serum/borsh/-/borsh-0.2.5.tgz#6059287aa624ecebbfc0edd35e4c28ff987d8663" integrity sha512-UmeUkUoKdQ7rhx6Leve1SssMR/Ghv8qrEiyywyxSWg7ooV7StdpPBhciiy5eB3T0qU1BXvdRNC8TdrkxK7WC5Q== @@ -175,27 +175,7 @@ buffer-layout "^1.2.0" dotenv "10.0.0" -"@solana/web3.js@^1.21.0": - version "1.30.2" - resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.30.2.tgz#e85da75e0825dc64f53eb64a1ff0115b27bec135" - integrity sha512-hznCj+rkfvM5taRP3Z+l5lumB7IQnDrB4l55Wpsg4kDU9Zds8pE5YOH5Z9bbF/pUzZJKQjyBjnY/6kScBm3Ugg== - dependencies: - "@babel/runtime" "^7.12.5" - "@ethersproject/sha2" "^5.5.0" - "@solana/buffer-layout" "^3.0.0" - bn.js "^5.0.0" - borsh "^0.4.0" - bs58 "^4.0.1" - buffer "6.0.1" - cross-fetch "^3.1.4" - jayson "^3.4.4" - js-sha3 "^0.8.0" - rpc-websockets "^7.4.2" - secp256k1 "^4.0.2" - superstruct "^0.14.2" - tweetnacl "^1.0.0" - -"@solana/web3.js@^1.32.0": +"@solana/web3.js@^1.17.0", "@solana/web3.js@^1.32.0": version "1.66.2" resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.66.2.tgz#80b43c5868b846124fe3ebac7d3943930c3fa60c" integrity sha512-RyaHMR2jGmaesnYP045VLeBGfR/gAW3cvZHzMFGg7bkO+WOYOYp1nEllf0/la4U4qsYGKCsO9eEevR5fhHiVHg== @@ -216,6 +196,26 @@ rpc-websockets "^7.5.0" superstruct "^0.14.2" +"@solana/web3.js@^1.21.0": + version "1.30.2" + resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.30.2.tgz#e85da75e0825dc64f53eb64a1ff0115b27bec135" + integrity sha512-hznCj+rkfvM5taRP3Z+l5lumB7IQnDrB4l55Wpsg4kDU9Zds8pE5YOH5Z9bbF/pUzZJKQjyBjnY/6kScBm3Ugg== + dependencies: + "@babel/runtime" "^7.12.5" + "@ethersproject/sha2" "^5.5.0" + "@solana/buffer-layout" "^3.0.0" + bn.js "^5.0.0" + borsh "^0.4.0" + bs58 "^4.0.1" + buffer "6.0.1" + cross-fetch "^3.1.4" + jayson "^3.4.4" + js-sha3 "^0.8.0" + rpc-websockets "^7.4.2" + secp256k1 "^4.0.2" + superstruct "^0.14.2" + tweetnacl "^1.0.0" + "@solana/web3.js@^1.64.0": version "1.64.0" resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.64.0.tgz#b7f5a976976039a0161242e94d6e1224ab5d30f9" @@ -522,6 +522,11 @@ camelcase@^6.0.0: resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.2.0.tgz#924af881c9d525ac9d87f40d964e5cea982a1809" integrity sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg== +camelcase@^6.3.0: + version "6.3.0" + resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.3.0.tgz#5685b95eb209ac9c0c177467778c9c84df58ba9a" + integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA== + chai@^4.3.4: version "4.3.4" resolved "https://registry.yarnpkg.com/chai/-/chai-4.3.4.tgz#b55e655b31e1eac7099be4c08c21964fce2e6c49"