]> Cypherpunks repositories - dsc.git/commitdiff
Fix directories import
authorSergey Matveev <stargrave@stargrave.org>
Tue, 23 Dec 2025 09:52:09 +0000 (12:52 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Tue, 23 Dec 2025 09:52:09 +0000 (12:52 +0300)
dsc
t/export.t
t/import-path-validation.t

diff --git a/dsc b/dsc
index 16542386921eaf93a66eb1a1ec41463186519832496d25d85280bce722e19309..b8ee44e60282511fb2cc193c4df2a7a63b3d7a00aea23a4cd2c32b58df1d0e29 100755 (executable)
--- 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
index 19061763356e77a526db8c3842662e08e59f3ec43bd0b4ca732a65e2656c1a9b..f6d582e75feb710bbd2395ed578d594bf00a8c17c1c897f086ae8c3299f5b1e0 100755 (executable)
@@ -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 <<EOF
 +net/bar/addr
 +net/bar/addr/2001:DB8::1:2345
 +net/bar/addr/2001:DB8::2:2345
++net/baz
 +net/foo
 +net/foo/addr
 +net/foo/addr/2001:DB8::1:1234
@@ -68,6 +71,8 @@ cat >expected <<EOF
 EOF
 test_expect_success "cmp" "test_cmp out expected"
 test_expect_success "commit" "dsc commit"
+test_expect_success "diff" "dsc diff >out"
+test_expect_success "diff is empty" "! [ -s out ]"
 test_expect_success "export" "dsc export >out"
 cat >expected <<EOF
 -- .dirs --
@@ -76,6 +81,7 @@ net/bar
 net/bar/addr
 net/bar/addr/2001:DB8::1:2345
 net/bar/addr/2001:DB8::2:2345
+net/baz
 net/foo
 net/foo/addr
 net/foo/addr/2001:DB8::1:1234
@@ -97,4 +103,10 @@ world
 EOF
 test_expect_success "cmp" "test_cmp out expected"
 
+rm -r $DSC_STASH
+test_expect_success "import" "dsc import <out"
+test_expect_success "diff" "dsc diff >out"
+cat out
+test_expect_success "diff is empty" "! [ -s out ]"
+
 test_done
index 7551c876c9abda835715e43f1490ef7245d13adc8cbe75c9f4a6a9ee5eecb9b0..4f25209624876f72c2e7df37296b39a989a626f20a0de714615a0b8415c7fa1a 100755 (executable)
@@ -22,4 +22,21 @@ test_expect_success "import rel" "! dsc import <in >out 2>&1"
 test_expect_success "import rel msg" \
     '[ "$(cat out)" = "relative paths are forbidden" ]'
 
+cat >in <<EOF
+-- .dirs --
+/abs/path
+gotcha
+EOF
+test_expect_success "import abs" "! dsc import <in >out 2>&1"
+test_expect_success "import abs msg" \
+    '[ "$(cat out)" = "absolute paths are forbidden" ]'
+
+cat >in <<EOF
+-- .dirs --
+path/../rel
+EOF
+test_expect_success "import rel" "! dsc import <in >out 2>&1"
+test_expect_success "import rel msg" \
+    '[ "$(cat out)" = "relative paths are forbidden" ]'
+
 test_done