Skip to content

Commit bce0128

Browse files
committed
feat(serverHandler): respond 403 status for permission error
1 parent 6e3ff2c commit bce0128

File tree

7 files changed

+62
-29
lines changed

7 files changed

+62
-29
lines changed

src/serverHandler/content.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func (h *handler) content(w http.ResponseWriter, r *http.Request, data *response
4343
header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
4444
header.Set("Last-Modified", item.ModTime().UTC().Format(http.TimeFormat))
4545
} else {
46-
w.WriteHeader(http.StatusInternalServerError)
46+
// take effect only if (!HasForbiddenError && !HasNotFoundError)
47+
data.HasInternalError = true
4748
}
49+
50+
writeHeader(w, r, data)
4851
}

src/serverHandler/json.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ type jsonItem struct {
1515
}
1616

1717
type jsonResponseData struct {
18+
ForbiddenError bool `json:"forbiddenError"`
19+
NotFoundError bool `json:"notFoundError"`
20+
InternalError bool `json:"internalError"`
21+
1822
Item *jsonItem `json:"item"`
1923
SubItems []*jsonItem `json:"subItems"`
2024
}
@@ -44,8 +48,11 @@ func getJsonData(data *responseData) *jsonResponseData {
4448
}
4549

4650
return &jsonResponseData{
47-
Item: item,
48-
SubItems: subItems,
51+
ForbiddenError: data.HasForbiddenError,
52+
NotFoundError: data.HasNotFoundError,
53+
InternalError: data.HasInternalError,
54+
Item: item,
55+
SubItems: subItems,
4956
}
5057
}
5158

@@ -54,13 +61,7 @@ func (h *handler) json(w http.ResponseWriter, r *http.Request, data *responseDat
5461
header.Set("Content-Type", "application/json; charset=utf-8")
5562
header.Set("Cache-Control", "public, max-age=0")
5663

57-
if data.HasInternalError {
58-
w.WriteHeader(http.StatusInternalServerError)
59-
} else if data.HasNotFoundError {
60-
w.WriteHeader(http.StatusNotFound)
61-
} else {
62-
w.WriteHeader(http.StatusOK)
63-
}
64+
writeHeader(w, r, data)
6465

6566
if needResponseBody(r.Method) {
6667
jsonData := getJsonData(data)

src/serverHandler/page.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@ func (h *handler) page(w http.ResponseWriter, r *http.Request, data *responseDat
77
header.Set("Content-Type", "text/html; charset=utf-8")
88
header.Set("Cache-Control", "public, max-age=0")
99

10-
if data.HasInternalError {
11-
w.WriteHeader(http.StatusInternalServerError)
12-
} else if data.HasNotFoundError {
13-
w.WriteHeader(http.StatusNotFound)
14-
} else {
15-
w.WriteHeader(http.StatusOK)
16-
}
10+
writeHeader(w, r, data)
1711

1812
if needResponseBody(r.Method) {
1913
updateSubsItemHtml(data.SubItems)

src/serverHandler/responseData.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ type responseData struct {
4040
rawReqPath string
4141
handlerReqPath string
4242

43-
errors []error
44-
HasNotFoundError bool
45-
HasInternalError bool
43+
errors []error
44+
HasForbiddenError bool
45+
HasNotFoundError bool
46+
HasInternalError bool
4647

4748
IsRoot bool
4849
Path string
@@ -266,6 +267,7 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
266267
rawReqPath := util.CleanUrlPath(requestUri)
267268
reqPath := util.CleanUrlPath(rawReqPath[len(h.urlPrefix):]) // strip url prefix path
268269
errs := []error{}
270+
forbidden := false
269271
notFound := false
270272
internalError := false
271273

@@ -287,8 +289,14 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
287289
file, item, _statErr := stat(reqFsPath, !h.emptyRoot)
288290
if _statErr != nil {
289291
errs = append(errs, _statErr)
290-
notFound = os.IsNotExist(_statErr)
291-
internalError = !notFound
292+
switch {
293+
case os.IsPermission(_statErr):
294+
forbidden = true
295+
case os.IsNotExist(_statErr):
296+
notFound = true
297+
default:
298+
internalError = true
299+
}
292300
}
293301

294302
itemName := getItemName(item, r)
@@ -321,9 +329,10 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
321329
rawReqPath: rawReqPath,
322330
handlerReqPath: reqPath,
323331

324-
errors: errs,
325-
HasNotFoundError: notFound,
326-
HasInternalError: internalError,
332+
errors: errs,
333+
HasForbiddenError: forbidden,
334+
HasNotFoundError: notFound,
335+
HasInternalError: internalError,
327336

328337
IsRoot: isRoot,
329338
Path: rawReqPath,

src/serverHandler/writeHeader.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package serverHandler
2+
3+
import "net/http"
4+
5+
func writeHeader(w http.ResponseWriter, r *http.Request, data *responseData) {
6+
switch {
7+
case data.HasForbiddenError:
8+
w.WriteHeader(http.StatusForbidden)
9+
case data.HasNotFoundError:
10+
w.WriteHeader(http.StatusNotFound)
11+
case data.HasInternalError:
12+
w.WriteHeader(http.StatusInternalServerError)
13+
default:
14+
w.WriteHeader(http.StatusOK)
15+
}
16+
}

src/tpl/page.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@
5656
{{end}}{{end}}
5757
</ul>
5858

59-
{{if .HasNotFoundError}}<div class="error">resource not found</div>{{end}}
60-
{{if .HasInternalError}}<div class="error">potential issue occurred</div>{{end}}
59+
{{if .HasForbiddenError}}
60+
<div class="error">403 resource is forbidden</div>
61+
{{else if .HasNotFoundError}}
62+
<div class="error">404 resource not found</div>
63+
{{else if .HasInternalError}}
64+
<div class="error">500 potential issue occurred</div>
65+
{{end}}
6166

6267
<script type="text/javascript" src="{{.RootRelPath}}/../assert/main.js"></script>
6368
</body>

src/tpl/page.html.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ const pageTplStr = `
6060
</li>
6161
{{end}}{{end}}
6262
</ul>
63-
{{if .HasNotFoundError}}<div class="error">resource not found</div>{{end}}
64-
{{if .HasInternalError}}<div class="error">potential issue occurred</div>{{end}}
63+
{{if .HasForbiddenError}}
64+
<div class="error">403 resource is forbidden</div>
65+
{{else if .HasNotFoundError}}
66+
<div class="error">404 resource not found</div>
67+
{{else if .HasInternalError}}
68+
<div class="error">500 potential issue occurred</div>
69+
{{end}}
6570
<script type="text/javascript" src="{{.RootRelPath}}?assert=main.js"></script>
6671
</body>
6772
</html>

0 commit comments

Comments
 (0)