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

Reserved word check should be post canonical conversion #2161

Merged
merged 6 commits into from
Sep 24, 2024

Conversation

wilwade
Copy link
Collaborator

@wilwade wilwade commented Sep 24, 2024

Goal

The goal of this PR is to make sure that reserved words cannot be used in different forms.

Closes #2147

Discussion

  • Used to_lowercase instead of to_ascii_lowercase for better handling
  • Move the three canonical validations together

Checklist

  • Unit Tests added?
  • Spec version incremented?

@@ -35,7 +35,7 @@ pub fn convert_to_canonical(input_str: &str) -> alloc::string::String {
let white_space_stripped = strip_unicode_whitespace(input_str);
let diacriticals_stripped = strip_diacriticals(&white_space_stripped);
let confusables_removed = replace_confusables(&diacriticals_stripped);
confusables_removed.to_ascii_lowercase()
confusables_removed.to_lowercase()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why we didn't use this before, but we should have.

)?;

Self::validate_canonical_handle_length(&canonical_handle_str)?;
Self::validate_canonical_handle(&canonical_handle_str)?;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having more than one of these was nice, but getting confusing as we now had 3 checks to make on canonical handles

@@ -668,33 +664,36 @@ pub mod pallet {
Error::<T>::InvalidHandleCharacterLength
);

ensure!(!is_reserved_handle(&base_handle_str), Error::<T>::HandleIsNotAllowed);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer a check done on the base_handle pre-canonicalization

&canonical_handle_str,
)
.is_ok();
return Self::validate_canonical_handle(&canonical_handle_str).is_ok();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this wasn't doing all the other canonical checks, but it should have been.

@wilwade wilwade requested review from a team, shannonwells, mattheworris, enddynayn, aramikm, claireolmstead and JoeCap08055 and removed request for a team September 24, 2024 13:53
Copy link

codecov bot commented Sep 24, 2024

Codecov Report

Attention: Patch coverage is 87.50000% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pallets/handles/src/lib.rs 75.00% 2 Missing ⚠️
Files with missing lines Coverage Δ
pallets/handles/src/handles-utils/src/converter.rs 100.00% <100.00%> (ø)
pallets/handles/src/handles-utils/src/validator.rs 100.00% <100.00%> (ø)
pallets/handles/src/lib.rs 90.43% <75.00%> (-1.06%) ⬇️

Copy link
Collaborator

@mattheworris mattheworris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Read through changes
    lgtm

@github-actions github-actions bot added the metadata-changed Metadata has changed since the latest full release label Sep 24, 2024
Comment on lines +14 to +27

// We MUST have the RESERVED_WORDS constant as canonical.
// Cannot easily be canonicalized at compile time currently
#[test]
fn ensure_reserved_words_canonical() {
for &word in &RESERVED_WORDS {
let canonical = crate::convert_to_canonical(word);
assert_eq!(
word, canonical,
"The reserved word '{}' MUST match canonical form: '{}'",
word, canonical
);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this would be a better test or not; otherwise technically the test passes if even if convert_to_canonical is a no-op

Suggested change
// We MUST have the RESERVED_WORDS constant as canonical.
// Cannot easily be canonicalized at compile time currently
#[test]
fn ensure_reserved_words_canonical() {
for &word in &RESERVED_WORDS {
let canonical = crate::convert_to_canonical(word);
assert_eq!(
word, canonical,
"The reserved word '{}' MUST match canonical form: '{}'",
word, canonical
);
}
}
// We MUST have the RESERVED_WORDS constant as canonical.
// Cannot easily be canonicalized at compile time currently
#[test]
fn ensure_reserved_words_canonical() {
const TEST_WORDS: [&str; 8] =
["admin", "everyone", "all", "administrator", "mod", "moderator", "here", "channel"];
for (i, &word) in TEST_WORDS.iter().enumerate() {
let canonical = crate::convert_to_canonical(word);
assert_eq!(
RESERVED_WORDS[i], canonical,
"The reserved word '{}' MUST match canonical form: '{}'",
word, canonical
);
}
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. This is just a check to make sure that convert_to_canonical does NOT change the output of RESERVED_WORDS. Other test (`test_is_reserved_canonical_handle_happy_path) make sure that reserved words are rejected.

Copy link
Collaborator

@aramikm aramikm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read the code. Looked good to me

@wilwade wilwade enabled auto-merge (squash) September 24, 2024 18:26
Copy link
Collaborator

@shannonwells shannonwells left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wilwade wilwade merged commit c02cbe9 into main Sep 24, 2024
27 checks passed
@wilwade wilwade deleted the bug/reserved-word-check-post-canonical-2147 branch September 24, 2024 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metadata-changed Metadata has changed since the latest full release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reserved handle word check should be post-canonical
5 participants