-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add new include_file_outside_project
lint
#13638
Open
GuillaumeGomez
wants to merge
3
commits into
rust-lang:master
Choose a base branch
from
GuillaumeGomez:include_file_outside_project
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+230
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
use rustc_ast::{Attribute, LitKind, MetaItem, MetaItemInner}; | ||
use rustc_data_structures::fx::FxHashSet; | ||
use rustc_hir::{Expr, ExprKind, HirId, Item}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::impl_lint_pass; | ||
use rustc_span::{FileName, Span, sym}; | ||
|
||
use clippy_config::Conf; | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::macros::root_macro_call_first_node; | ||
|
||
use cargo_metadata::MetadataCommand; | ||
|
||
use std::path::{Path, PathBuf}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Check if files included with one of the `include` macros (ie, `include!`, `include_bytes!` | ||
/// and `include_str!`) or the `path` attribute are actually part of the project. | ||
/// | ||
/// ### Why is this bad? | ||
/// If the included file is outside of the project folder, it will not be part of the releases, | ||
/// prevent project to work when others use it. | ||
/// | ||
/// ### Example | ||
/// ```ignore | ||
/// let x = include_str!("/etc/passwd"); | ||
/// | ||
/// #[path = "/etc/passwd"] | ||
/// mod bar; | ||
/// ``` | ||
/// Use instead: | ||
/// ```ignore | ||
/// let x = include_str!("./passwd"); | ||
/// | ||
/// #[path = "./passwd"] | ||
/// mod bar; | ||
/// ``` | ||
#[clippy::version = "1.84.0"] | ||
pub INCLUDE_FILE_OUTSIDE_PROJECT, | ||
suspicious, | ||
"checks that all included files are inside the project folder" | ||
} | ||
|
||
pub(crate) struct IncludeFileOutsideProject { | ||
cargo_manifest_dir: Option<PathBuf>, | ||
warned_spans: FxHashSet<PathBuf>, | ||
can_check_crate: bool, | ||
} | ||
|
||
impl_lint_pass!(IncludeFileOutsideProject => [INCLUDE_FILE_OUTSIDE_PROJECT]); | ||
|
||
impl IncludeFileOutsideProject { | ||
pub(crate) fn new(conf: &'static Conf) -> Self { | ||
let mut can_check_crate = true; | ||
if !conf.cargo_ignore_publish { | ||
match MetadataCommand::new().no_deps().exec() { | ||
Ok(metadata) => { | ||
for package in &metadata.packages { | ||
// only run the lint if publish is `None` (`publish = true` or skipped entirely) | ||
// or if the vector isn't empty (`publish = ["something"]`) | ||
if !matches!(package.publish.as_deref(), Some([]) | None) { | ||
can_check_crate = false; | ||
break; | ||
} | ||
} | ||
}, | ||
Err(_) => can_check_crate = false, | ||
} | ||
} | ||
|
||
Self { | ||
cargo_manifest_dir: std::env::var("CARGO_MANIFEST_DIR").ok().map(PathBuf::from), | ||
warned_spans: FxHashSet::default(), | ||
can_check_crate, | ||
} | ||
} | ||
|
||
fn check_file_path(&mut self, cx: &LateContext<'_>, span: Span) { | ||
if span.is_dummy() { | ||
return; | ||
} | ||
let source_map = cx.tcx.sess.source_map(); | ||
let file = source_map.lookup_char_pos(span.lo()).file; | ||
if let FileName::Real(real_filename) = file.name.clone() | ||
&& let Some(file_path) = real_filename.into_local_path() | ||
&& let Ok(file_path) = file_path.canonicalize() | ||
// Only lint once per path for `include` macros. | ||
&& !self.warned_spans.contains(&file_path) | ||
&& !self.is_part_of_project_dir(&file_path) | ||
{ | ||
let span = span.source_callsite(); | ||
self.emit_error(cx, span.with_hi(span.lo()), file_path); | ||
} | ||
} | ||
|
||
fn is_part_of_project_dir(&self, file_path: &Path) -> bool { | ||
if let Some(ref cargo_manifest_dir) = self.cargo_manifest_dir { | ||
// Check if both paths start with the same thing. | ||
let mut file_iter = file_path.iter(); | ||
|
||
for cargo_item in cargo_manifest_dir { | ||
match file_iter.next() { | ||
Some(file_path) if file_path == cargo_item => {}, | ||
_ => { | ||
// If we enter this arm, it means that the included file path is not | ||
// into the cargo manifest folder. | ||
return false; | ||
}, | ||
} | ||
} | ||
} | ||
true | ||
} | ||
|
||
fn emit_error(&mut self, cx: &LateContext<'_>, span: Span, file_path: PathBuf) { | ||
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] | ||
span_lint_and_then( | ||
cx, | ||
INCLUDE_FILE_OUTSIDE_PROJECT, | ||
span, | ||
"attempted to include a file outside of the project", | ||
|diag| { | ||
diag.note(format!( | ||
"file is located at `{}` which is outside of project folder (`{}`)", | ||
file_path.display(), | ||
self.cargo_manifest_dir.as_ref().unwrap().display(), | ||
)); | ||
}, | ||
); | ||
self.warned_spans.insert(file_path); | ||
} | ||
|
||
fn check_hir_id(&mut self, cx: &LateContext<'_>, span: Span, hir_id: HirId) { | ||
if self.cargo_manifest_dir.is_some() | ||
&& let hir = cx.tcx.hir() | ||
&& let Some(parent_hir_id) = hir.parent_id_iter(hir_id).next() | ||
&& let parent_span = hir.span(parent_hir_id) | ||
&& !parent_span.contains(span) | ||
{ | ||
self.check_file_path(cx, span); | ||
} | ||
} | ||
|
||
fn check_attribute(&mut self, cx: &LateContext<'_>, attr: &MetaItem) { | ||
let Some(ident) = attr.ident() else { return }; | ||
if ident.name == sym::path { | ||
if let Some(value) = attr.value_str() | ||
&& let Some(span) = attr.name_value_literal_span() | ||
&& let file_path = Path::new(value.as_str()) | ||
&& let Ok(file_path) = file_path.canonicalize() | ||
&& !self.is_part_of_project_dir(&file_path) | ||
{ | ||
self.emit_error(cx, span, file_path); | ||
} | ||
} else if ident.name == sym::cfg_attr | ||
&& let Some(&[_, MetaItemInner::MetaItem(ref attr)]) = attr.meta_item_list() | ||
{ | ||
self.check_attribute(cx, attr); | ||
} | ||
} | ||
} | ||
|
||
impl LateLintPass<'_> for IncludeFileOutsideProject { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) { | ||
if !self.can_check_crate { | ||
return; | ||
} | ||
if !expr.span.from_expansion() { | ||
self.check_hir_id(cx, expr.span, expr.hir_id); | ||
} else if let ExprKind::Lit(lit) = &expr.kind | ||
&& matches!(lit.node, LitKind::ByteStr(..) | LitKind::Str(..)) | ||
&& let Some(macro_call) = root_macro_call_first_node(cx, expr) | ||
&& (cx.tcx.is_diagnostic_item(sym::include_bytes_macro, macro_call.def_id) | ||
|| cx.tcx.is_diagnostic_item(sym::include_str_macro, macro_call.def_id)) | ||
{ | ||
self.check_hir_id(cx, expr.span, expr.hir_id); | ||
} | ||
} | ||
|
||
fn check_item(&mut self, cx: &LateContext<'_>, item: &'_ Item<'_>) { | ||
// Interestingly enough, `include!` content is not considered expanded. Which allows us | ||
// to easily filter out items we're not interested into. | ||
if self.can_check_crate && !item.span.from_expansion() { | ||
self.check_hir_id(cx, item.span, item.hir_id()); | ||
} | ||
} | ||
|
||
fn check_attributes(&mut self, cx: &LateContext<'_>, attrs: &[Attribute]) { | ||
if !self.can_check_crate { | ||
return; | ||
} | ||
for attr in attrs { | ||
if let Some(attr) = attr.meta() { | ||
self.check_attribute(cx, &attr); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//@ normalize-stderr-test: "located at `.+/.crates.toml`" -> "located at `$$DIR/.crates.toml`" | ||
//@ normalize-stderr-test: "folder \(`.+`" -> "folder (`$$CLIPPY_DIR`" | ||
|
||
#![deny(clippy::include_file_outside_project)] | ||
|
||
// Should not lint. | ||
include!("./auxiliary/external_consts.rs"); | ||
|
||
fn main() { | ||
let x = include_str!(concat!(env!("CARGO_HOME"), "/.crates.toml")); | ||
//~^ include_file_outside_project | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: attempted to include a file outside of the project | ||
--> tests/ui/include_file_outside_project.rs:10:13 | ||
| | ||
LL | let x = include_str!(concat!(env!("CARGO_HOME"), "/.crates.toml")); | ||
| ^ | ||
| | ||
= note: file is located at `$DIR/.crates.toml` which is outside of project folder (`$CLIPPY_DIR`) | ||
note: the lint level is defined here | ||
--> tests/ui/include_file_outside_project.rs:4:9 | ||
| | ||
LL | #![deny(clippy::include_file_outside_project)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 1 previous error | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would have to be a
cargo
lint if it's doing this to deduplicate thecargo metadata
invocation and so it doesn't happen by defaultBut I don't think we need to here,
cargo publish
already copies the project files intotarget/package/crate-x.y.z
before checking it builds, which would catch most../../outside-the-project
includesThe case that it wouldn't catch is an include of an absolute path, but it seems reasonable to lint these unconditionally