Skip to content

Commit ecde394

Browse files
authored
Merge pull request #982 from owasp-noir/bug/issue-980
Fix file extension removal bug when using dot base path (-b .)
2 parents 5b4f29a + d3d535d commit ecde394

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
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: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ def remove_start_slash(input_path : String) : String
33
end
44

55
def get_relative_path(base_path : String, path : String) : String
6-
# Ensure base_path ends with slash for consistent substitution
7-
base = base_path.ends_with?("/") ? base_path : "#{base_path}/"
6+
# First, determine the path relative to the base_path, without other normalization.
7+
unstripped_path = if base_path == "."
8+
# When base_path is ".", the path is already relative.
9+
# This avoids an issue where `.sub(".", "")` would remove the dot from file extensions.
10+
path
11+
else
12+
# For other base paths, remove the base path prefix.
13+
base = base_path.ends_with?("/") ? base_path : "#{base_path}/"
14+
path
15+
.sub(base, "")
16+
.sub(base_path, "") # Fallback if base doesn't end with /
17+
end
818

9-
# Remove base path and normalize
10-
relative_path = path
11-
.sub(base, "")
12-
.sub(base_path, "") # Fallback if base doesn't end with /
13-
.sub("./", "")
19+
# Then, normalize the resulting path.
20+
relative_path = unstripped_path
21+
.sub(/^\.\//, "") # Remove leading "./" only at the start
1422
.sub("//", "/")
1523

1624
remove_start_slash(relative_path)

0 commit comments

Comments
 (0)