Skip to content

Commit

Permalink
repo: Allow /s in repo names
Browse files Browse the repository at this point in the history
At Endless we have repos organized into staging subdirectories, but the
repo resource was only allowing a single path element. This changes the
parameter to a single glob and then walks backwards to find a repo name
that matches a path prefix.
  • Loading branch information
dbnicholson committed Nov 12, 2019
1 parent 5252cb3 commit c53aad7
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ impl Config {
pub fn get_repoconfig(&self, name: &str) -> Result<&RepoConfig, ApiError> {
self.repos.get(name).ok_or_else (|| ApiError::BadRequest("No such repo".to_string()))
}

pub fn get_repoconfig_from_path(&self, path: &Path) -> Result<&RepoConfig, ApiError> {
for ancestor in path.ancestors() {
if let Some(name) = ancestor.to_str() {
if let Some(config) = self.repos.get(name) {
return Ok(config)
}
}
}
Err(ApiError::BadRequest("No such repo".to_string()))
}
}


Expand Down Expand Up @@ -319,11 +330,13 @@ fn handle_build_repo(req: &HttpRequest<AppState>) -> actix_web::Result<NamedFile

fn handle_repo(req: &HttpRequest<AppState>) -> actix_web::Result<NamedFile> {
let tail: String = req.match_info().query("tail")?;
let repo: String = req.match_info().query("repo")?;
let state = req.state();
let repoconfig = state.config.get_repoconfig(&repo)?;
// Strip out any "../.." or other unsafe things
let relpath = PathBuf::from_param(tail.trim_start_matches('/'))?;
let tailpath = PathBuf::from_param(tail.trim_start_matches('/'))?;
let state = req.state();
let repoconfig = state.config.get_repoconfig_from_path(&tailpath)?;
let namepath = Path::new(&repoconfig.name);
let relpath = tailpath.strip_prefix(&namepath)
.map_err(|e| ApiError::InternalServerError(e.to_string()))?;
let path = Path::new(&repoconfig.path).join(&relpath);
if path.is_dir() {
return Err(ErrorNotFound("Ignoring directory"));
Expand Down Expand Up @@ -389,7 +402,7 @@ pub fn create_app(
r.head().f(handle_build_repo);
r.f(|_| HttpResponse::MethodNotAllowed())
})
.resource("/repo/{repo}/{tail:.*}", |r| {
.resource("/repo/{tail:.*}", |r| {
r.get().f(handle_repo);
r.head().f(handle_repo);
r.f(|_| HttpResponse::MethodNotAllowed())
Expand Down

0 comments on commit c53aad7

Please sign in to comment.