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 allow custom build script path to escape package root #12286

Closed
Show file tree
Hide file tree
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
33 changes: 27 additions & 6 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2888,20 +2888,41 @@ impl TomlManifest {
&self,
build: &Option<StringOrBool>,
package_root: &Path,
) -> Option<PathBuf> {
) -> CargoResult<Option<PathBuf>> {
let build_rs = package_root.join("build.rs");
match *build {
// Explicitly no build script.
Some(StringOrBool::Bool(false)) => None,
Some(StringOrBool::Bool(true)) => Some(build_rs),
Some(StringOrBool::String(ref s)) => Some(PathBuf::from(s)),
Some(StringOrBool::Bool(false)) => Ok(None),
Some(StringOrBool::Bool(true)) => Ok(Some(build_rs)),
Some(StringOrBool::String(ref s)) => {
let custom_build = PathBuf::from(s);

// Check if custom build path escapes the package root. If so, bail.
// Can we assume that package_root is absolute path?
if custom_build.is_absolute() {
bail!("custom build script path cannot be absolute");
}
if custom_build.is_relative() {
let custom_build_path = package_root.join(&custom_build);
let custom_build_path = custom_build_path.canonicalize();
if custom_build_path.is_err() {
bail!("cannot find path to custom build script");
}
let custom_build_path = custom_build_path.unwrap();
if !custom_build_path.starts_with(package_root) {
bail!("custom build script has to be inside package directory");
}
}

Ok(Some(custom_build))
}
None => {
// If there is a `build.rs` file next to the `Cargo.toml`, assume it is
// a build script.
if build_rs.is_file() {
Some(build_rs)
Ok(Some(build_rs))
} else {
None
Ok(None)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/toml/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub fn targets(
)?);

// processing the custom build script
if let Some(custom_build) = manifest.maybe_custom_build(custom_build, package_root) {
if let Some(custom_build) = manifest.maybe_custom_build(custom_build, package_root)? {
if metabuild.is_some() {
anyhow::bail!("cannot specify both `metabuild` and `build`");
}
Expand Down