Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Duniter v2S
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
nodes
rust
Duniter v2S
Commits
bb12ac37
Commit
bb12ac37
authored
2 years ago
by
Hugo Trentesaux
Browse files
Options
Downloads
Patches
Plain Diff
restrict identity name
see discussion
https://forum.duniter.org/t/format-du-userid/10147
parent
f6836691
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
primitives/duniter/src/lib.rs
+16
-17
16 additions, 17 deletions
primitives/duniter/src/lib.rs
with
16 additions
and
17 deletions
primitives/duniter/src/lib.rs
+
16
−
17
View file @
bb12ac37
...
...
@@ -17,23 +17,15 @@
#![cfg_attr(not(feature
=
"std"
),
no_std)]
/// Rules for valid identity names are defined below
/// - Bound length to 64
/// - forbid trailing or double spaces
/// - accept only ascii alphanumeric or punctuation or space
/// - Bound length to 42
/// - accept only ascii alphanumeric or - or _
pub
fn
validate_idty_name
(
idty_name
:
&
[
u8
])
->
bool
{
idty_name
.len
()
>=
3
&&
idty_name
.len
()
<=
64
// length smaller than 64
&&
idty_name
[
0
]
!=
32
// does not start with space
&&
idty_name
[
idty_name
.len
()
-
1
]
!=
32
// does not end with space
// all characters are alphanumeric or punctuation or space
&&
idty_name
.len
()
<=
42
// length smaller than 42
// all characters are alphanumeric or - or _
&&
idty_name
.iter
()
.all
(|
c
|
c
.is_ascii_alphanumeric
()
||
c
.is_ascii_punctuation
()
||
*
c
==
32
)
// disallow two consecutive spaces
&&
idty_name
.iter
()
.zip
(
idty_name
.iter
()
.skip
(
1
))
.all
(|(
c1
,
c2
)|
*
c1
!=
32
||
*
c2
!=
32
)
.all
(|
c
|
c
.is_ascii_alphanumeric
()
||
*
c
==
b'-'
||
*
c
==
b'_'
)
}
#[cfg(test)]
...
...
@@ -42,12 +34,19 @@ mod tests {
#[test]
fn
test_validate_idty_name
()
{
// --- allow
assert!
(
validate_idty_name
(
b"B0b"
));
assert!
(
validate_idty_name
(
b"lorem ipsum dolor-sit_amet."
));
assert!
(
!
validate_idty_name
(
b" space"
));
assert!
(
!
validate_idty_name
(
b"space "
));
assert!
(
!
validate_idty_name
(
b"double space"
));
assert!
(
validate_idty_name
(
b"lorem_ipsum-dolor-sit_amet"
));
assert!
(
validate_idty_name
(
b"1_______________________________________42"
));
// --- disallow
assert!
(
!
validate_idty_name
(
b"1________________________________________43"
));
assert!
(
!
validate_idty_name
(
b"with space"
));
assert!
(
!
validate_idty_name
(
"non-ascii🌵"
.as_bytes
()));
assert!
(
!
validate_idty_name
(
"ğune"
.as_bytes
()));
assert!
(
!
validate_idty_name
(
"toto!"
.as_bytes
()));
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment