Skip to content

Commit af5e993

Browse files
committed
fix: make path traversal check in aptos_contract_file! non-bypassable
Canonicalization failures now produce compile errors instead of silently skipping the path validation check.
1 parent 62dacf6 commit af5e993

File tree

1 file changed

+26
-5
lines changed
  • crates/aptos-sdk-macros/src

1 file changed

+26
-5
lines changed

crates/aptos-sdk-macros/src/lib.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,32 @@ pub fn aptos_contract_file(input: TokenStream) -> TokenStream {
116116
let file_path = manifest_path.join(&input.path);
117117

118118
// SECURITY: Verify the resolved path is under CARGO_MANIFEST_DIR to prevent
119-
// path traversal attacks (e.g., "../../../../etc/passwd")
120-
if let (Ok(canonical_manifest), Ok(canonical_file)) =
121-
(manifest_path.canonicalize(), file_path.canonicalize())
122-
&& !canonical_file.starts_with(&canonical_manifest)
123-
{
119+
// path traversal attacks (e.g., "../../../../etc/passwd").
120+
// Canonicalization failures are treated as errors to ensure this check
121+
// is never silently skipped.
122+
let canonical_manifest = match manifest_path.canonicalize() {
123+
Ok(p) => p,
124+
Err(e) => {
125+
return syn::Error::new(
126+
input.name.span(),
127+
format!("Failed to resolve project directory: {e}"),
128+
)
129+
.to_compile_error()
130+
.into();
131+
}
132+
};
133+
let canonical_file = match file_path.canonicalize() {
134+
Ok(p) => p,
135+
Err(e) => {
136+
return syn::Error::new(
137+
input.name.span(),
138+
format!("Failed to resolve ABI file path '{}': {e}", input.path),
139+
)
140+
.to_compile_error()
141+
.into();
142+
}
143+
};
144+
if !canonical_file.starts_with(&canonical_manifest) {
124145
return syn::Error::new(
125146
input.name.span(),
126147
format!(

0 commit comments

Comments
 (0)