Skip to content

Commit 1b0af07

Browse files
committed
Give ArgumentSafety traits; test *_as_argument methods
The three new `Url::*_as_argument` methods were only tested indirectly before (and the path one not at all). This adds tests for them so they have the same direct coverage as the corresponding `*_argument_safe` methods. The `Debug` and `PartialEq` traits added to `ArgumentSafety` are primarily added to allow the new assertions to be written in a more streamlined way (without matching).
1 parent 5457998 commit 1b0af07

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

gix-url/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub fn expand_path(user: Option<&expand_path::ForUser>, path: &BStr) -> Result<P
6666
///
6767
/// This type only expresses known *syntactic* risk. It does not cover other risks, such as passing a personal access
6868
/// token as a username rather than a password in an application that logs usernames.
69+
#[derive(Debug, PartialEq)]
6970
pub enum ArgumentSafety<T> {
7071
/// May be safe. There is nothing to pass, so there is nothing dangerous.
7172
Absent,

gix-url/tests/access/mod.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ mod canonicalized {
3030
}
3131
}
3232

33+
use gix_url::ArgumentSafety;
34+
3335
#[test]
3436
fn user() -> crate::Result {
3537
let mut url = gix_url::parse("https://user:password@host/path".into())?;
@@ -53,81 +55,108 @@ fn password() -> crate::Result {
5355
}
5456

5557
#[test]
56-
fn user_argument_safe() -> crate::Result {
58+
fn user_argument_safety() -> crate::Result {
5759
let url = gix_url::parse("ssh://-Fconfigfile@foo/bar".into())?;
5860

5961
assert_eq!(url.user(), Some("-Fconfigfile"));
62+
assert_eq!(url.user_as_argument(), ArgumentSafety::Dangerous("-Fconfigfile"));
6063
assert_eq!(url.user_argument_safe(), None); // An unsafe username is blocked.
6164

6265
assert_eq!(url.host(), Some("foo"));
66+
assert_eq!(url.host_as_argument(), ArgumentSafety::Usable("foo"));
6367
assert_eq!(url.host_argument_safe(), Some("foo"));
6468

6569
assert_eq!(url.path, "/bar");
70+
assert_eq!(url.path_as_argument(), ArgumentSafety::Usable("/bar".into()));
6671
assert_eq!(url.path_argument_safe(), Some("/bar".into()));
6772

6873
Ok(())
6974
}
7075

7176
#[test]
72-
fn host_argument_safe() -> crate::Result {
77+
fn host_argument_safety() -> crate::Result {
7378
let url = gix_url::parse("ssh://-oProxyCommand=open$IFS-aCalculator/foo".into())?;
7479

7580
assert_eq!(url.user(), None);
81+
assert_eq!(url.user_as_argument(), ArgumentSafety::Absent);
7682
assert_eq!(url.user_argument_safe(), None); // As there is no user. See all_argument_safe_valid().
7783

7884
assert_eq!(url.host(), Some("-oProxyCommand=open$IFS-aCalculator"));
85+
assert_eq!(
86+
url.host_as_argument(),
87+
ArgumentSafety::Dangerous("-oProxyCommand=open$IFS-aCalculator")
88+
);
7989
assert_eq!(url.host_argument_safe(), None); // An unsafe host string is blocked.
8090

8191
assert_eq!(url.path, "/foo");
92+
assert_eq!(url.path_as_argument(), ArgumentSafety::Usable("/foo".into()));
8293
assert_eq!(url.path_argument_safe(), Some("/foo".into()));
8394

8495
Ok(())
8596
}
8697

8798
#[test]
88-
fn path_argument_safe() -> crate::Result {
99+
fn path_argument_safety() -> crate::Result {
89100
let url = gix_url::parse("ssh://foo/-oProxyCommand=open$IFS-aCalculator".into())?;
90101

91102
assert_eq!(url.user(), None);
103+
assert_eq!(url.user_as_argument(), ArgumentSafety::Absent);
92104
assert_eq!(url.user_argument_safe(), None); // As there is no user. See all_argument_safe_valid().
93105

94106
assert_eq!(url.host(), Some("foo"));
107+
assert_eq!(url.host_as_argument(), ArgumentSafety::Usable("foo"));
95108
assert_eq!(url.host_argument_safe(), Some("foo"));
96109

97110
assert_eq!(url.path, "/-oProxyCommand=open$IFS-aCalculator");
111+
assert_eq!(
112+
url.path_as_argument(),
113+
ArgumentSafety::Dangerous("/-oProxyCommand=open$IFS-aCalculator".into())
114+
);
98115
assert_eq!(url.path_argument_safe(), None); // An unsafe path is blocked.
99116

100117
Ok(())
101118
}
102119

103120
#[test]
104-
fn all_argument_safe_allowed() -> crate::Result {
121+
fn all_argument_safety_safe() -> crate::Result {
105122
let url = gix_url::parse("ssh://[email protected]/path/to/file".into())?;
106123

107124
assert_eq!(url.user(), Some("user.name"));
125+
assert_eq!(url.user_as_argument(), ArgumentSafety::Usable("user.name"));
108126
assert_eq!(url.user_argument_safe(), Some("user.name"));
109127

110128
assert_eq!(url.host(), Some("example.com"));
129+
assert_eq!(url.host_as_argument(), ArgumentSafety::Usable("example.com"));
111130
assert_eq!(url.host_argument_safe(), Some("example.com"));
112131

113132
assert_eq!(url.path, "/path/to/file");
133+
assert_eq!(url.path_as_argument(), ArgumentSafety::Usable("/path/to/file".into()));
114134
assert_eq!(url.path_argument_safe(), Some("/path/to/file".into()));
115135

116136
Ok(())
117137
}
118138

119139
#[test]
120-
fn all_argument_safe_disallowed() -> crate::Result {
140+
fn all_argument_safety_not_safe() -> crate::Result {
121141
let all_bad = "ssh://-Fconfigfile@-oProxyCommand=open$IFS-aCalculator/-oProxyCommand=open$IFS-aCalculator";
122142
let url = gix_url::parse(all_bad.into())?;
123143

124144
assert_eq!(url.user(), Some("-Fconfigfile"));
145+
assert_eq!(url.user_as_argument(), ArgumentSafety::Dangerous("-Fconfigfile"));
125146
assert_eq!(url.user_argument_safe(), None); // An unsafe username is blocked.
126147

127148
assert_eq!(url.host(), Some("-oProxyCommand=open$IFS-aCalculator"));
149+
assert_eq!(
150+
url.host_as_argument(),
151+
ArgumentSafety::Dangerous("-oProxyCommand=open$IFS-aCalculator")
152+
);
128153
assert_eq!(url.host_argument_safe(), None); // An unsafe host string is blocked.
129154

130155
assert_eq!(url.path, "/-oProxyCommand=open$IFS-aCalculator");
156+
assert_eq!(
157+
url.path_as_argument(),
158+
ArgumentSafety::Dangerous("/-oProxyCommand=open$IFS-aCalculator".into())
159+
);
131160
assert_eq!(url.path_argument_safe(), None); // An unsafe path is blocked.
132161

133162
Ok(())

0 commit comments

Comments
 (0)