Skip to content

Commit ec4c00a

Browse files
committed
Validate extension name in phylum extension new
Since `phylum extension new` is likely the most common way users will create new extensions, it should not be possible to creat invalid extensions with it. This patch validates the extension name during creation, refusing to create one with an invalid name. Closes #515.
1 parent 0077f4e commit ec4c00a

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

cli/src/commands/extensions/extension.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,7 @@ impl TryFrom<PathBuf> for Extension {
188188
));
189189
}
190190

191-
if !EXTENSION_NAME_RE.is_match(&manifest.name) {
192-
return Err(anyhow!(
193-
"{}: invalid extension name, must be lowercase alphanumeric, dash (-) or \
194-
underscore (_)",
195-
manifest.name
196-
));
197-
}
191+
validate_name(&manifest.name)?;
198192

199193
// TODO add further validation if necessary:
200194
// - Check that the entry point is a supported format (.wasm?)
@@ -203,6 +197,19 @@ impl TryFrom<PathBuf> for Extension {
203197
}
204198
}
205199

200+
/// Check extension name for validity.
201+
pub fn validate_name(name: &str) -> Result<(), anyhow::Error> {
202+
if EXTENSION_NAME_RE.is_match(&name) {
203+
Ok(())
204+
} else {
205+
Err(anyhow!(
206+
"{}: invalid extension name, must be lowercase alphanumeric, dash (-) or \
207+
underscore (_)",
208+
name
209+
))
210+
}
211+
}
212+
206213
// Construct and return the extension path: $XDG_DATA_HOME/phylum/extensions
207214
pub fn extensions_path() -> Result<PathBuf, anyhow::Error> {
208215
Ok(dirs::data_dir()?.join("phylum").join("extensions"))

cli/src/commands/extensions/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ pub async fn handle_create_extension(path: &str) -> CommandResult {
207207
.and_then(OsStr::to_str)
208208
.ok_or_else(|| anyhow!("Last segment in {path:?} is not a valid extension name"))?;
209209

210+
extension::validate_name(name)?;
211+
210212
// Create all missing directories.
211213
fs::create_dir_all(&extension_path)
212214
.with_context(|| format!("Unable to create all directories in {path:?}"))?;

cli/tests/extensions.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,38 @@ fn arg_access() {
264264
.stdout("[ \"--test\", \"-x\", \"a\" ]\n");
265265
}
266266

267+
// Extension creation works.
268+
#[test]
269+
fn create_extension() {
270+
let tempdir = TempDir::new().unwrap();
271+
Command::cargo_bin("phylum")
272+
.unwrap()
273+
.current_dir(tempdir.path())
274+
.env("XDG_DATA_HOME", tempdir.path())
275+
.arg("extension")
276+
.arg("new")
277+
.arg("my-ext")
278+
.assert()
279+
.success()
280+
.stderr(predicates::str::contains("✅ Extension created successfully"));
281+
}
282+
283+
// Extension creation with invalid name fails
284+
#[test]
285+
fn create_incorrect_name() {
286+
let tempdir = TempDir::new().unwrap();
287+
Command::cargo_bin("phylum")
288+
.unwrap()
289+
.current_dir(tempdir.path())
290+
.env("XDG_DATA_HOME", tempdir.path())
291+
.arg("extension")
292+
.arg("new")
293+
.arg("@@@")
294+
.assert()
295+
.failure()
296+
.stderr(predicates::str::contains("invalid extension name"));
297+
}
298+
267299
////////////////////////////////////////////////////////////////////////////////
268300
// Miscellaneous tests
269301
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)