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

don't use errnos when we didn't see that errno #926

Merged
merged 1 commit into from
Apr 15, 2022
Merged
Changes from all commits
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
36 changes: 20 additions & 16 deletions nexus/src/external_api/console_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,9 @@ pub async fn asset(
Some(static_dir) => find_file(path, &static_dir.join("assets")),
_ => Err(not_found("static_dir undefined")),
}?;
let file_contents =
tokio::fs::read(&file).await.map_err(|_| not_found("EBADF"))?;
let file_contents = tokio::fs::read(&file)
.await
.map_err(|e| not_found(&format!("accessing {:?}: {:#}", file, e)))?;

// Derive the MIME type from the file name
let content_type = mime_guess::from_path(&file)
Expand Down Expand Up @@ -316,8 +317,9 @@ async fn serve_console_index(
.to_owned()
.ok_or_else(|| not_found("static_dir undefined"))?;
let file = static_dir.join(PathBuf::from("index.html"));
let file_contents =
tokio::fs::read(&file).await.map_err(|_| not_found("EBADF"))?;
let file_contents = tokio::fs::read(&file)
.await
.map_err(|e| not_found(&format!("accessing {:?}: {:#}", file, e)))?;
Ok(Response::builder()
.status(StatusCode::OK)
.header(http::header::CONTENT_TYPE, "text/html; charset=UTF-8")
Expand Down Expand Up @@ -358,27 +360,29 @@ fn find_file(
// If we hit a non-directory thing already and we still have segments
// left in the path, bail. We have nowhere to go.
if !current.is_dir() {
return Err(not_found("ENOENT"));
return Err(not_found("expected a directory"));
}

current.push(segment);

// Don't follow symlinks.
// Error means either the user doesn't have permission to pull
// metadata or the path doesn't exist.
let m = current.symlink_metadata().map_err(|_| not_found("ENOENT"))?;
let m = current
.symlink_metadata()
.map_err(|_| not_found("failed to get file metadata"))?;
if m.file_type().is_symlink() {
return Err(not_found("EMLINK"));
return Err(not_found("attempted to follow a symlink"));
}
}

// can't serve a directory
if current.is_dir() {
return Err(not_found("EISDIR"));
return Err(not_found("expected a non-directory"));
}

if !file_ext_allowed(&current) {
return Err(not_found("EACCES"));
return Err(not_found("file extension not allowed"));
}

Ok(current)
Expand Down Expand Up @@ -409,7 +413,7 @@ mod test {
let error = find_file(get_path("tests/static/nonexistent.svg"), &root)
.unwrap_err();
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
assert_eq!(error.internal_message, "ENOENT".to_string());
assert_eq!(error.internal_message, "failed to get file metadata",);
}

#[test]
Expand All @@ -419,7 +423,7 @@ mod test {
find_file(get_path("tests/static/a/b/c/nonexistent.svg"), &root)
.unwrap_err();
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
assert_eq!(error.internal_message, "ENOENT".to_string());
assert_eq!(error.internal_message, "failed to get file metadata")
}

#[test]
Expand All @@ -429,7 +433,7 @@ mod test {
find_file(get_path("tests/static/assets/a_directory"), &root)
.unwrap_err();
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
assert_eq!(error.internal_message, "EISDIR".to_string());
assert_eq!(error.internal_message, "expected a non-directory");
}

#[test]
Expand All @@ -448,7 +452,7 @@ mod test {
// so we 404
let error = find_file(get_path(path_str), &root).unwrap_err();
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
assert_eq!(error.internal_message, "EMLINK".to_string());
assert_eq!(error.internal_message, "attempted to follow a symlink");
}

#[test]
Expand All @@ -462,7 +466,7 @@ mod test {
// but it 404s because the path goes through a symlink
let error = find_file(get_path(path_str), &root).unwrap_err();
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
assert_eq!(error.internal_message, "EMLINK".to_string());
assert_eq!(error.internal_message, "attempted to follow a symlink");
}

#[test]
Expand All @@ -472,11 +476,11 @@ mod test {
find_file(get_path("tests/static/assets/blocked.ext"), &root)
.unwrap_err();
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
assert_eq!(error.internal_message, "EACCES".to_string());
assert_eq!(error.internal_message, "file extension not allowed",);

let error = find_file(get_path("tests/static/assets/no_ext"), &root)
.unwrap_err();
assert_eq!(error.status_code, StatusCode::NOT_FOUND);
assert_eq!(error.internal_message, "EACCES".to_string());
assert_eq!(error.internal_message, "file extension not allowed");
}
}