Skip to content

Commit 33b8911

Browse files
committed
Fix file extension removal bug when using dot base path (-b .)
1 parent 5b4f29a commit 33b8911

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

spec/unit_test/utils/utils_spec.cr

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ describe "get_relative_path" do
2222
it "end with /" do
2323
get_relative_path("/abcd/", "1.cr").should eq("1.cr")
2424
end
25+
26+
# Bug fix: https://github.com/owasp-noir/noir/issues/980
27+
# When base_path is ".", file extensions were being removed
28+
# because .sub(".", "") matched the "." in ".php"
29+
it "dot base path with file extension" do
30+
get_relative_path(".", "./test.php").should eq("test.php")
31+
end
32+
33+
it "dot base path with nested path and extension" do
34+
get_relative_path(".", "./vulnerabilities/api/echo.php").should eq("vulnerabilities/api/echo.php")
35+
end
36+
37+
it "dot base path without leading ./" do
38+
get_relative_path(".", "vulnerabilities/api/echo.php").should eq("vulnerabilities/api/echo.php")
39+
end
40+
41+
it "normal base path preserves extension" do
42+
get_relative_path("/home/user/DVWA", "/home/user/DVWA/vulnerabilities/api/echo.php").should eq("vulnerabilities/api/echo.php")
43+
end
2544
end
2645

2746
describe "get_symbol" do

src/utils/utils.cr

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,24 @@ def remove_start_slash(input_path : String) : String
33
end
44

55
def get_relative_path(base_path : String, path : String) : String
6+
# Handle special case where base_path is "." (current directory)
7+
# Without this, .sub(".", "") would remove the first "." found anywhere in the path
8+
# (e.g., removing the "." from ".php" extension)
9+
if base_path == "."
10+
return path
11+
.sub(/^\.\//, "") # Remove leading "./" only at the start
12+
.sub("//", "/")
13+
.lstrip('/')
14+
end
15+
616
# Ensure base_path ends with slash for consistent substitution
717
base = base_path.ends_with?("/") ? base_path : "#{base_path}/"
818

919
# Remove base path and normalize
1020
relative_path = path
1121
.sub(base, "")
12-
.sub(base_path, "") # Fallback if base doesn't end with /
13-
.sub("./", "")
22+
.sub(base_path, "") # Fallback if base doesn't end with /
23+
.sub(/^\.\//, "") # Remove leading "./" only at the start
1424
.sub("//", "/")
1525

1626
remove_start_slash(relative_path)

0 commit comments

Comments
 (0)