Skip to content

Commit 2533449

Browse files
feat(cli): add custom language loading policy (#2971)
* feat(cli): add custom language loading policy * fix(cli): warn when custom languages are ignored * refactor(cli): remove deny custom language policy
1 parent 6602aeb commit 2533449

6 files changed

Lines changed: 128 additions & 55 deletions

File tree

‎crates/cli/src/config.rs‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@ use ast_grep_config::{
66
DeserializeEnv, GlobalRules, RuleCollection, RuleConfig, from_str, from_yaml_string,
77
};
88
use ast_grep_language::config_file_type;
9+
use clap::ValueEnum;
910
use ignore::WalkBuilder;
1011
use serde::{Deserialize, Serialize};
1112

1213
use std::collections::{HashMap, HashSet};
1314
use std::fs::read_to_string;
1415
use std::path::{Path, PathBuf};
1516

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+
1626
#[derive(Serialize, Deserialize, Clone)]
1727
#[serde(rename_all = "camelCase")]
1828
pub struct TestConfig {
@@ -95,12 +105,18 @@ impl ProjectConfig {
95105
/// returns a Result of Result.
96106
/// The inner Result is for configuration not found, or ProjectNotExist
97107
/// The outer Result is for definitely wrong config.
98-
pub fn setup(config_path: Option<PathBuf>, allow_custom_languages: bool) -> Result<Result<Self>> {
108+
pub fn setup(
109+
config_path: Option<PathBuf>,
110+
custom_languages: CustomLanguagePolicy,
111+
) -> Result<Result<Self>> {
99112
let Some((project_dir, mut sg_config)) = Self::discover_project(config_path)? else {
100113
return Ok(Err(anyhow::anyhow!(EC::ProjectNotExist)));
101114
};
102-
let outline_rules =
103-
custom_language_outline_rules(&project_dir, sg_config.custom_languages.as_ref());
115+
let outline_rules = if custom_languages == CustomLanguagePolicy::Allow {
116+
custom_language_outline_rules(&project_dir, sg_config.custom_languages.as_ref())
117+
} else {
118+
vec![]
119+
};
104120
let config = ProjectConfig {
105121
project_dir,
106122
rule_dirs: std::mem::take(&mut sg_config.rule_dirs),
@@ -109,7 +125,7 @@ impl ProjectConfig {
109125
util_dirs: sg_config.util_dirs.take(),
110126
};
111127
// sg_config will not use rule dirs and test configs anymore
112-
register_custom_language(&config.project_dir, sg_config, allow_custom_languages)?;
128+
register_custom_language(&config.project_dir, sg_config, custom_languages)?;
113129
Ok(Ok(config))
114130
}
115131
}
@@ -129,13 +145,20 @@ fn custom_language_outline_rules(
129145
fn register_custom_language(
130146
project_dir: &Path,
131147
sg_config: AstGrepConfig,
132-
allow_custom_languages: bool,
148+
policy: CustomLanguagePolicy,
133149
) -> Result<()> {
134150
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));
151+
match policy {
152+
CustomLanguagePolicy::Allow => {
153+
SgLang::register_custom_language(project_dir, custom_langs)?;
154+
}
155+
CustomLanguagePolicy::Ignore if !custom_langs.is_empty() => {
156+
eprintln!(
157+
"Warning: custom languages are ignored because native libraries can execute malicious code. Review the project before using `--custom-languages allow`."
158+
);
159+
}
160+
CustomLanguagePolicy::Ignore => {}
137161
}
138-
SgLang::register_custom_language(project_dir, custom_langs)?;
139162
}
140163
if let Some(globs) = sg_config.language_globs {
141164
SgLang::register_globs(globs)?;

‎crates/cli/src/lib.rs‎

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use clap::{Parser, Subcommand};
1515
use std::{path::PathBuf, process::ExitCode};
1616

1717
use completions::{CompletionsArg, run_shell_completion};
18-
use config::ProjectConfig;
18+
use config::{CustomLanguagePolicy, ProjectConfig};
1919
use lsp::{LspArg, run_language_server};
2020
use new::{NewArg, run_create_new};
2121
use outline::{OutlineArg, run_outline};
@@ -46,9 +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,
49+
/// Control how sgconfig.yml handles native custom language libraries.
50+
#[clap(long, global = true, value_enum, default_value_t)]
51+
custom_languages: CustomLanguagePolicy,
5252
}
5353

5454
#[derive(Subcommand)]
@@ -106,7 +106,7 @@ fn insert_default_run(args: &mut Vec<String>) {
106106
/// finding project and setup custom language configuration
107107
fn setup_project_is_possible(
108108
args: &[String],
109-
allow_custom_languages: bool,
109+
custom_languages: CustomLanguagePolicy,
110110
) -> Result<Result<ProjectConfig>> {
111111
let mut config = None;
112112
for i in 0..args.len() {
@@ -127,21 +127,48 @@ fn setup_project_is_possible(
127127
let config_file = (&args[i + 1]).into();
128128
config = Some(config_file);
129129
}
130-
ProjectConfig::setup(config, allow_custom_languages)
130+
ProjectConfig::setup(config, custom_languages)
131+
}
132+
133+
fn parse_custom_language_policy(args: &[String]) -> CustomLanguagePolicy {
134+
let mut policy = None;
135+
let mut i = 1;
136+
while i < args.len() {
137+
let arg = args[i].as_str();
138+
if arg == "--" {
139+
break;
140+
}
141+
let value = if arg == "--custom-languages" {
142+
i += 1;
143+
args.get(i).map(String::as_str)
144+
} else {
145+
arg.strip_prefix("--custom-languages=")
146+
};
147+
if let Some(value) = value {
148+
if policy.is_some() {
149+
return CustomLanguagePolicy::Ignore;
150+
}
151+
policy = match value {
152+
"allow" => Some(CustomLanguagePolicy::Allow),
153+
"ignore" => Some(CustomLanguagePolicy::Ignore),
154+
_ => return CustomLanguagePolicy::Ignore,
155+
};
156+
}
157+
i += 1;
158+
}
159+
policy.unwrap_or_default()
131160
}
132161

133162
// this wrapper function is for testing
134163
pub fn main_with_args(args: impl Iterator<Item = String>) -> Result<ExitCode> {
135164
let mut args: Vec<_> = args.collect();
136165
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");
166+
let custom_languages = parse_custom_language_policy(&args);
141167
// do not unwrap project before cmd parsing
142168
// sg help does not need a valid sgconfig.yml
143-
let project = setup_project_is_possible(&args, allow_custom_languages);
169+
let project = setup_project_is_possible(&args, custom_languages);
144170
let app = App::try_parse_from(args)?;
171+
debug_assert_eq!(custom_languages, app.custom_languages);
145172
let project = project?; // unwrap here to report invalid project
146173
match app.command {
147174
Commands::Run(arg) => run_with_pattern(arg, project),
@@ -203,6 +230,29 @@ mod test_cli {
203230
insert_default_run(&mut args);
204231
assert_eq!(args[1], "run");
205232
}
233+
234+
#[test]
235+
fn test_custom_language_policy() {
236+
let args = |args: &[&str]| {
237+
args
238+
.iter()
239+
.map(|arg| (*arg).to_string())
240+
.collect::<Vec<_>>()
241+
};
242+
assert_eq!(
243+
parse_custom_language_policy(&args(&["sg", "scan"])),
244+
CustomLanguagePolicy::Ignore
245+
);
246+
assert_eq!(
247+
parse_custom_language_policy(&args(&["sg", "scan", "--custom-languages=allow"])),
248+
CustomLanguagePolicy::Allow
249+
);
250+
assert_eq!(
251+
parse_custom_language_policy(&args(&["sg", "run", "--", "--custom-languages", "allow"])),
252+
CustomLanguagePolicy::Ignore
253+
);
254+
}
255+
206256
#[test]
207257
fn test_no_arg_run() {
208258
let ret = main_with_args(["sg".to_owned()].into_iter());
@@ -269,6 +319,10 @@ mod test_cli {
269319
#[test]
270320
fn test_scan() {
271321
ok("scan");
322+
ok("scan --custom-languages allow");
323+
ok("scan --custom-languages ignore");
324+
error("scan --custom-languages deny");
325+
error("scan --custom-languages invalid");
272326
ok("scan dir");
273327
ok("scan -r test-rule.yml dir");
274328
ok("scan -c test-rule.yml dir");

‎crates/cli/src/new.rs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ fn create_new_util(found: ProjectConfig, arg: NewArg) -> Result<ExitCode> {
362362
#[cfg(test)]
363363
mod test {
364364
use super::*;
365+
use crate::config::CustomLanguagePolicy;
365366
use ast_grep_language::SupportLang;
366367
use std::path::Path;
367368
use tempfile::TempDir;
@@ -379,7 +380,10 @@ mod test {
379380
}
380381

381382
fn create_rule(temp: &Path) -> Result<()> {
382-
let project = ProjectConfig::setup(Some(temp.join("sgconfig.yml")), false)?;
383+
let project = ProjectConfig::setup(
384+
Some(temp.join("sgconfig.yml")),
385+
CustomLanguagePolicy::Ignore,
386+
)?;
383387
let arg = NewArg {
384388
entity: Some(Entity::Rule),
385389
name: Some("test-rule".into()),
@@ -392,7 +396,10 @@ mod test {
392396
}
393397

394398
fn create_util(temp: &Path) -> Result<()> {
395-
let project = ProjectConfig::setup(Some(temp.join("sgconfig.yml")), false)?;
399+
let project = ProjectConfig::setup(
400+
Some(temp.join("sgconfig.yml")),
401+
CustomLanguagePolicy::Ignore,
402+
)?;
396403
let arg = NewArg {
397404
entity: Some(Entity::Util),
398405
name: Some("test-utils".into()),

‎crates/cli/src/scan.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ fn match_rule_on_file<T>(
437437
#[cfg(test)]
438438
mod test {
439439
use super::*;
440+
use crate::config::CustomLanguagePolicy;
440441
use crate::print::ColorArg;
441442
use std::fs::File;
442443
use std::io::Write;
@@ -517,8 +518,11 @@ rule:
517518
.write_all("fn test() { Some(123) }".as_bytes())
518519
.unwrap();
519520
file.sync_all().unwrap();
520-
let project_config =
521-
ProjectConfig::setup(Some(dir.path().join("sgconfig.yml")), false).unwrap();
521+
let project_config = ProjectConfig::setup(
522+
Some(dir.path().join("sgconfig.yml")),
523+
CustomLanguagePolicy::Ignore,
524+
)
525+
.unwrap();
522526
let arg = default_scan_arg();
523527
assert!(run_with_config(arg, project_config).is_ok());
524528
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ pub enum ErrorContext {
4141
UnrecognizableLanguage(String),
4242
LangInjection,
4343
CustomLanguage,
44-
CustomLanguageNotAllowed,
4544
// Run
4645
ParsePattern,
4746
ParseSelector,
@@ -93,7 +92,7 @@ impl ErrorContext {
9392
ProjectAlreadyExist | FileAlreadyExist(_) => 17,
9493
InsufficientCLIArgument(_) => 22,
9594
UnrecognizableLanguage(_) => 33,
96-
CustomLanguage | CustomLanguageNotAllowed => 79,
95+
CustomLanguage => 79,
9796
OpenEditor | StartLanguageServer => 126,
9897
// soft error
9998
PatternHasError | ExitInteractiveEditing => 0,
@@ -185,11 +184,6 @@ impl ErrorMessage {
185184
"The custom language library is not found or cannot be loaded.",
186185
CUSTOM_LANG_GUIDE,
187186
),
188-
CustomLanguageNotAllowed => Self::new(
189-
"Custom language libraries require explicit opt-in",
190-
"Custom language libraries in untrusted environments may run malicious code. Review the project before using `--allow-custom-languages`.",
191-
CUSTOM_LANG_GUIDE,
192-
),
193187
InvalidGlobalUtils => Self::new(
194188
"Error occurs when parsing global utility rules",
195189
"Please check the YAML rules inside the rule directory",

crates/cli/tests/custom_language_trust_test.rs renamed to crates/cli/tests/custom_language_policy_test.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ mod common;
33
use anyhow::Result;
44
use assert_cmd::{Command, cargo_bin};
55
use common::create_test_files;
6+
use predicates::prelude::PredicateBooleanExt;
67
use predicates::str::contains;
7-
use std::fs;
88
use std::path::PathBuf;
99

1010
const CUSTOM_LANGUAGE_CONFIG: &str = r#"
@@ -33,64 +33,54 @@ fn loadable_custom_language_config() -> Option<String> {
3333
}
3434

3535
#[test]
36-
fn custom_languages_require_explicit_opt_in() -> Result<()> {
36+
fn custom_languages_are_ignored_by_default() -> Result<()> {
3737
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
3838

3939
Command::new(cargo_bin!())
4040
.current_dir(dir.path())
4141
.arg("scan")
4242
.assert()
43-
.failure()
44-
.stderr(contains(
45-
"Custom language libraries require explicit opt-in",
46-
));
43+
.success()
44+
.stderr(contains("custom languages are ignored"));
4745
Ok(())
4846
}
4947

5048
#[test]
51-
fn allow_custom_languages_attempts_to_load_the_library() -> Result<()> {
49+
fn explicit_ignore_skips_custom_languages() -> Result<()> {
5250
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
5351

5452
Command::new(cargo_bin!())
5553
.current_dir(dir.path())
56-
.args(["scan", "--allow-custom-languages"])
54+
.args(["scan", "--custom-languages", "ignore"])
5755
.assert()
58-
.failure()
59-
.stderr(contains("Cannot load custom language library"));
56+
.success()
57+
.stderr(contains("custom languages are ignored"));
6058
Ok(())
6159
}
6260

6361
#[test]
64-
fn positional_path_does_not_enable_custom_languages() -> Result<()> {
62+
fn allow_custom_languages_attempts_to_load_the_library() -> Result<()> {
6563
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
6664

6765
Command::new(cargo_bin!())
6866
.current_dir(dir.path())
69-
.args(["-p", "foo", "--", "--allow-custom-languages"])
67+
.args(["scan", "--custom-languages", "allow"])
7068
.assert()
7169
.failure()
72-
.stderr(contains(
73-
"Custom language libraries require explicit opt-in",
74-
));
70+
.stderr(contains("Cannot load custom language library"));
7571
Ok(())
7672
}
7773

7874
#[test]
79-
fn denied_custom_languages_do_not_overwrite_project_config() -> Result<()> {
75+
fn positional_path_does_not_enable_custom_languages() -> Result<()> {
8076
let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?;
8177

8278
Command::new(cargo_bin!())
8379
.current_dir(dir.path())
84-
.args(["new", "project", "--yes"])
80+
.args(["-p", "foo", "--", "--custom-languages", "allow"])
8581
.assert()
8682
.failure()
87-
.stderr(contains(
88-
"Custom language libraries require explicit opt-in",
89-
));
90-
assert_eq!(
91-
fs::read_to_string(dir.path().join("sgconfig.yml"))?,
92-
CUSTOM_LANGUAGE_CONFIG
93-
);
83+
.stderr(contains("Cannot load custom language library").not());
9484
Ok(())
9585
}
9686

@@ -109,7 +99,8 @@ fn allowed_custom_language_is_registered_before_cli_parsing() -> Result<()> {
10999
"1",
110100
"-l",
111101
"myjson",
112-
"--allow-custom-languages",
102+
"--custom-languages",
103+
"allow",
113104
"--stdin",
114105
])
115106
.write_stdin(r#"{"key": 1}"#)

0 commit comments

Comments
 (0)