Skip to content

Commit a0a480c

Browse files
fix: require opt-in for custom language libraries
1 parent 979c143 commit a0a480c

6 files changed

Lines changed: 182 additions & 12 deletions

File tree

‎crates/cli/src/config.rs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl ProjectConfig {
9595
/// returns a Result of Result.
9696
/// The inner Result is for configuration not found, or ProjectNotExist
9797
/// The outer Result is for definitely wrong config.
98-
pub fn setup(config_path: Option<PathBuf>) -> Result<Result<Self>> {
98+
pub fn setup(config_path: Option<PathBuf>, allow_custom_languages: bool) -> Result<Result<Self>> {
9999
let Some((project_dir, mut sg_config)) = Self::discover_project(config_path)? else {
100100
return Ok(Err(anyhow::anyhow!(EC::ProjectNotExist)));
101101
};
@@ -109,7 +109,7 @@ impl ProjectConfig {
109109
util_dirs: sg_config.util_dirs.take(),
110110
};
111111
// sg_config will not use rule dirs and test configs anymore
112-
register_custom_language(&config.project_dir, sg_config)?;
112+
register_custom_language(&config.project_dir, sg_config, allow_custom_languages)?;
113113
Ok(Ok(config))
114114
}
115115
}
@@ -126,8 +126,15 @@ fn custom_language_outline_rules(
126126
.collect()
127127
}
128128

129-
fn register_custom_language(project_dir: &Path, sg_config: AstGrepConfig) -> Result<()> {
129+
fn register_custom_language(
130+
project_dir: &Path,
131+
sg_config: AstGrepConfig,
132+
allow_custom_languages: bool,
133+
) -> Result<()> {
130134
if let Some(custom_langs) = sg_config.custom_languages {
135+
if !custom_langs.is_empty() && !allow_custom_languages {
136+
return Err(anyhow::anyhow!(EC::CustomLanguageNotAllowed));
137+
}
131138
SgLang::register_custom_language(project_dir, custom_langs)?;
132139
}
133140
if let Some(globs) = sg_config.language_globs {

‎crates/cli/src/lib.rs‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ struct App {
4646
/// Path to ast-grep root config, default is sgconfig.yml.
4747
#[clap(short, long, global = true, value_name = "CONFIG_FILE")]
4848
config: Option<PathBuf>,
49+
/// Allow sgconfig.yml to load native custom language libraries.
50+
#[clap(long, global = true)]
51+
allow_custom_languages: bool,
4952
}
5053

5154
#[derive(Subcommand)]
@@ -96,15 +99,27 @@ fn try_default_run(args: &[String]) -> Result<Option<RunArg>> {
9699
let should_use_default_run_command = (is_pattern || is_kind) && args[1].starts_with('-');
97100
if should_use_default_run_command {
98101
// handle no subcommand
99-
let arg = RunArg::try_parse_from(args)?;
102+
let delimiter = args
103+
.iter()
104+
.position(|arg| arg == "--")
105+
.unwrap_or(args.len());
106+
let filtered_args = args
107+
.iter()
108+
.enumerate()
109+
.filter(|(index, arg)| *index >= delimiter || arg.as_str() != "--allow-custom-languages")
110+
.map(|(_, arg)| arg);
111+
let arg = RunArg::try_parse_from(filtered_args)?;
100112
Ok(Some(arg))
101113
} else {
102114
Ok(None)
103115
}
104116
}
105117

106118
/// finding project and setup custom language configuration
107-
fn setup_project_is_possible(args: &[String]) -> Result<Result<ProjectConfig>> {
119+
fn setup_project_is_possible(
120+
args: &[String],
121+
allow_custom_languages: bool,
122+
) -> Result<Result<ProjectConfig>> {
108123
let mut config = None;
109124
for i in 0..args.len() {
110125
let arg = &args[i];
@@ -124,21 +139,29 @@ fn setup_project_is_possible(args: &[String]) -> Result<Result<ProjectConfig>> {
124139
let config_file = (&args[i + 1]).into();
125140
config = Some(config_file);
126141
}
127-
ProjectConfig::setup(config)
142+
ProjectConfig::setup(config, allow_custom_languages)
128143
}
129144

130145
// this wrapper function is for testing
131146
pub fn main_with_args(args: impl Iterator<Item = String>) -> Result<ExitCode> {
132147
let args: Vec<_> = args.collect();
148+
let allow_custom_languages = args
149+
.iter()
150+
.take_while(|arg| arg.as_str() != "--")
151+
.any(|arg| arg == "--allow-custom-languages");
133152
// do not unwrap project before cmd parsing
134153
// sg help does not need a valid sgconfig.yml
135-
let project = setup_project_is_possible(&args);
154+
let project = setup_project_is_possible(&args, allow_custom_languages);
136155
if let Some(arg) = try_default_run(&args)? {
137156
return run_with_pattern(arg, project?);
138157
}
139158
let app = App::try_parse_from(args)?;
159+
let command = match app.command {
160+
Commands::Completions(arg) => return run_shell_completion::<App>(arg),
161+
command => command,
162+
};
140163
let project = project?; // unwrap here to report invalid project
141-
match app.command {
164+
match command {
142165
Commands::Run(arg) => run_with_pattern(arg, project),
143166
Commands::Scan(arg) => run_with_config(arg, project),
144167
Commands::Test(arg) => run_test_rule(arg, project),

‎crates/cli/src/new.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ mod test {
379379
}
380380

381381
fn create_rule(temp: &Path) -> Result<()> {
382-
let project = ProjectConfig::setup(Some(temp.join("sgconfig.yml")))?;
382+
let project = ProjectConfig::setup(Some(temp.join("sgconfig.yml")), false)?;
383383
let arg = NewArg {
384384
entity: Some(Entity::Rule),
385385
name: Some("test-rule".into()),
@@ -392,7 +392,7 @@ mod test {
392392
}
393393

394394
fn create_util(temp: &Path) -> Result<()> {
395-
let project = ProjectConfig::setup(Some(temp.join("sgconfig.yml")))?;
395+
let project = ProjectConfig::setup(Some(temp.join("sgconfig.yml")), false)?;
396396
let arg = NewArg {
397397
entity: Some(Entity::Util),
398398
name: Some("test-utils".into()),

‎crates/cli/src/scan.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,8 @@ rule:
517517
.write_all("fn test() { Some(123) }".as_bytes())
518518
.unwrap();
519519
file.sync_all().unwrap();
520-
let project_config = ProjectConfig::setup(Some(dir.path().join("sgconfig.yml"))).unwrap();
520+
let project_config =
521+
ProjectConfig::setup(Some(dir.path().join("sgconfig.yml")), false).unwrap();
521522
let arg = default_scan_arg();
522523
assert!(run_with_config(arg, project_config).is_ok());
523524
}

‎crates/cli/src/utils/error_context.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub enum ErrorContext {
4141
UnrecognizableLanguage(String),
4242
LangInjection,
4343
CustomLanguage,
44+
CustomLanguageNotAllowed,
4445
// Run
4546
ParsePattern,
4647
ParseSelector,
@@ -92,7 +93,7 @@ impl ErrorContext {
9293
ProjectAlreadyExist | FileAlreadyExist(_) => 17,
9394
InsufficientCLIArgument(_) => 22,
9495
UnrecognizableLanguage(_) => 33,
95-
CustomLanguage => 79,
96+
CustomLanguage | CustomLanguageNotAllowed => 79,
9697
OpenEditor | StartLanguageServer => 126,
9798
// soft error
9899
PatternHasError | ExitInteractiveEditing => 0,
@@ -184,6 +185,11 @@ impl ErrorMessage {
184185
"The custom language library is not found or cannot be loaded.",
185186
CUSTOM_LANG_GUIDE,
186187
),
188+
CustomLanguageNotAllowed => Self::new(
189+
"Custom language libraries are not allowed",
190+
"Loading a custom language executes native code. Review the project and pass `--allow-custom-languages` to continue.",
191+
CUSTOM_LANG_GUIDE,
192+
),
187193
InvalidGlobalUtils => Self::new(
188194
"Error occurs when parsing global utility rules",
189195
"Please check the YAML rules inside the rule directory",
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
mod common;
2+
3+
use anyhow::Result;
4+
use assert_cmd::{Command, cargo_bin};
5+
use common::create_test_files;
6+
use predicates::str::contains;
7+
use std::fs;
8+
use std::path::PathBuf;
9+
10+
const CUSTOM_LANGUAGE_CONFIG: &str = r#"
11+
ruleDirs: []
12+
customLanguages:
13+
example:
14+
libraryPath: missing-library.so
15+
extensions: [example]
16+
"#;
17+
18+
#[test]
19+
fn custom_languages_require_explicit_opt_in() -> Result<()> {
20+
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
21+
22+
Command::new(cargo_bin!())
23+
.current_dir(dir.path())
24+
.arg("scan")
25+
.assert()
26+
.failure()
27+
.stderr(contains("Custom language libraries are not allowed"));
28+
Ok(())
29+
}
30+
31+
#[test]
32+
fn allow_custom_languages_attempts_to_load_the_library() -> Result<()> {
33+
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
34+
35+
Command::new(cargo_bin!())
36+
.current_dir(dir.path())
37+
.args(["scan", "--allow-custom-languages"])
38+
.assert()
39+
.failure()
40+
.stderr(contains("Cannot load custom language library"));
41+
Ok(())
42+
}
43+
44+
#[test]
45+
fn help_never_loads_custom_languages() -> Result<()> {
46+
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
47+
48+
Command::new(cargo_bin!())
49+
.current_dir(dir.path())
50+
.args(["--allow-custom-languages", "--help"])
51+
.assert()
52+
.success()
53+
.stdout(contains("Search and Rewrite code"));
54+
Ok(())
55+
}
56+
57+
#[test]
58+
fn completions_never_load_custom_languages() -> Result<()> {
59+
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
60+
61+
Command::new(cargo_bin!())
62+
.current_dir(dir.path())
63+
.args(["completions", "bash", "--allow-custom-languages"])
64+
.assert()
65+
.success()
66+
.stdout(contains("ast-grep"));
67+
Ok(())
68+
}
69+
70+
#[test]
71+
fn positional_path_does_not_enable_custom_languages() -> Result<()> {
72+
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
73+
74+
Command::new(cargo_bin!())
75+
.current_dir(dir.path())
76+
.args(["-p", "foo", "--", "--allow-custom-languages"])
77+
.assert()
78+
.failure()
79+
.stderr(contains("Custom language libraries are not allowed"));
80+
Ok(())
81+
}
82+
83+
#[test]
84+
fn denied_custom_languages_do_not_overwrite_project_config() -> Result<()> {
85+
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
86+
87+
Command::new(cargo_bin!())
88+
.current_dir(dir.path())
89+
.args(["new", "project", "--yes"])
90+
.assert()
91+
.failure()
92+
.stderr(contains("Custom language libraries are not allowed"));
93+
assert_eq!(
94+
fs::read_to_string(dir.path().join("sgconfig.yml"))?,
95+
CUSTOM_LANGUAGE_CONFIG
96+
);
97+
Ok(())
98+
}
99+
100+
#[test]
101+
fn allowed_custom_language_is_registered_before_cli_parsing() -> Result<()> {
102+
let library = if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
103+
"json-mac.so"
104+
} else if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
105+
"json-linux.so"
106+
} else {
107+
return Ok(());
108+
};
109+
let library = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
110+
.join("../../fixtures")
111+
.join(library);
112+
let config = format!(
113+
"customLanguages:\n myjson:\n libraryPath: {}\n languageSymbol: tree_sitter_json\n extensions: [myjson]\n",
114+
library.display()
115+
);
116+
let dir = create_test_files([("sgconfig.yml", config.as_str())])?;
117+
118+
Command::new(cargo_bin!())
119+
.current_dir(dir.path())
120+
.args([
121+
"run",
122+
"-p",
123+
"1",
124+
"-l",
125+
"myjson",
126+
"--allow-custom-languages",
127+
"--stdin",
128+
])
129+
.write_stdin(r#"{"key": 1}"#)
130+
.assert()
131+
.success();
132+
Ok(())
133+
}

0 commit comments

Comments
 (0)