Skip to content

Commit

Permalink
Fix crash on client timeout #125, minor formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Forceu committed Feb 7, 2024
1 parent f322013 commit 9de7618
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
3 changes: 1 addition & 2 deletions internal/configuration/setup/Setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,8 @@ func getFormValueBool(formObjects *[]jsonFormObject, key string) (bool, error) {
valueHidden, err2 := getFormValueString(formObjects, key+".unchecked")
if err2 != nil {
return false, err
} else {
value = valueHidden
}
value = valueHidden
}
if value == "0" || value == "false" {
return false, nil
Expand Down
23 changes: 18 additions & 5 deletions internal/helper/OS.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bufio"
"errors"
"golang.org/x/term"
"log"
"os"
"syscall"
)
Expand Down Expand Up @@ -56,11 +57,23 @@ func ReadPassword() string {
return ReadLine()
}

// Check panics if error is not nil
func Check(e error) {
if e != nil {
panic(e)
// Check panics if err is not nil
func Check(err error) {
if err != nil {
panic(err)
}
}

// CheckIgnoreTimeout panics if err is not nil and not a timeout
func CheckIgnoreTimeout(err error) {
if err == nil {
return
}
if os.IsTimeout(err) {
log.Println(err)
return
}
Check(err)
}

// IsInArray returns true if value is in array
Expand All @@ -86,4 +99,4 @@ func GetFileSize(file *os.File) (int64, error) {
var ErrPathDoesNotExist = errors.New("path does not exist")

// ErrPathIsNotDir is raised if the requested path is not a directory
var ErrPathIsNotDir = errors.New("path is not a directoryt")
var ErrPathIsNotDir = errors.New("path is not a directory")
44 changes: 23 additions & 21 deletions internal/webserver/Webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func serveDownloadWasm(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/wasm")
file, err := wasmDownloadFile.ReadFile("web/main.wasm")
helper.Check(err)
w.Write(file)
_, _ = w.Write(file)
}

// Handling of /e2e.wasm
Expand All @@ -211,7 +211,7 @@ func serveE2EWasm(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/wasm")
file, err := wasmE2EFile.ReadFile("web/e2e.wasm")
helper.Check(err)
w.Write(file)
_, _ = w.Write(file)
}

// Handling of /logout
Expand All @@ -222,7 +222,7 @@ func doLogout(w http.ResponseWriter, r *http.Request) {
// Handling of /index and redirecting to globalConfig.RedirectUrl
func showIndex(w http.ResponseWriter, r *http.Request) {
err := templateFolder.ExecuteTemplate(w, "index", genericView{RedirectUrl: configuration.Get().RedirectUrl, PublicName: configuration.Get().PublicName})
helper.Check(err)
helper.CheckIgnoreTimeout(err)
}

// Handling of /error
Expand All @@ -239,13 +239,13 @@ func showError(w http.ResponseWriter, r *http.Request) {
errorReason = wrongCipher
}
err := templateFolder.ExecuteTemplate(w, "error", genericView{ErrorId: errorReason, PublicName: configuration.Get().PublicName})
helper.Check(err)
helper.CheckIgnoreTimeout(err)
}

// Handling of /error-auth
func showErrorAuth(w http.ResponseWriter, r *http.Request) {
err := templateFolder.ExecuteTemplate(w, "error_auth", genericView{PublicName: configuration.Get().PublicName})
helper.Check(err)
helper.CheckIgnoreTimeout(err)
}

// Handling of /error-oauth
Expand All @@ -256,20 +256,20 @@ func showErrorIntOAuth(w http.ResponseWriter, r *http.Request) {
view.ErrorProvidedMessage = r.URL.Query().Get("error_description")
view.ErrorGenericMessage = r.URL.Query().Get("error_generic")
err := templateFolder.ExecuteTemplate(w, "error_int_oauth", view)
helper.Check(err)
helper.CheckIgnoreTimeout(err)
}

// Handling of /forgotpw
func forgotPassword(w http.ResponseWriter, r *http.Request) {
err := templateFolder.ExecuteTemplate(w, "forgotpw", genericView{PublicName: configuration.Get().PublicName})
helper.Check(err)
helper.CheckIgnoreTimeout(err)
}

// Handling of /api
// If user is authenticated, this menu lists all uploads and enables uploading new files
func showApiAdmin(w http.ResponseWriter, r *http.Request) {
err := templateFolder.ExecuteTemplate(w, "api", (&UploadView{}).convertGlobalConfig(ViewAPI))
helper.Check(err)
helper.CheckIgnoreTimeout(err)
}

// Handling of /apiNew
Expand Down Expand Up @@ -310,7 +310,11 @@ func showLogin(w http.ResponseWriter, r *http.Request) {
return
}
err := r.ParseForm()
helper.Check(err)
if err != nil {
fmt.Println("Invalid form data sent to server for /login")
fmt.Println(err)
return
}
user := r.Form.Get("username")
pw := r.Form.Get("password")
failedLogin := false
Expand All @@ -333,7 +337,7 @@ func showLogin(w http.ResponseWriter, r *http.Request) {
IsAdminView: false,
PublicName: configuration.Get().PublicName,
})
helper.Check(err)
helper.CheckIgnoreTimeout(err)
}

// LoginView contains variables for the login template
Expand Down Expand Up @@ -389,7 +393,7 @@ func showDownload(w http.ResponseWriter, r *http.Request) {
}
view.IsPasswordView = true
err := templateFolder.ExecuteTemplate(w, "download_password", view)
helper.Check(err)
helper.CheckIgnoreTimeout(err)
return
}
if !isValidPwCookie(r, file) {
Expand All @@ -400,7 +404,7 @@ func showDownload(w http.ResponseWriter, r *http.Request) {
}
}
err := templateFolder.ExecuteTemplate(w, "download", view)
helper.Check(err)
helper.CheckIgnoreTimeout(err)
}

// Handling of /hotlink/
Expand All @@ -411,8 +415,7 @@ func showHotlink(w http.ResponseWriter, r *http.Request) {
file, ok := storage.GetFileByHotlink(hotlinkId)
if !ok {
w.Header().Set("Content-Type", "image/svg+xml")
_, err := w.Write(imageExpiredPicture)
helper.Check(err)
_, _ = w.Write(imageExpiredPicture)
return
}
storage.ServeFile(file, w, r, false)
Expand Down Expand Up @@ -464,9 +467,9 @@ func storeE2eInfo(w http.ResponseWriter, r *http.Request) {

func getE2eInfo(w http.ResponseWriter) {
info := database.GetEnd2EndInfo()
bytes, err := json.Marshal(info)
bytesE2e, err := json.Marshal(info)
helper.Check(err)
_, _ = w.Write(bytes)
_, _ = w.Write(bytesE2e)
}

// Handling of /delete
Expand Down Expand Up @@ -505,14 +508,14 @@ func showAdminMenu(w http.ResponseWriter, r *http.Request) {
}
}
err := templateFolder.ExecuteTemplate(w, "admin", (&UploadView{}).convertGlobalConfig(ViewMain))
helper.Check(err)
helper.CheckIgnoreTimeout(err)
}

// Handling of /logs
// If user is authenticated, this menu shows the stored logs
func showLogs(w http.ResponseWriter, r *http.Request) {
err := templateFolder.ExecuteTemplate(w, "logs", (&UploadView{}).convertGlobalConfig(ViewLogs))
helper.Check(err)
helper.CheckIgnoreTimeout(err)
}

func showE2ESetup(w http.ResponseWriter, r *http.Request) {
Expand All @@ -522,7 +525,7 @@ func showE2ESetup(w http.ResponseWriter, r *http.Request) {
}
e2einfo := database.GetEnd2EndInfo()
err := templateFolder.ExecuteTemplate(w, "e2esetup", e2ESetupView{HasBeenSetup: e2einfo.HasBeenSetUp(), PublicName: configuration.Get().PublicName})
helper.Check(err)
helper.CheckIgnoreTimeout(err)
}

// DownloadView contains parameters for the download template
Expand Down Expand Up @@ -703,8 +706,7 @@ func requireLogin(next http.HandlerFunc, isUpload bool) http.HandlerFunc {
return
}
if isUpload {
_, err := io.WriteString(w, "{\"Result\":\"error\",\"ErrorMessage\":\"Not authenticated\"}")
helper.Check(err)
_, _ = io.WriteString(w, "{\"Result\":\"error\",\"ErrorMessage\":\"Not authenticated\"}")
return
}
redirect(w, "login")
Expand Down
4 changes: 2 additions & 2 deletions internal/webserver/authentication/Authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ func TestCheckOauthUser(t *testing.T) {
test.IsEqualString(t, redirectsToSite(output), "error-auth")

authSettings.OAuthUserScope = "invalidScope"
output, err = getOuthUserOutput(t, info)
_, err = getOuthUserOutput(t, info)
test.IsNotNil(t, err)

newClaims := testInfo{Output: []byte("{invalid")}
info.ClaimsSent = newClaims
output, err = getOuthUserOutput(t, info)
_, err = getOuthUserOutput(t, info)
test.IsNotNil(t, err)
}

Expand Down

0 comments on commit 9de7618

Please sign in to comment.