auth: fully percent-decode code/state from redirect (fixes Atlassian MCP login)
This commit is contained in:
1 parent
88ed54994e
commit
0cc0f8de70
1 file changed
+35
-5
@@ -253,6 +253,31 @@ pub async fn refresh(k: Kind, cred: &Credential) -> Result<Credential> {
|
|||||||
Ok(c)
|
Ok(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Percent-decode `%XX` (UTF-8, invalid escapes kept as-is); `+` stays `+` (codes may contain it).
|
||||||
|
pub fn urldec(s: &str) -> String {
|
||||||
|
let b = s.as_bytes();
|
||||||
|
let mut out = Vec::with_capacity(b.len());
|
||||||
|
let mut i = 0;
|
||||||
|
while i < b.len() {
|
||||||
|
let hex = |c: u8| (c as char).to_digit(16);
|
||||||
|
match (
|
||||||
|
b[i],
|
||||||
|
b.get(i + 1).and_then(|&c| hex(c)),
|
||||||
|
b.get(i + 2).and_then(|&c| hex(c)),
|
||||||
|
) {
|
||||||
|
(b'%', Some(h), Some(l)) => {
|
||||||
|
out.push((h * 16 + l) as u8);
|
||||||
|
i += 3;
|
||||||
|
}
|
||||||
|
(c, ..) => {
|
||||||
|
out.push(c);
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String::from_utf8_lossy(&out).into_owned()
|
||||||
|
}
|
||||||
|
|
||||||
/// Accepts a full redirect URL, `code#state`, a query string, or a bare code.
|
/// Accepts a full redirect URL, `code#state`, a query string, or a bare code.
|
||||||
pub fn parse_code(input: &str) -> (Option<String>, Option<String>) {
|
pub fn parse_code(input: &str) -> (Option<String>, Option<String>) {
|
||||||
let v = input.trim();
|
let v = input.trim();
|
||||||
@@ -261,11 +286,7 @@ pub fn parse_code(input: &str) -> (Option<String>, Option<String>) {
|
|||||||
let get = |key: &str| {
|
let get = |key: &str| {
|
||||||
q.split(['&', '#'])
|
q.split(['&', '#'])
|
||||||
.find_map(|kv| kv.strip_prefix(&format!("{key}=")))
|
.find_map(|kv| kv.strip_prefix(&format!("{key}=")))
|
||||||
.map(|s| {
|
.map(urldec)
|
||||||
s.replace("%2B", "+")
|
|
||||||
.replace("%2F", "/")
|
|
||||||
.replace("%3D", "=")
|
|
||||||
})
|
|
||||||
};
|
};
|
||||||
return (get("code"), get("state"));
|
return (get("code"), get("state"));
|
||||||
}
|
}
|
||||||
@@ -470,6 +491,15 @@ mod tests {
|
|||||||
assert_eq!(parse_code(""), (None, None));
|
assert_eq!(parse_code(""), (None, None));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn code_is_fully_decoded() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_code("/callback?code=a%3Ab%2Fc%2Bd+e%25&state=s%3D"),
|
||||||
|
(Some("a:b/c+d+e%".into()), Some("s=".into()))
|
||||||
|
);
|
||||||
|
assert_eq!(urldec("100%"), "100%");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn jwt_account() {
|
fn jwt_account() {
|
||||||
let p = URL_SAFE_NO_PAD
|
let p = URL_SAFE_NO_PAD
|
||||||
|
|||||||
Reference in new issue
Block a user