Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

feat: clean undisplayable glyphs #47

Merged
merged 6 commits into from
Mar 21, 2024
Merged
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
26 changes: 25 additions & 1 deletion fmt_buffer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@ impl<const N: usize> Buffer<N> {
}
}

pub fn as_str(&self) -> &str {
pub fn as_str(&mut self) -> &str {
for byte in self.buffer[..self.used].iter_mut() {
// NOTE: this workaround is needed until https://github.com/LedgerHQ/ledger-device-rust-sdk/issues/146
// is handled at sdk level
if *byte < 0x20 {
*byte = 0x7f;
}
// NOTE: this workaround is needed until https://github.com/LedgerHQ/ledger-device-rust-sdk/issues/124
// is handled at sdk level
if *byte > 0x7f {
// NOTE: this is a square glyph, of DEL display
*byte = 0x7f;
}
}
debug_assert!(self.used <= self.buffer.len());
// .unwrap() is ok, as only bytes, comprising a sequence of valid utf8 chars
// are going to be written to `self.buffer` on `self.write_str` calls
Expand Down Expand Up @@ -135,4 +148,15 @@ mod tests {
assert_eq!("toooooo long: 400000 - 0x61", buffer.as_str());
assert_eq!(true, buffer.truncated());
}

#[test]
pub fn test_rewrite_undisplayable_chars() {
let mut buffer = Buffer::<30>::new();

buffer.write_str("Prefix: ");

buffer.write_str("\x0A\x0D");
assert_eq!("Prefix: \x7F\x7F", buffer.as_str());
assert_eq!(false, buffer.truncated());
}
}
4 changes: 2 additions & 2 deletions src/app_ui/sign/common/action/delete_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl FieldsContext {
}
}
pub fn format<'b, 'a: 'b>(
delete_account: &'a parsing::types::DeleteAccount,
delete_account: &'a mut parsing::types::DeleteAccount,
field_context: &'a mut FieldsContext,
writer: &'_ mut FieldsWriter<'b, 3>,
) {
Expand All @@ -25,7 +25,7 @@ pub fn format<'b, 'a: 'b>(
}));

let beneficiary_id = ElipsisFields::from_capped_string(
&delete_account.beneficiary_id,
&mut delete_account.beneficiary_id,
"Beneficiary",
&mut field_context.beneficiary_display_buf,
);
Expand Down
4 changes: 2 additions & 2 deletions src/app_ui/sign/common/action/function_call_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl FieldsContext {
}
}
pub fn format<'b, 'a: 'b, const N: usize>(
func_call_common: &'a parsing::types::FunctionCallCommon,
func_call_common: &'a mut parsing::types::FunctionCallCommon,
field_context: &'a mut FieldsContext,
writer: &'_ mut FieldsWriter<'b, N>,
) {
Expand All @@ -33,7 +33,7 @@ pub fn format<'b, 'a: 'b, const N: usize>(
}));

let method_name = ElipsisFields::from_capped_string(
&func_call_common.method_name,
&mut func_call_common.method_name,
"Method Name",
&mut field_context.method_name_display_buf,
);
Expand Down
6 changes: 3 additions & 3 deletions src/app_ui/sign/common/action/function_call_permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl FieldsContext {
}

pub fn format<'b, 'a: 'b>(
function_call_perm: &'a parsing::types::FunctionCallPermission,
function_call_perm: &'a mut parsing::types::FunctionCallPermission,
field_context: &'a mut FieldsContext,
writer: &'_ mut FieldsWriter<'b, 10>,
) {
Expand All @@ -42,7 +42,7 @@ pub fn format<'b, 'a: 'b>(
}));

let recevier_id = ElipsisFields::from_capped_string(
&function_call_perm.receiver_id,
&mut function_call_perm.receiver_id,
"FnCall Receiver",
&mut field_context.receiver_display_buf,
);
Expand All @@ -58,7 +58,7 @@ pub fn format<'b, 'a: 'b>(
}));

let methods_names_fields = ElipsisFields::from_fmt_buffer(
&function_call_perm.method_names,
&mut function_call_perm.method_names,
"Method Names",
&mut field_context.method_names_display_buf,
);
Expand Down
2 changes: 1 addition & 1 deletion src/app_ui/sign/common/action/function_call_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl FieldsContext {
}
}
pub fn format<'b, 'a: 'b>(
args: &'b CappedString<200>,
args: &'b mut CappedString<200>,
field_context: &'a mut FieldsContext,
writer: &'_ mut FieldsWriter<'b, 7>,
) {
Expand Down
10 changes: 5 additions & 5 deletions src/app_ui/sign/common/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn ui_display_create_account(
}

pub fn ui_display_delete_account(
delete_account: &parsing::types::DeleteAccount,
delete_account: &mut parsing::types::DeleteAccount,
params: ActionParams,
) -> bool {
let mut writer: FieldsWriter<'_, 3> = FieldsWriter::new();
Expand Down Expand Up @@ -91,7 +91,7 @@ pub fn ui_display_add_key_fullaccess(

pub fn ui_display_add_key_functioncall(
add_key: &parsing::types::AddKey,
function_call_per: &parsing::types::FunctionCallPermission,
function_call_per: &mut parsing::types::FunctionCallPermission,
params: ActionParams,
) -> bool {
let mut common_field_context: add_key_common::FieldsContext =
Expand Down Expand Up @@ -123,8 +123,8 @@ pub fn ui_display_deploy_contract(
}

pub fn ui_display_function_call_str(
func_call_common: &parsing::types::FunctionCallCommon,
args: &CappedString<200>,
func_call_common: &mut parsing::types::FunctionCallCommon,
args: &mut CappedString<200>,
params: ActionParams,
) -> bool {
let mut writer: FieldsWriter<'_, 7> = FieldsWriter::new();
Expand All @@ -140,7 +140,7 @@ pub fn ui_display_function_call_str(
}

pub fn ui_display_function_call_bin(
func_call_common: &parsing::types::FunctionCallCommon,
func_call_common: &mut parsing::types::FunctionCallCommon,
args: &HexDisplay<200>,
params: ActionParams,
) -> bool {
Expand Down
8 changes: 4 additions & 4 deletions src/app_ui/sign/nep366_delegate_action/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ impl FieldsContext {
}

fn format<'b, 'a: 'b>(
prefix: &'b parsing::types::nep366_delegate_action::prefix::Prefix,
prefix: &'b mut parsing::types::nep366_delegate_action::prefix::Prefix,
field_context: &'a mut FieldsContext,
writer: &'_ mut FieldsWriter<'b, 5>,
) {
let sender_id = ElipsisFields::from_capped_string(
&prefix.sender_id,
&mut prefix.sender_id,
"Sender Id",
&mut field_context.display_buf1,
);
writer.push_fields(sender_id);

let receiver_id = ElipsisFields::from_capped_string(
&prefix.receiver_id,
&mut prefix.receiver_id,
"Receiver Id",
&mut field_context.display_buf2,
);
Expand All @@ -52,7 +52,7 @@ fn format<'b, 'a: 'b>(
value: num_actions_str,
}));
}
pub fn ui_display(prefix: &parsing::types::nep366_delegate_action::prefix::Prefix) -> bool {
pub fn ui_display(prefix: &mut parsing::types::nep366_delegate_action::prefix::Prefix) -> bool {
let mut field_writer: FieldsWriter<'_, 5> = FieldsWriter::new();
let mut field_context: FieldsContext = FieldsContext::new();
format(prefix, &mut field_context, &mut field_writer);
Expand Down
10 changes: 5 additions & 5 deletions src/app_ui/sign/nep413/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ impl FieldsContext {
}

fn format<'b, 'a: 'b>(
payload: &'b Payload,
payload: &'b mut Payload,
field_context: &'a mut FieldsContext,
writer: &'_ mut FieldsWriter<'b, 7>,
) {
// 2
let message_fields = ElipsisFields::from_capped_string(
&payload.message,
&mut payload.message,
"Message",
&mut field_context.msg_display_buf,
);
Expand All @@ -50,14 +50,14 @@ fn format<'b, 'a: 'b>(

// 5
let recipient_fields = ElipsisFields::from_capped_string(
&payload.recipient,
&mut payload.recipient,
"Recipient",
&mut field_context.recipient_display_buf,
);
writer.push_fields(recipient_fields);

// 7
if let Some(callback_url) = payload.callback_url.as_ref() {
if let Some(callback_url) = payload.callback_url.as_mut() {
let callback_url_fields = ElipsisFields::from_capped_string(
callback_url,
"Callback Url",
Expand All @@ -66,7 +66,7 @@ fn format<'b, 'a: 'b>(
writer.push_fields(callback_url_fields);
}
}
pub fn ui_display(payload: &Payload) -> bool {
pub fn ui_display(payload: &mut Payload) -> bool {
let mut field_writer: FieldsWriter<'_, 7> = FieldsWriter::new();
let mut field_context: FieldsContext = FieldsContext::new();
format(payload, &mut field_context, &mut field_writer);
Expand Down
8 changes: 4 additions & 4 deletions src/app_ui/sign/transaction/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ impl FieldsContext {
}

fn format<'b, 'a: 'b>(
prefix: &'b parsing::types::transaction::prefix::Prefix,
prefix: &'b mut parsing::types::transaction::prefix::Prefix,
field_context: &'a mut FieldsContext,
writer: &'_ mut FieldsWriter<'b, 5>,
) {
let signer_id = ElipsisFields::from_capped_string(
&prefix.signer_id,
&mut prefix.signer_id,
"Signer Id",
&mut field_context.display_buf1,
);
writer.push_fields(signer_id);

let receiver_id = ElipsisFields::from_capped_string(
&prefix.receiver_id,
&mut prefix.receiver_id,
"Receiver Id",
&mut field_context.display_buf2,
);
Expand All @@ -52,7 +52,7 @@ fn format<'b, 'a: 'b>(
value: num_actions_str,
}));
}
pub fn ui_display(prefix: &parsing::types::transaction::prefix::Prefix) -> bool {
pub fn ui_display(prefix: &mut parsing::types::transaction::prefix::Prefix) -> bool {
let mut field_writer: FieldsWriter<'_, 5> = FieldsWriter::new();
let mut field_context: FieldsContext = FieldsContext::new();
format(prefix, &mut field_context, &mut field_writer);
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/common/action/add_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn handle_function_call(
.map_err(|_err| AppSW::TxParsingFail)?;
if !sign_ui::action::ui_display_add_key_functioncall(
add_key_common,
&function_call_perm,
&mut function_call_perm,
params,
) {
return Err(AppSW::Deny);
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/common/action/delete_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn handle(
.deserialize_reader_in_place(stream)
.map_err(|_err| AppSW::TxParsingFail)?;

if !sign_ui::action::ui_display_delete_account(&delete_account, params) {
if !sign_ui::action::ui_display_delete_account(&mut delete_account, params) {
return Err(AppSW::Deny);
}
Ok(())
Expand Down
23 changes: 16 additions & 7 deletions src/handlers/common/action/function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn handle(
let args_bytes_count: u32 =
u32::deserialize_reader(stream).map_err(|_err| AppSW::TxParsingFail)?;

let representation = match stream
let mut representation = match stream
.reader
.peek_u8()
.map_err(|_err| AppSW::TxParsingFail)?
Expand Down Expand Up @@ -57,24 +57,33 @@ pub fn handle(
return Err(AppSW::TxParsingFail);
}
};
handle_common(stream, method_name, params, &representation)
handle_common(stream, method_name, params, &mut representation)
}
fn handle_common(
stream: &mut HashingStream<SingleTxStream<'_>>,
method_name: CappedString<50>,
params: ActionParams,
representation: &ArgsRepr,
representation: &mut ArgsRepr,
) -> Result<(), AppSW> {
let func_call_common = FunctionCallCommon::deserialize_with_method_name(stream, method_name)
.map_err(|_err| AppSW::TxParsingFail)?;
let mut func_call_common =
FunctionCallCommon::deserialize_with_method_name(stream, method_name)
.map_err(|_err| AppSW::TxParsingFail)?;
match representation {
ArgsRepr::BinHex(args_bin) => {
if !sign_ui::action::ui_display_function_call_bin(&func_call_common, args_bin, params) {
if !sign_ui::action::ui_display_function_call_bin(
&mut func_call_common,
args_bin,
params,
) {
return Err(AppSW::Deny);
}
}
ArgsRepr::String(args_str) => {
if !sign_ui::action::ui_display_function_call_str(&func_call_common, args_str, params) {
if !sign_ui::action::ui_display_function_call_str(
&mut func_call_common,
args_str,
params,
) {
return Err(AppSW::Deny);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/sign_nep366_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn handle_prefix(stream: &mut HashingStream<SingleTxStream<'_>>) -> Result<u32,
.deserialize_reader_in_place(stream)
.map_err(|_err| AppSW::TxParsingFail)?;

if !sign_ui::nep366_delegate_action::prefix::ui_display(&delegate_action_prefix) {
if !sign_ui::nep366_delegate_action::prefix::ui_display(&mut delegate_action_prefix) {
return Err(AppSW::Deny);
}
Ok(delegate_action_prefix.number_of_actions)
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/sign_nep413_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn handler(mut stream: SingleTxStream<'_>) -> Result<Signature, AppSW> {
.deserialize_reader_in_place(&mut stream)
.map_err(|_err| AppSW::TxParsingFail)?;

if !sign_ui::nep413::payload::ui_display(&payload) {
if !sign_ui::nep413::payload::ui_display(&mut payload) {
return Err(AppSW::Deny);
}

Expand Down
2 changes: 1 addition & 1 deletion src/handlers/sign_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn handle_transaction_prefix(
.deserialize_reader_in_place(stream)
.map_err(|_err| AppSW::TxParsingFail)?;

if !sign_ui::transaction::prefix::ui_display(&tx_prefix) {
if !sign_ui::transaction::prefix::ui_display(&mut tx_prefix) {
return Err(AppSW::Deny);
}
let tx_public_key = PublicKeyBe::try_from(tx_prefix.public_key);
Expand Down
16 changes: 15 additions & 1 deletion src/utils/types/capped_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,21 @@ impl<const N: usize> CappedString<N> {
}
}

pub fn as_str(&self) -> &str {
pub fn as_str(&mut self) -> &str {
for byte in self.buffer[..self.used].iter_mut() {
// NOTE: this workaround is needed until https://github.com/LedgerHQ/ledger-device-rust-sdk/issues/146
// is handled at sdk level
if *byte < 0x20 {
// NOTE: this is a square glyph, of DEL display
*byte = 0x7f;
}
// NOTE: this workaround is needed until https://github.com/LedgerHQ/ledger-device-rust-sdk/issues/124
// is handled at sdk level
if *byte > 0x7f {
// NOTE: this is a square glyph, of DEL display
*byte = 0x7f;
}
}
// .unwrap() is ok because it's either based on complete deserialized `str`
// based on previous validation by `core::str::from_utf8`,
// or `self.used` index is equal to value of [`Utf8Error::valid_up_to()`](https://doc.rust-lang.org/std/str/struct.Utf8Error.html#method.valid_up_to)
Expand Down
4 changes: 2 additions & 2 deletions src/utils/types/elipsis_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<'a> ElipsisFields<'a> {
}

pub fn from_fmt_buffer<const N: usize>(
source: &'a Buffer<N>,
source: &'a mut Buffer<N>,
title: &'a str,
display_buf: &'a mut [u8; 20],
) -> Self {
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<'a> ElipsisFields<'a> {
}

pub fn from_capped_string<const N: usize>(
source: &'a CappedString<N>,
source: &'a mut CappedString<N>,
title: &'a str,
display_buf: &'a mut [u8; 20],
) -> Self {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading