-
Notifications
You must be signed in to change notification settings - Fork 0
more sanitization and tests #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| .DS_Store | ||
| *.log | ||
| main | ||
| release | ||
| testCoverage* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| ) | ||
|
|
||
| // deny serves a JSON response for disallowed requests. | ||
| func deny(w http.ResponseWriter, code int, reason string, r *Request) { | ||
| writeJSON(w, code, errorJSON(reason)) | ||
| } | ||
|
|
||
| // errorJSON returns an error string map containing the string. | ||
| func errorJSON(s string) map[string]string { | ||
| return map[string]string{ | ||
| "error": s, | ||
| } | ||
| } | ||
|
|
||
| // writeJSON serves a JSON response with data. | ||
| func writeJSON(w http.ResponseWriter, code int, data interface{}) { | ||
| w.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
| w.WriteHeader(code) | ||
| err := json.NewEncoder(w).Encode(data) | ||
| if err != nil { | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| _ = json.NewEncoder(w).Encode(errorJSON(err.Error())) | ||
| return | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package handlers | ||
|
|
||
| import "net/http" | ||
|
|
||
| // getRequestParameter returns a parameter from the request | ||
| // URL or a form value. | ||
| func getRequestParameter( | ||
| r *http.Request, pathLen int, fieldName string) string { | ||
| if pathLen > len(r.URL.Path) { | ||
| return "" | ||
| } | ||
| p := r.URL.Path[pathLen:] | ||
| if p != "" { | ||
| return p | ||
| } | ||
| if queryValue := r.URL.Query().Get(fieldName); queryValue != "" { | ||
| return queryValue | ||
| } | ||
| return r.FormValue(fieldName) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/url" | ||
| "testing" | ||
| ) | ||
|
|
||
| // TestGetRequestParameter validates the parameter value | ||
| // is read from the URL or form. | ||
| func TestGetRequestParameter(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| request *http.Request | ||
| pathLen int | ||
| field string | ||
| want string | ||
| }{ | ||
| { | ||
| name: "URL parameter", | ||
| request: &http.Request{ | ||
| URL: &url.URL{Path: "/download/id1"}, | ||
| }, | ||
| pathLen: 10, | ||
| field: "", | ||
| want: "id1", | ||
| }, | ||
| { | ||
| name: "Query parameter", | ||
| request: &http.Request{ | ||
| URL: &url.URL{ | ||
| Path: "/download/", | ||
| RawQuery: "field=id2", | ||
| }, | ||
| }, | ||
| pathLen: 10, | ||
| field: "field", | ||
| want: "id2", | ||
| }, | ||
| { | ||
| name: "Form parameter", | ||
| request: &http.Request{ | ||
| Body: http.NoBody, | ||
| Form: url.Values{"field": {"id3"}}, | ||
| URL: &url.URL{Path: "/download/"}, | ||
| }, | ||
| pathLen: 10, | ||
| field: "field", | ||
| want: "id3", | ||
| }, | ||
| { | ||
| name: "No parameter", | ||
| request: &http.Request{ | ||
| Body: http.NoBody, | ||
| URL: &url.URL{Path: "/download/"}, | ||
| }, | ||
| pathLen: 10, | ||
| field: "", | ||
| want: "", | ||
| }, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| result := getRequestParameter(test.request, test.pathLen, test.field) | ||
| if result != test.want { | ||
| t.Fatalf("%s: expect '%q'; got '%q'", test.name, test.want, result) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "slices" | ||
| "time" | ||
|
|
||
| "github.com/drduh/gone/auth" | ||
| "github.com/drduh/gone/util" | ||
| ) | ||
|
|
||
| const autoTheme = "auto" | ||
|
|
||
| // getDefaultTheme returns a default theme, based on | ||
| // the current time if set to automatically theme. | ||
| func getDefaultTheme(theme string) string { | ||
| if theme != autoTheme { | ||
| return theme | ||
| } | ||
| if util.IsDaytime() { | ||
| return "light" | ||
| } | ||
| return "dark" | ||
| } | ||
|
|
||
| // getTheme returns the CSS theme based on cookie preference, | ||
| // setting the cookie value if none exists, or is invalid. | ||
| func getTheme(w http.ResponseWriter, r *http.Request, | ||
| defaultTheme, id string, t time.Duration, themes []string) string { | ||
| formContent := r.FormValue(formFieldTheme) | ||
| if formContent != "" { | ||
| theme := formContent | ||
| if !slices.Contains(themes, theme) { | ||
| theme = getDefaultTheme(autoTheme) | ||
| } | ||
| http.SetCookie(w, auth.NewCookie(theme, id, t)) | ||
| return theme | ||
| } | ||
| return auth.GetCookie(w, r, defaultTheme, id, t) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.