From b1d5b959f0fb46804332faca90f5a6b980f4da6b74b20335a9da14bad5e5b978 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Tue, 23 Dec 2025 12:52:09 +0300 Subject: [PATCH] Fix directories import --- dsc | 27 ++++++++++++++++----------- t/export.t | 12 ++++++++++++ t/import-path-validation.t | 17 +++++++++++++++++ 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/dsc b/dsc index 1654238..b8ee44e 100755 --- a/dsc +++ b/dsc @@ -130,6 +130,19 @@ proc txtar-fn {line} { return "" } +proc path-sanitize {fn} { + if {[string index $fn 0] == "/"} { + puts stderr "absolute paths are forbidden" + exit 1 + } + foreach e [file split $fn] { + if {$e == ".."} { + puts stderr "relative paths are forbidden" + exit 1 + } + } +} + proc assure-all-list-params-exist {opt {offset 0}} { global Stash set pth [find-opt-schema $opt] @@ -360,23 +373,15 @@ switch [lindex $argv 0] { while {[gets stdin line] >= 0} { set fn [txtar-fn $line] if {$fn == ""} { - file mkdir $Stash/$fn + path-sanitize $line + file mkdir $Stash/$line } else { break } } } proc openfh {fn} { - if {[string index $fn 0] == "/"} { - puts stderr "absolute paths are forbidden" - exit 1 - } - foreach e [file split $fn] { - if {$e == ".."} { - puts stderr "relative paths are forbidden" - exit 1 - } - } + path-sanitize $fn set bin no if {[string range $fn [expr {[string length $fn]-7}] end] == ":base64"} { set bin yes diff --git a/t/export.t b/t/export.t index 1906176..f6d582e 100755 --- a/t/export.t +++ b/t/export.t @@ -25,6 +25,8 @@ test_expect_success "2001:db8::1:2345/80 add" "dsc set $addr/prefixlen 80" addr=net/bar/addr/2001:db8::2:2345 test_expect_success "2001:db8::2:2345 add" "dsc add $addr >got" +test_expect_success "baz net" "dsc add net/baz" + test_expect_success "note" 'dsc set sys/note "hello world "' @@ -41,6 +43,7 @@ cat >expected <expected <out" +test_expect_success "diff is empty" "! [ -s out ]" test_expect_success "export" "dsc export >out" cat >expected <out" +cat out +test_expect_success "diff is empty" "! [ -s out ]" + test_done diff --git a/t/import-path-validation.t b/t/import-path-validation.t index 7551c87..4f25209 100755 --- a/t/import-path-validation.t +++ b/t/import-path-validation.t @@ -22,4 +22,21 @@ test_expect_success "import rel" "! dsc import out 2>&1" test_expect_success "import rel msg" \ '[ "$(cat out)" = "relative paths are forbidden" ]' +cat >in <out 2>&1" +test_expect_success "import abs msg" \ + '[ "$(cat out)" = "absolute paths are forbidden" ]' + +cat >in <out 2>&1" +test_expect_success "import rel msg" \ + '[ "$(cat out)" = "relative paths are forbidden" ]' + test_done -- 2.52.0