Skip to content
This repository was archived by the owner on Jun 1, 2026. It is now read-only.

Commit b7611e9

Browse files
committed
fix(solana): reject transaction payloads in sign message
1 parent d35225c commit b7611e9

4 files changed

Lines changed: 95 additions & 16 deletions

File tree

‎crates/gem_solana/src/signer/chain_signer.rs‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use super::{instructions, swap, transaction};
2-
use crate::decode_transaction;
2+
use crate::{decode_transaction, transaction::is_transaction_bytes};
33
use gem_encoding::encode_base64;
44
use primitives::{ChainSigner, SignerError, SignerInput, TransferDataOutputType};
55
use solana_primitives::{Pubkey, sign_message as sign_solana_message};
66

77
#[derive(Default)]
88
pub struct SolanaChainSigner;
99

10+
const SIGN_MESSAGE_PAYLOAD_REJECTION: &str = "Serialized Solana transaction or transaction message received in signMessage request; use signTransaction instead";
11+
1012
impl ChainSigner for SolanaChainSigner {
1113
fn sign_transfer(&self, input: &SignerInput, private_key: &[u8]) -> Result<String, SignerError> {
1214
let sender = Pubkey::from_base58(&input.sender_address).map_err(SignerError::from_display)?;
@@ -38,6 +40,9 @@ impl ChainSigner for SolanaChainSigner {
3840
}
3941

4042
fn sign_message(&self, message: &[u8], private_key: &[u8]) -> Result<String, SignerError> {
43+
if is_transaction_bytes(message) {
44+
return Err(SignerError::invalid_input(SIGN_MESSAGE_PAYLOAD_REJECTION));
45+
}
4146
let signature = sign_solana_message(private_key, message).map_err(|e| SignerError::signing_error(format!("sign: {e}")))?;
4247
Ok(bs58::encode(signature.as_bytes()).into_string())
4348
}
@@ -69,7 +74,7 @@ impl ChainSigner for SolanaChainSigner {
6974
#[cfg(test)]
7075
mod tests {
7176
use super::*;
72-
use crate::signer::testkit::{DOUBLE_SIG_TX, EXPECTED_MESSAGE_HEX, SINGLE_SIG_TX};
77+
use crate::signer::testkit::{DOUBLE_SIG_TX, EXPECTED_MESSAGE_HEX, SINGLE_SIG_TX, mock_legacy_transaction};
7378
use gem_encoding::decode_base64;
7479
use primitives::testkit::signer_mock::TEST_PRIVATE_KEY;
7580
use primitives::{Chain, ChainSigner, SignerInput, TransactionLoadInput, TransferDataOutputType};
@@ -125,10 +130,27 @@ mod tests {
125130

126131
#[test]
127132
fn test_sign_message() {
128-
let signer = SolanaChainSigner;
129-
130-
let result = signer.sign_message(b"hello", &TEST_PRIVATE_KEY).unwrap();
133+
let result = SolanaChainSigner.sign_message(b"hello", &TEST_PRIVATE_KEY).unwrap();
131134

132135
assert_eq!(bs58::decode(result).into_vec().unwrap().len(), 64);
133136
}
137+
138+
#[test]
139+
fn test_sign_message_rejects_transaction_payloads() {
140+
let bytes = decode_base64(SINGLE_SIG_TX).unwrap();
141+
let result = SolanaChainSigner.sign_message(&bytes, &TEST_PRIVATE_KEY);
142+
143+
assert_eq!(result.unwrap_err().to_string(), format!("Invalid input: {SIGN_MESSAGE_PAYLOAD_REJECTION}"));
144+
145+
let transaction = VersionedTransaction::deserialize_with_version(&bytes).unwrap();
146+
let message = transaction.serialize_message().unwrap();
147+
let result = SolanaChainSigner.sign_message(&message, &TEST_PRIVATE_KEY);
148+
149+
assert_eq!(result.unwrap_err().to_string(), format!("Invalid input: {SIGN_MESSAGE_PAYLOAD_REJECTION}"));
150+
151+
let message = mock_legacy_transaction().serialize_message().unwrap();
152+
let result = SolanaChainSigner.sign_message(&message, &TEST_PRIVATE_KEY);
153+
154+
assert_eq!(result.unwrap_err().to_string(), format!("Invalid input: {SIGN_MESSAGE_PAYLOAD_REJECTION}"));
155+
}
134156
}

‎crates/gem_solana/src/signer/testkit.rs‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use gem_encoding::decode_base64;
22
use primitives::testkit::signer_mock::TEST_PRIVATE_KEY;
33
use primitives::{SolanaTokenProgramId, TransactionLoadMetadata};
4-
use solana_primitives::{Pubkey, VersionedTransaction, get_address};
4+
use solana_primitives::{CompiledInstruction, LegacyMessage, MessageHeader, Pubkey, VersionedTransaction, get_address};
55

66
pub const TEST_RECIPIENT: &str = "EN2sCsJ1WDV8UFqsiTXHcUPUxQ4juE71eCknHYYMifkd";
77
pub const TEST_SENDER_TOKEN_ADDRESS: &str = "HEeranxp3y7kVQKVSLdZW1rUmnbs7bAtUTMu8o88Jash";
@@ -26,6 +26,26 @@ pub fn base58_transaction(encoded_base64: &str) -> String {
2626
bs58::encode(decode_base64(encoded_base64).unwrap()).into_string()
2727
}
2828

29+
pub fn mock_legacy_transaction() -> VersionedTransaction {
30+
VersionedTransaction::Legacy {
31+
signatures: vec![],
32+
message: LegacyMessage {
33+
header: MessageHeader {
34+
num_required_signatures: 1,
35+
num_readonly_signed_accounts: 0,
36+
num_readonly_unsigned_accounts: 1,
37+
},
38+
account_keys: vec![Pubkey::new([1; 32]), Pubkey::new([2; 32])],
39+
recent_blockhash: [3; 32],
40+
instructions: vec![CompiledInstruction {
41+
program_id_index: 1,
42+
accounts: vec![0],
43+
data: vec![],
44+
}],
45+
},
46+
}
47+
}
48+
2949
pub fn program_id(transaction: &VersionedTransaction, index: usize) -> String {
3050
let instruction = &transaction.instructions()[index];
3151
transaction.account_keys()[instruction.program_id_index as usize].to_base58()

‎crates/gem_solana/src/transaction.rs‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,26 @@ use solana_primitives::{AccountMeta, AddressLookupTableAccount, Instruction, Pub
44

55
pub fn try_decode_transaction(transaction_base64: &str) -> Option<VersionedTransaction> {
66
let data = decode_base64(transaction_base64).ok()?;
7-
VersionedTransaction::deserialize_with_version(&data).ok()
7+
try_decode_transaction_bytes(&data)
8+
}
9+
10+
pub(crate) fn try_decode_transaction_bytes(transaction: &[u8]) -> Option<VersionedTransaction> {
11+
let decoded = VersionedTransaction::deserialize_with_version(transaction).ok()?;
12+
// Ensure the parser consumed exactly this transaction, with no trailing bytes.
13+
(decoded.serialize().ok()? == transaction).then_some(decoded)
14+
}
15+
16+
pub(crate) fn is_transaction_bytes(transaction: &[u8]) -> bool {
17+
try_decode_transaction_bytes(transaction).is_some() || try_decode_transaction_message(transaction).is_some()
18+
}
19+
20+
fn try_decode_transaction_message(message: &[u8]) -> Option<VersionedTransaction> {
21+
let mut transaction = Vec::with_capacity(message.len() + 1);
22+
transaction.push(0);
23+
transaction.extend_from_slice(message);
24+
25+
let decoded = VersionedTransaction::deserialize_with_version(&transaction).ok()?;
26+
(decoded.serialize_message().ok()? == message).then_some(decoded)
827
}
928

1029
pub fn decode_transaction(transaction_base64: &str) -> Result<VersionedTransaction, String> {
@@ -49,11 +68,32 @@ pub fn instructions_from_primitives(instructions: Vec<SolanaInstruction>) -> Res
4968
#[cfg(test)]
5069
mod tests {
5170
use super::*;
71+
#[cfg(feature = "signer")]
72+
use crate::signer::testkit::{SINGLE_SIG_TX, mock_legacy_transaction};
5273

5374
#[test]
5475
fn test_try_decode_blockhash() {
5576
assert!(try_decode_blockhash("BZcyEKqjBNG5bEY6i5ev6PfPTgDSB9LwovJE1hJfJoHF").is_some());
5677
assert!(try_decode_blockhash("invalid blockhash").is_none());
5778
assert!(try_decode_blockhash("1111111111111111111111111111111").is_none());
5879
}
80+
81+
#[cfg(feature = "signer")]
82+
#[test]
83+
fn test_is_transaction_bytes() {
84+
let full_transaction = gem_encoding::decode_base64(SINGLE_SIG_TX).unwrap();
85+
let transaction = VersionedTransaction::deserialize_with_version(&full_transaction).unwrap();
86+
let mut v0_message = transaction.serialize_message().unwrap();
87+
let mut transaction_with_trailing_byte = full_transaction.clone();
88+
89+
assert!(is_transaction_bytes(&full_transaction));
90+
assert!(is_transaction_bytes(&v0_message));
91+
assert!(is_transaction_bytes(&mock_legacy_transaction().serialize_message().unwrap()));
92+
93+
transaction_with_trailing_byte.push(0);
94+
v0_message.push(0);
95+
assert!(!is_transaction_bytes(&transaction_with_trailing_byte));
96+
assert!(!is_transaction_bytes(&v0_message));
97+
assert!(!is_transaction_bytes(b"hello"));
98+
}
5999
}

‎gemstone/src/message/signer.rs‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ use base64::Engine;
44
use base64::engine::general_purpose::STANDARD as BASE64;
55
use bs58;
66
use gem_evm::message::eip191_hash_message;
7+
use gem_solana::signer::SolanaChainSigner;
78
use gem_sui::signer as sui_signer;
89
use gem_ton::address::base64_to_hex_address;
910
use gem_ton::signer::{TonSignDataResponse, TonSignMessageData, TonSignResult, TonSigner};
1011
use primitives::hex::encode_with_0x;
11-
use signer::{SIGNATURE_LENGTH, SignatureScheme, Signer, ensure_ethereum_signature_recovery_id_offset, hash_eip712};
12+
use signer::{SIGNATURE_LENGTH, Signer, ensure_ethereum_signature_recovery_id_offset, hash_eip712};
1213
use std::time::{SystemTime, UNIX_EPOCH};
1314
use sui_types::PersonalMessage;
1415

@@ -19,7 +20,7 @@ use super::{
1920
};
2021
use crate::{GemstoneError, siwe::SiweMessage};
2122
use gem_tron::signer::tron_hash_message;
22-
use primitives::SimulationPayloadField;
23+
use primitives::{ChainSigner, SimulationPayloadField};
2324
use zeroize::Zeroizing;
2425

2526
fn siwe_or_text_preview(chain: primitives::Chain, data: &[u8]) -> MessagePreview {
@@ -133,10 +134,7 @@ impl MessageSigner {
133134
let digest = hash_eip712(&json)?;
134135
Ok(digest.to_vec())
135136
}
136-
SignDigestType::Base58 => {
137-
let decoded = bs58::decode(&self.message.data).into_vec().map_err(|e| GemstoneError::from(e.to_string()))?;
138-
Ok(decoded)
139-
}
137+
SignDigestType::Base58 => bs58::decode(&self.message.data).into_vec().map_err(|e| GemstoneError::from(e.to_string())),
140138
}
141139
}
142140

@@ -172,9 +170,8 @@ impl MessageSigner {
172170
Ok(encode_with_0x(&signature))
173171
}
174172
SignDigestType::Base58 => {
175-
let hash = self.hash()?;
176-
let signed = Signer::sign_digest(SignatureScheme::Ed25519, hash, private_key.to_vec())?;
177-
Ok(self.get_result(&signed))
173+
let message = self.hash()?;
174+
SolanaChainSigner.sign_message(&message, private_key.as_slice()).map_err(GemstoneError::from)
178175
}
179176
}
180177
}

0 commit comments

Comments
 (0)