Skip to content

Commit 920b007

Browse files
committed
Auto merge of #362 - pietroalbini:fix-broken-agent-sha-detection, r=pietroalbini
Fixes to previous PRs
2 parents 9975823 + c8b5fb7 commit 920b007

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

src/agent/api.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,17 @@ impl AgentApi {
9191
let retry = if let Some(AgentApiError::ServerUnavailable) = err.downcast_ref() {
9292
true
9393
} else if let Some(err) = err.downcast_ref::<::reqwest::Error>() {
94-
err.cause()
95-
.map(|cause| cause.downcast_ref::<::std::io::Error>().is_some())
96-
.unwrap_or(false)
94+
let reqwest_io = err
95+
.get_ref()
96+
.map(|inner| inner.is::<::std::io::Error>())
97+
.unwrap_or(false);
98+
let hyper_io = err
99+
.get_ref()
100+
.and_then(|inner| inner.downcast_ref::<::hyper::Error>())
101+
.and_then(|inner| inner.cause2())
102+
.map(|inner| inner.is::<::std::io::Error>())
103+
.unwrap_or(false);
104+
reqwest_io || hyper_io
97105
} else {
98106
false
99107
};

src/server/auth.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use warp::{self, Filter, Rejection};
1010

1111
lazy_static! {
1212
static ref GIT_REVISION_RE: Regex =
13-
Regex::new(r"^crater(-agent)?/(?P<sha>[a-f0-9]{7,40})$").unwrap();
13+
Regex::new(r"^crater(-agent)?/(?P<sha>[a-f0-9]{7,40})( \(.*\))?$").unwrap();
1414
}
1515

1616
#[derive(Copy, Clone)]
@@ -38,15 +38,17 @@ fn parse_token(authorization: &str) -> Option<&str> {
3838
None
3939
}
4040

41+
fn git_revision(user_agent: &str) -> Option<String> {
42+
GIT_REVISION_RE
43+
.captures(user_agent)
44+
.map(|cap| cap["sha"].to_string())
45+
}
46+
4147
fn check_auth(data: &Data, headers: &HeaderMap, token_type: TokenType) -> Option<AuthDetails> {
4248
// Try to extract the git revision from the User-Agent header
4349
let git_revision = if let Some(ua_value) = headers.get(USER_AGENT) {
4450
if let Ok(ua) = ua_value.to_str() {
45-
if let Some(cap) = GIT_REVISION_RE.captures(ua) {
46-
Some(cap["sha"].to_string())
47-
} else {
48-
None
49-
}
51+
git_revision(ua)
5052
} else {
5153
None
5254
}
@@ -177,7 +179,7 @@ impl ACL {
177179

178180
#[cfg(test)]
179181
mod tests {
180-
use super::parse_token;
182+
use super::{git_revision, parse_token};
181183

182184
#[test]
183185
fn test_parse_token() {
@@ -187,4 +189,28 @@ mod tests {
187189
assert_eq!(parse_token("CraterToken foo"), Some("foo"));
188190
assert_eq!(parse_token("CraterToken foo bar"), None);
189191
}
192+
193+
#[test]
194+
fn test_git_revision() {
195+
for sha in &["0000000", "0000000000000000000000000000000000000000"] {
196+
assert_eq!(
197+
git_revision(&format!("crater/{}", sha)),
198+
Some(sha.to_string())
199+
);
200+
assert_eq!(
201+
git_revision(&format!("crater/{} (foo bar!)", sha)),
202+
Some(sha.to_string())
203+
);
204+
}
205+
206+
// Test with too few and too many digits
207+
assert!(git_revision("crater/000000").is_none());
208+
assert!(git_revision("crater/00000000000000000000000000000000000000000").is_none());
209+
210+
// Test invalid syntaxes
211+
assert!(git_revision("crater/ggggggg").is_none());
212+
assert!(git_revision("crater/0000000(foo bar!)").is_none());
213+
assert!(git_revision("crater/0000000 (foo bar!) ").is_none());
214+
assert!(git_revision("crate/0000000").is_none());
215+
}
190216
}

0 commit comments

Comments
 (0)