Skip to content

Commit 7395401

Browse files
committed
Disallow parent path component
1 parent 8e2d49d commit 7395401

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

schema/v2/path.schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"description": "A *Path* is a string with a relative file path that identifies a file in the Distribution. The path **MUST** be specified with Unix conventions.",
66
"type": "string",
77
"minLength": 2,
8+
"format": "path",
89
"pattern": "^(?:[^/\\\\]|\\\\\\\\)(?:[^\\\\]|\\\\\\\\)+$",
910
"$comment": "https://regex101.com/r/d49AVj",
1011
"examples": [".git", "src/pair.c", "doc/pair.md"]

tests/common/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fs::{self, File};
2+
use std::path::{Component, Path};
23
use std::{collections::HashMap, error::Error};
34

45
use boon::{Compiler, Schemas};
@@ -90,6 +91,10 @@ pub fn id_for(version: u8, schema: &str) -> String {
9091
pub fn new_compiler(dir: &str) -> Result<Compiler, Box<dyn Error>> {
9192
let mut compiler = Compiler::new();
9293
compiler.enable_format_assertions();
94+
compiler.register_format(boon::Format {
95+
name: "path",
96+
func: is_path,
97+
});
9398
let paths = fs::read_dir(dir)?;
9499
for path in paths {
95100
let path = path?.path();
@@ -110,6 +115,24 @@ pub fn new_compiler(dir: &str) -> Result<Compiler, Box<dyn Error>> {
110115
Ok(compiler)
111116
}
112117

118+
fn is_path(v: &Value) -> Result<(), Box<dyn Error>> {
119+
let Value::String(s) = v else {
120+
return Ok(()); // applicable only on strings
121+
};
122+
123+
let path = Path::new(s);
124+
for c in path.components() {
125+
match c {
126+
Component::ParentDir => Err("parent dir")?,
127+
Component::Prefix(_) => Err("windows path")?,
128+
Component::RootDir => Err("absolute path")?,
129+
_ => (),
130+
};
131+
}
132+
133+
Ok(())
134+
}
135+
113136
pub fn test_term_schema(mut compiler: Compiler, version: u8) -> Result<(), Box<dyn Error>> {
114137
let mut schemas = Schemas::new();
115138
let id = id_for(version, "term");

tests/v2_schema_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ fn test_v2_path() -> Result<(), Box<dyn Error>> {
8383
json!("\\foo.md"),
8484
json!("this\\and\\that.txt"),
8585
json!("/absolute/path"),
86-
// ECMA supports look-ahead, but the regex crate does not.
86+
// Enforced only by custom format for now.
8787
// https://github.com/santhosh-tekuri/boon/issues/19
88-
// json!("../outside/path"),
89-
// json!("thing/../other"),
88+
json!("../outside/path"),
89+
json!("thing/../other"),
9090
] {
9191
if schemas.validate(&invalid_path, idx).is_ok() {
9292
panic!("{} unexpectedly passed!", invalid_path)

0 commit comments

Comments
 (0)