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

Normalise paths before comparing to signed values #17

Merged
merged 2 commits into from
Sep 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/request_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,25 @@
.map_err(|e| VerifyError::ExtractHeader(SIGNATURE_JWT_V1_HEADER, Box::new(e)))?
.ok_or(VerifyError::MissingHeader(SIGNATURE_JWT_V1_HEADER))?;

self.check_v1_keys(jwt, path)
self.check_v1_keys(jwt, Self::normalise_path(path))
}
SIGNATURE_SCHEME_UNSIGNED => Err(VerifyError::UnsignedRequest),
scheme => Err(VerifyError::BadSchemeHeader(scheme.to_owned())),
}
}

fn normalise_path<'a>(path: &'a str) -> &'a str {

Check failure on line 147 in src/request_identity.rs

View workflow job for this annotation

GitHub Actions / Build and test with features 'all' (ubuntu-22.04)

the following explicit lifetimes could be elided: 'a
let slashes: Vec<usize> = path.match_indices('/').map(|(index, _)| index).collect();
if slashes.len() >= 3
&& &path[slashes[slashes.len() - 3]..slashes[slashes.len() - 2]] == "/invoke"
{
&path[slashes[slashes.len() - 3]..]
} else if !slashes.is_empty() && &path[slashes[slashes.len() - 1]..] == "/discover" {
&path[slashes[slashes.len() - 1]..]
} else {
path
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -195,6 +208,31 @@
assert!(verifier.verify_identity(&headers, "/invoke/foo").is_err())
}

#[test]
fn normalise_path() {
let paths = vec![
("/invoke/a/b", "/invoke/a/b"),
("/foo/invoke/a/b", "/invoke/a/b"),
("/foo/bar/invoke/a/b", "/invoke/a/b"),
("/discover", "/discover"),
("/foo/discover", "/discover"),
("/foo/bar/discover", "/discover"),
("/foo", "/foo"),
("/invoke", "/invoke"),
("/foo/invoke", "/foo/invoke"),
("/invoke/a", "/invoke/a"),
("/foo/invoke/a", "/foo/invoke/a"),
("", ""),
("/", "/"),
("discover", "discover"),
];

for (path, expected_path) in paths {
let actual_path = IdentityVerifier::normalise_path(path);
assert_eq!(expected_path, actual_path)
}
}

fn mock_token_and_key() -> (String, String) {
let serialized_keypair = Ed25519KeyPair::generate_pkcs8(&SystemRandom::new()).unwrap();
let keypair = Ed25519KeyPair::from_pkcs8(serialized_keypair.as_ref()).unwrap();
Expand Down
Loading