Skip to content

Commit dfbbc27

Browse files
fix: require opt-in for custom language libraries
1 parent 3703a9e commit dfbbc27

6 files changed

Lines changed: 147 additions & 10 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: 13 additions & 3 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)]
@@ -101,7 +104,10 @@ fn insert_default_run(args: &mut Vec<String>) {
101104
}
102105

103106
/// finding project and setup custom language configuration
104-
fn setup_project_is_possible(args: &[String]) -> Result<Result<ProjectConfig>> {
107+
fn setup_project_is_possible(
108+
args: &[String],
109+
allow_custom_languages: bool,
110+
) -> Result<Result<ProjectConfig>> {
105111
let mut config = None;
106112
for i in 0..args.len() {
107113
let arg = &args[i];
@@ -121,16 +127,20 @@ fn setup_project_is_possible(args: &[String]) -> Result<Result<ProjectConfig>> {
121127
let config_file = (&args[i + 1]).into();
122128
config = Some(config_file);
123129
}
124-
ProjectConfig::setup(config)
130+
ProjectConfig::setup(config, allow_custom_languages)
125131
}
126132

127133
// this wrapper function is for testing
128134
pub fn main_with_args(args: impl Iterator<Item = String>) -> Result<ExitCode> {
129135
let mut args: Vec<_> = args.collect();
130136
insert_default_run(&mut args);
137+
let allow_custom_languages = args
138+
.iter()
139+
.take_while(|arg| arg.as_str() != "--")
140+
.any(|arg| arg == "--allow-custom-languages");
131141
// do not unwrap project before cmd parsing
132142
// sg help does not need a valid sgconfig.yml
133-
let project = setup_project_is_possible(&args);
143+
let project = setup_project_is_possible(&args, allow_custom_languages);
134144
let app = App::try_parse_from(args)?;
135145
let project = project?; // unwrap here to report invalid project
136146
match app.command {

‎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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
fn loadable_custom_language_config() -> Option<String> {
19+
let library = if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
20+
"json-mac.so"
21+
} else if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
22+
"json-linux.so"
23+
} else {
24+
return None;
25+
};
26+
let library = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
27+
.join("../../fixtures")
28+
.join(library);
29+
Some(format!(
30+
"customLanguages:\n myjson:\n libraryPath: {}\n languageSymbol: tree_sitter_json\n extensions: [myjson]\n",
31+
library.display()
32+
))
33+
}
34+
35+
#[test]
36+
fn custom_languages_require_explicit_opt_in() -> Result<()> {
37+
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
38+
39+
Command::new(cargo_bin!())
40+
.current_dir(dir.path())
41+
.arg("scan")
42+
.assert()
43+
.failure()
44+
.stderr(contains("Custom language libraries are not allowed"));
45+
Ok(())
46+
}
47+
48+
#[test]
49+
fn allow_custom_languages_attempts_to_load_the_library() -> Result<()> {
50+
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
51+
52+
Command::new(cargo_bin!())
53+
.current_dir(dir.path())
54+
.args(["scan", "--allow-custom-languages"])
55+
.assert()
56+
.failure()
57+
.stderr(contains("Cannot load custom language library"));
58+
Ok(())
59+
}
60+
61+
#[test]
62+
fn positional_path_does_not_enable_custom_languages() -> Result<()> {
63+
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
64+
65+
Command::new(cargo_bin!())
66+
.current_dir(dir.path())
67+
.args(["-p", "foo", "--", "--allow-custom-languages"])
68+
.assert()
69+
.failure()
70+
.stderr(contains("Custom language libraries are not allowed"));
71+
Ok(())
72+
}
73+
74+
#[test]
75+
fn denied_custom_languages_do_not_overwrite_project_config() -> Result<()> {
76+
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
77+
78+
Command::new(cargo_bin!())
79+
.current_dir(dir.path())
80+
.args(["new", "project", "--yes"])
81+
.assert()
82+
.failure()
83+
.stderr(contains("Custom language libraries are not allowed"));
84+
assert_eq!(
85+
fs::read_to_string(dir.path().join("sgconfig.yml"))?,
86+
CUSTOM_LANGUAGE_CONFIG
87+
);
88+
Ok(())
89+
}
90+
91+
#[test]
92+
fn allowed_custom_language_is_registered_before_cli_parsing() -> Result<()> {
93+
let Some(config) = loadable_custom_language_config() else {
94+
return Ok(());
95+
};
96+
let dir = create_test_files([("sgconfig.yml", config.as_str())])?;
97+
98+
Command::new(cargo_bin!())
99+
.current_dir(dir.path())
100+
.args([
101+
"run",
102+
"-p",
103+
"1",
104+
"-l",
105+
"myjson",
106+
"--allow-custom-languages",
107+
"--stdin",
108+
])
109+
.write_stdin(r#"{"key": 1}"#)
110+
.assert()
111+
.success();
112+
Ok(())
113+
}

0 commit comments

Comments
 (0)