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
72bc6932
Commit
72bc6932
authored
Apr 19, 2016
by
W-Mark Kubacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
browse: Jail the root directory using http.Dir()
parent
a41e3d25
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
20 deletions
+34
-20
caddy/setup/browse.go
caddy/setup/browse.go
+11
-1
middleware/browse/browse.go
middleware/browse/browse.go
+19
-15
middleware/browse/browse_test.go
middleware/browse/browse_test.go
+4
-4
No files found.
caddy/setup/browse.go
View file @
72bc6932
...
@@ -3,6 +3,7 @@ package setup
...
@@ -3,6 +3,7 @@ package setup
import
(
import
(
"fmt"
"fmt"
"io/ioutil"
"io/ioutil"
"net/http"
"text/template"
"text/template"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware"
...
@@ -17,7 +18,6 @@ func Browse(c *Controller) (middleware.Middleware, error) {
...
@@ -17,7 +18,6 @@ func Browse(c *Controller) (middleware.Middleware, error) {
}
}
browse
:=
browse
.
Browse
{
browse
:=
browse
.
Browse
{
Root
:
c
.
Root
,
Configs
:
configs
,
Configs
:
configs
,
IgnoreIndexes
:
false
,
IgnoreIndexes
:
false
,
}
}
...
@@ -50,6 +50,16 @@ func browseParse(c *Controller) ([]browse.Config, error) {
...
@@ -50,6 +50,16 @@ func browseParse(c *Controller) ([]browse.Config, error) {
}
else
{
}
else
{
bc
.
PathScope
=
"/"
bc
.
PathScope
=
"/"
}
}
bc
.
Root
=
http
.
Dir
(
c
.
Root
)
theRoot
,
err
:=
bc
.
Root
.
Open
(
"/"
)
// catch a missing path early
if
err
!=
nil
{
return
configs
,
err
}
defer
theRoot
.
Close
()
_
,
err
=
theRoot
.
Readdir
(
-
1
)
if
err
!=
nil
{
return
configs
,
err
}
// Second argument would be the template file to use
// Second argument would be the template file to use
var
tplText
string
var
tplText
string
...
...
middleware/browse/browse.go
View file @
72bc6932
...
@@ -9,7 +9,6 @@ import (
...
@@ -9,7 +9,6 @@ import (
"net/url"
"net/url"
"os"
"os"
"path"
"path"
"path/filepath"
"sort"
"sort"
"strconv"
"strconv"
"strings"
"strings"
...
@@ -24,7 +23,6 @@ import (
...
@@ -24,7 +23,6 @@ import (
// directories in the given paths are specified.
// directories in the given paths are specified.
type
Browse
struct
{
type
Browse
struct
{
Next
middleware
.
Handler
Next
middleware
.
Handler
Root
string
Configs
[]
Config
Configs
[]
Config
IgnoreIndexes
bool
IgnoreIndexes
bool
}
}
...
@@ -32,6 +30,7 @@ type Browse struct {
...
@@ -32,6 +30,7 @@ type Browse struct {
// Config is a configuration for browsing in a particular path.
// Config is a configuration for browsing in a particular path.
type
Config
struct
{
type
Config
struct
{
PathScope
string
PathScope
string
Root
http
.
FileSystem
Variables
interface
{}
Variables
interface
{}
Template
*
template
.
Template
Template
*
template
.
Template
}
}
...
@@ -247,8 +246,7 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
...
@@ -247,8 +246,7 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
inScope
:
inScope
:
// Browse works on existing directories; delegate everything else
// Browse works on existing directories; delegate everything else
requestedFilepath
:=
filepath
.
Join
(
b
.
Root
,
r
.
URL
.
Path
)
requestedFilepath
,
err
:=
bc
.
Root
.
Open
(
r
.
URL
.
Path
)
info
,
err
:=
os
.
Stat
(
requestedFilepath
)
if
err
!=
nil
{
if
err
!=
nil
{
switch
{
switch
{
case
os
.
IsPermission
(
err
)
:
case
os
.
IsPermission
(
err
)
:
...
@@ -259,6 +257,19 @@ inScope:
...
@@ -259,6 +257,19 @@ inScope:
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
}
}
}
}
defer
requestedFilepath
.
Close
()
info
,
err
:=
requestedFilepath
.
Stat
()
if
err
!=
nil
{
switch
{
case
os
.
IsPermission
(
err
)
:
return
http
.
StatusForbidden
,
err
case
os
.
IsExist
(
err
)
:
return
http
.
StatusGone
,
err
default
:
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
}
}
if
!
info
.
IsDir
()
{
if
!
info
.
IsDir
()
{
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
}
}
...
@@ -283,15 +294,8 @@ inScope:
...
@@ -283,15 +294,8 @@ inScope:
return
b
.
ServeListing
(
w
,
r
,
requestedFilepath
,
bc
)
return
b
.
ServeListing
(
w
,
r
,
requestedFilepath
,
bc
)
}
}
func
(
b
Browse
)
loadDirectoryContents
(
requestedFilepath
,
urlPath
string
)
(
*
Listing
,
bool
,
error
)
{
func
(
b
Browse
)
loadDirectoryContents
(
requestedFilepath
http
.
File
,
urlPath
string
)
(
*
Listing
,
bool
,
error
)
{
// Load directory contents
files
,
err
:=
requestedFilepath
.
Readdir
(
-
1
)
file
,
err
:=
os
.
Open
(
requestedFilepath
)
if
err
!=
nil
{
return
nil
,
false
,
err
}
defer
file
.
Close
()
files
,
err
:=
file
.
Readdir
(
-
1
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
false
,
err
return
nil
,
false
,
err
}
}
...
@@ -351,7 +355,7 @@ func (b Browse) handleSortOrder(w http.ResponseWriter, r *http.Request, scope st
...
@@ -351,7 +355,7 @@ func (b Browse) handleSortOrder(w http.ResponseWriter, r *http.Request, scope st
}
}
// ServeListing returns a formatted view of 'requestedFilepath' contents'.
// ServeListing returns a formatted view of 'requestedFilepath' contents'.
func
(
b
Browse
)
ServeListing
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
requestedFilepath
string
,
bc
*
Config
)
(
int
,
error
)
{
func
(
b
Browse
)
ServeListing
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
requestedFilepath
http
.
File
,
bc
*
Config
)
(
int
,
error
)
{
listing
,
containsIndex
,
err
:=
b
.
loadDirectoryContents
(
requestedFilepath
,
r
.
URL
.
Path
)
listing
,
containsIndex
,
err
:=
b
.
loadDirectoryContents
(
requestedFilepath
,
r
.
URL
.
Path
)
if
err
!=
nil
{
if
err
!=
nil
{
switch
{
switch
{
...
@@ -367,7 +371,7 @@ func (b Browse) ServeListing(w http.ResponseWriter, r *http.Request, requestedFi
...
@@ -367,7 +371,7 @@ func (b Browse) ServeListing(w http.ResponseWriter, r *http.Request, requestedFi
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
}
}
listing
.
Context
=
middleware
.
Context
{
listing
.
Context
=
middleware
.
Context
{
Root
:
http
.
Dir
(
b
.
Root
)
,
Root
:
bc
.
Root
,
Req
:
r
,
Req
:
r
,
URL
:
r
.
URL
,
URL
:
r
.
URL
,
}
}
...
...
middleware/browse/browse_test.go
View file @
72bc6932
...
@@ -114,10 +114,10 @@ func TestBrowseHTTPMethods(t *testing.T) {
...
@@ -114,10 +114,10 @@ func TestBrowseHTTPMethods(t *testing.T) {
Next
:
middleware
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
Next
:
middleware
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
return
http
.
StatusTeapot
,
nil
// not t.Fatalf, or we will not see what other methods yield
return
http
.
StatusTeapot
,
nil
// not t.Fatalf, or we will not see what other methods yield
}),
}),
Root
:
"./testdata"
,
Configs
:
[]
Config
{
Configs
:
[]
Config
{
{
{
PathScope
:
"/photos"
,
PathScope
:
"/photos"
,
Root
:
http
.
Dir
(
"./testdata"
),
Template
:
tmpl
,
Template
:
tmpl
,
},
},
},
},
...
@@ -153,10 +153,10 @@ func TestBrowseTemplate(t *testing.T) {
...
@@ -153,10 +153,10 @@ func TestBrowseTemplate(t *testing.T) {
t
.
Fatalf
(
"Next shouldn't be called"
)
t
.
Fatalf
(
"Next shouldn't be called"
)
return
0
,
nil
return
0
,
nil
}),
}),
Root
:
"./testdata"
,
Configs
:
[]
Config
{
Configs
:
[]
Config
{
{
{
PathScope
:
"/photos"
,
PathScope
:
"/photos"
,
Root
:
http
.
Dir
(
"./testdata"
),
Template
:
tmpl
,
Template
:
tmpl
,
},
},
},
},
...
@@ -208,16 +208,16 @@ func TestBrowseJson(t *testing.T) {
...
@@ -208,16 +208,16 @@ func TestBrowseJson(t *testing.T) {
t
.
Fatalf
(
"Next shouldn't be called"
)
t
.
Fatalf
(
"Next shouldn't be called"
)
return
0
,
nil
return
0
,
nil
}),
}),
Root
:
"./testdata"
,
Configs
:
[]
Config
{
Configs
:
[]
Config
{
{
{
PathScope
:
"/photos/"
,
PathScope
:
"/photos/"
,
Root
:
http
.
Dir
(
"./testdata"
),
},
},
},
},
}
}
//Getting the listing from the ./testdata/photos, the listing returned will be used to validate test results
//Getting the listing from the ./testdata/photos, the listing returned will be used to validate test results
testDataPath
:=
b
.
Root
+
"/photos/"
testDataPath
:=
filepath
.
Join
(
"./testdata"
,
"photos"
)
file
,
err
:=
os
.
Open
(
testDataPath
)
file
,
err
:=
os
.
Open
(
testDataPath
)
if
err
!=
nil
{
if
err
!=
nil
{
if
os
.
IsPermission
(
err
)
{
if
os
.
IsPermission
(
err
)
{
...
...
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