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

Prevent improper files (like /dev/random) from being used as file arguments #10733

Merged
merged 12 commits into from
Jun 18, 2024
Merged
38 changes: 26 additions & 12 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use helix_lsp::{
use helix_stdx::path::get_relative_path;
use helix_view::{
align_view,
document::DocumentSavedEventResult,
document::{DocumentSavedEventResult, IrregularFileError},
editor::{ConfigEvent, EditorEvent},
graphics::Rect,
theme,
Expand Down Expand Up @@ -186,9 +186,17 @@ impl Application {
Some(Layout::Horizontal) => Action::HorizontalSplit,
None => Action::Load,
};
let doc_id = editor
let doc_id = match editor
.open(&file, action)
.context(format!("open '{}'", file.to_string_lossy()))?;
.context(format!("open '{}'", file.to_string_lossy()))
{
// Ignore irregular files during application init.
Err(e) if e.is::<IrregularFileError>() => {
nr_of_files -= 1;
continue;
}
a => a,
}?;
// with Action::Load all documents have the same view
// NOTE: this isn't necessarily true anymore. If
// `--vsplit` or `--hsplit` are used, the file which is
Expand All @@ -199,15 +207,21 @@ impl Application {
doc.set_selection(view_id, pos);
}
}
editor.set_status(format!(
"Loaded {} file{}.",
nr_of_files,
if nr_of_files == 1 { "" } else { "s" } // avoid "Loaded 1 files." grammo
));
// align the view to center after all files are loaded,
// does not affect views without pos since it is at the top
let (view, doc) = current!(editor);
align_view(doc, view, Align::Center);

// if all files were invalid, replace with empty buffer
if nr_of_files == 0 {
editor.new_file(Action::VerticalSplit);
} else {
editor.set_status(format!(
"Loaded {} file{}.",
nr_of_files,
if nr_of_files == 1 { "" } else { "s" } // avoid "Loaded 1 files." grammo
));
// align the view to center after all files are loaded,
// does not affect views without pos since it is at the top
let (view, doc) = current!(editor);
align_view(doc, view, Align::Center);
}
} else {
editor.new_file(Action::VerticalSplit);
}
Expand Down
18 changes: 18 additions & 0 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ pub struct SavePoint {
revert: Mutex<Transaction>,
}

#[derive(Debug)]
pub struct IrregularFileError {
msg: String,
}

impl Display for IrregularFileError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.msg)
}
}

pub struct Document {
pub(crate) id: DocumentId,
text: Rope,
Expand Down Expand Up @@ -686,6 +697,13 @@ impl Document {
config_loader: Option<Arc<ArcSwap<syntax::Loader>>>,
config: Arc<dyn DynAccess<Config>>,
) -> Result<Self, Error> {
// if the path is not a regular file (e.g.: /dev/random) it should not be opened
if path.exists() && !path.is_file() && !path.is_symlink() {
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
return Err(anyhow::anyhow!(IrregularFileError {
msg: format!("Path argument must be a regular file, a directory, or a symlink.")
}));
}

// Open the file if it exists, otherwise assume it is a new file (and thus empty).
let (rope, encoding, has_bom) = if path.exists() {
let mut file =
Expand Down
Loading