Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
008ad398
Commit
008ad398
authored
Mar 12, 2016
by
Abiola Ibrahim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Hopefully, this is the final nail on the coffin.
parent
e92a911e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
30 deletions
+25
-30
middleware/fileserver.go
middleware/fileserver.go
+10
-20
middleware/fileserver_test.go
middleware/fileserver_test.go
+15
-10
No files found.
middleware/fileserver.go
View file @
008ad398
...
...
@@ -122,7 +122,7 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st
}
// If file is on hide list.
if
fh
.
isHidden
(
d
.
Name
()
)
{
if
fh
.
isHidden
(
d
)
{
return
http
.
StatusNotFound
,
nil
}
...
...
@@ -133,30 +133,20 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st
return
http
.
StatusOK
,
nil
}
// isHidden checks if file with name is on hide list.
func
(
fh
fileHandler
)
isHidden
(
name
string
)
bool
{
// Clean up on Windows.
// Remove trailing dots and trim whitespaces.
if
runtimeGoos
==
"windows"
{
name
=
strings
.
TrimSpace
(
name
)
for
strings
.
HasSuffix
(
name
,
"."
)
{
name
=
name
[
:
len
(
name
)
-
1
]
name
=
strings
.
TrimSpace
(
name
)
}
}
// isHidden checks if file with FileInfo d is on hide list.
func
(
fh
fileHandler
)
isHidden
(
d
os
.
FileInfo
)
bool
{
// If the file is supposed to be hidden, return a 404
// (TODO: If the slice gets large, a set may be faster)
for
_
,
hiddenPath
:=
range
fh
.
hide
{
// Case-insensitive file systems may have loaded "CaddyFile" when
// we think we got "Caddyfile", which poses a security risk if we
// aren't careful here: case-insensitive comparison is required!
// TODO: This matches file NAME only, regardless of path. In other
// words, trying to serve another file with the same name as the
// active config file will result in a 404 when it shouldn't.
if
strings
.
EqualFold
(
name
,
filepath
.
Base
(
hiddenPath
))
{
// Check if the served file is exactly the hidden file.
if
hFile
,
err
:=
fh
.
root
.
Open
(
hiddenPath
);
err
==
nil
{
fs
,
_
:=
hFile
.
Stat
()
hFile
.
Close
()
if
os
.
SameFile
(
d
,
fs
)
{
return
true
}
}
}
return
false
}
...
...
middleware/fileserver_test.go
View file @
008ad398
...
...
@@ -35,7 +35,7 @@ func TestServeHTTP(t *testing.T) {
beforeServeHTTPTest
(
t
)
defer
afterServeHTTPTest
(
t
)
fileserver
:=
FileServer
(
http
.
Dir
(
testDir
),
[]
string
{
"hidden.html"
})
fileserver
:=
FileServer
(
http
.
Dir
(
testDir
),
[]
string
{
"
dir/
hidden.html"
})
movedPermanently
:=
"Moved Permanently"
...
...
@@ -84,54 +84,59 @@ func TestServeHTTP(t *testing.T) {
expectedStatus
:
http
.
StatusMovedPermanently
,
expectedBodyContent
:
movedPermanently
,
},
// Test
6
- access file with trailing slash
// Test
7
- access file with trailing slash
{
url
:
"https://foo/file1.html/"
,
expectedStatus
:
http
.
StatusMovedPermanently
,
expectedBodyContent
:
movedPermanently
,
},
// Test
7
- access not existing path
// Test
8
- access not existing path
{
url
:
"https://foo/not_existing"
,
expectedStatus
:
http
.
StatusNotFound
,
},
// Test
8
- access a file, marked as hidden
// Test
9
- access a file, marked as hidden
{
url
:
"https://foo/dir/hidden.html"
,
expectedStatus
:
http
.
StatusNotFound
,
},
// Test
9
- access a index file directly
// Test
10
- access a index file directly
{
url
:
"https://foo/dirwithindex/index.html"
,
expectedStatus
:
http
.
StatusOK
,
expectedBodyContent
:
testFiles
[
filepath
.
Join
(
"dirwithindex"
,
"index.html"
)],
},
// Test 1
0
- send a request with query params
// Test 1
1
- send a request with query params
{
url
:
"https://foo/dir?param1=val"
,
expectedStatus
:
http
.
StatusMovedPermanently
,
expectedBodyContent
:
movedPermanently
,
},
// Test 1
1
- attempt to bypass hidden file
// Test 1
2
- attempt to bypass hidden file
{
url
:
"https://foo/dir/hidden.html%20"
,
expectedStatus
:
http
.
StatusNotFound
,
},
// Test 1
2
- attempt to bypass hidden file
// Test 1
3
- attempt to bypass hidden file
{
url
:
"https://foo/dir/hidden.html."
,
expectedStatus
:
http
.
StatusNotFound
,
},
// Test 1
3
- attempt to bypass hidden file
// Test 1
4
- attempt to bypass hidden file
{
url
:
"https://foo/dir/hidden.html.%20"
,
expectedStatus
:
http
.
StatusNotFound
,
},
// Test 1
4
- attempt to bypass hidden file
// Test 1
5
- attempt to bypass hidden file
{
url
:
"https://foo/dir/hidden.html%20."
,
expectedStatus
:
http
.
StatusNotFound
,
},
// Test 16 - serve another file with same name as hidden file.
{
url
:
"https://foo/hidden.html"
,
expectedStatus
:
http
.
StatusNotFound
,
},
}
for
i
,
test
:=
range
tests
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment