Skip to content

Commit d6035dd

Browse files
feat(cli): add trust and revoke custom language policies
1 parent 2533449 commit d6035dd

8 files changed

Lines changed: 329 additions & 90 deletions

File tree

‎Cargo.lock‎

Lines changed: 52 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/cli/Cargo.toml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ anyhow.workspace = true
3838
clap = { version = "4.5.4", features = ["derive"] }
3939
codespan-reporting = "0.13.0"
4040
crossterm = "0.29.0"
41+
directories = "6.0.0"
4142
ignore.workspace = true
4243
regex.workspace = true
4344
dashmap.workspace = true
@@ -50,18 +51,18 @@ serde_yaml.workspace = true
5051
serde-sarif = "0.8.0"
5152
similar = { version = "3.0.0", features = ["inline"] }
5253
smallvec = "1.13.2"
54+
tempfile = "3.10.1"
5355
tokio = { version = "1.37.0", features = ["rt-multi-thread", "io-std"] }
5456
clap_complete = "4.5.2"
5557
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
5658

5759
[dev-dependencies]
5860
assert_cmd = "2.0.14"
5961
predicates = "3.1.0"
60-
tempfile = "3.10.1"
6162

6263
# Support cargo bininstall, https://github.com/ast-grep/ast-grep/issues/1742
6364
[package.metadata.binstall]
6465
pkg-url = "{ repo }/releases/download/{ version }/app-{ target }{ archive-suffix }"
6566
pkg-fmt = "zip"
6667
bin-dir = "{ bin }{ binary-ext }"
67-
disabled-strategies = ["quick-install"]
68+
disabled-strategies = ["quick-install"]

‎crates/cli/src/config.rs‎

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::custom_language::CustomLanguageAction;
12
use crate::lang::{CustomLang, LanguageGlobs, SerializableInjection, SgLang};
23
use crate::utils::{ErrorContext as EC, RuleOverwrite, RuleTrace};
34

@@ -6,23 +7,13 @@ use ast_grep_config::{
67
DeserializeEnv, GlobalRules, RuleCollection, RuleConfig, from_str, from_yaml_string,
78
};
89
use ast_grep_language::config_file_type;
9-
use clap::ValueEnum;
1010
use ignore::WalkBuilder;
1111
use serde::{Deserialize, Serialize};
1212

1313
use std::collections::{HashMap, HashSet};
1414
use std::fs::read_to_string;
1515
use std::path::{Path, PathBuf};
1616

17-
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, ValueEnum)]
18-
pub enum CustomLanguagePolicy {
19-
/// Load native custom language libraries for this invocation.
20-
Allow,
21-
/// Skip native custom language libraries without reporting an error.
22-
#[default]
23-
Ignore,
24-
}
25-
2617
#[derive(Serialize, Deserialize, Clone)]
2718
#[serde(rename_all = "camelCase")]
2819
pub struct TestConfig {
@@ -107,12 +98,12 @@ impl ProjectConfig {
10798
/// The outer Result is for definitely wrong config.
10899
pub fn setup(
109100
config_path: Option<PathBuf>,
110-
custom_languages: CustomLanguagePolicy,
101+
custom_languages: CustomLanguageAction,
111102
) -> Result<Result<Self>> {
112103
let Some((project_dir, mut sg_config)) = Self::discover_project(config_path)? else {
113104
return Ok(Err(anyhow::anyhow!(EC::ProjectNotExist)));
114105
};
115-
let outline_rules = if custom_languages == CustomLanguagePolicy::Allow {
106+
let outline_rules = if custom_languages == CustomLanguageAction::Load {
116107
custom_language_outline_rules(&project_dir, sg_config.custom_languages.as_ref())
117108
} else {
118109
vec![]
@@ -145,19 +136,19 @@ fn custom_language_outline_rules(
145136
fn register_custom_language(
146137
project_dir: &Path,
147138
sg_config: AstGrepConfig,
148-
policy: CustomLanguagePolicy,
139+
action: CustomLanguageAction,
149140
) -> Result<()> {
150141
if let Some(custom_langs) = sg_config.custom_languages {
151-
match policy {
152-
CustomLanguagePolicy::Allow => {
142+
match action {
143+
CustomLanguageAction::Load => {
153144
SgLang::register_custom_language(project_dir, custom_langs)?;
154145
}
155-
CustomLanguagePolicy::Ignore if !custom_langs.is_empty() => {
146+
CustomLanguageAction::Ignore if !custom_langs.is_empty() => {
156147
eprintln!(
157148
"Warning: custom languages are ignored because native libraries can execute malicious code. Review the project before using `--custom-languages allow`."
158149
);
159150
}
160-
CustomLanguagePolicy::Ignore => {}
151+
CustomLanguageAction::Ignore => {}
161152
}
162153
}
163154
if let Some(globs) = sg_config.language_globs {
@@ -307,7 +298,9 @@ const CONFIG_FILE_YML: &str = "sgconfig.yml";
307298
const CONFIG_FILE_YAML: &str = "sgconfig.yaml";
308299

309300
/// return None if config file does not exist
310-
fn find_config_path_with_default(config_path: Option<PathBuf>) -> Result<Option<PathBuf>> {
301+
pub(crate) fn find_config_path_with_default(
302+
config_path: Option<PathBuf>,
303+
) -> Result<Option<PathBuf>> {
311304
if config_path.is_some() {
312305
return Ok(config_path);
313306
}

0 commit comments

Comments
 (0)