From 12bb1179b241d21cc131546fd993dadf7e950884 Mon Sep 17 00:00:00 2001 From: milarin Date: Thu, 16 Feb 2023 15:15:41 +0100 Subject: [PATCH] accept all paramters as base64 --- main.go | 14 +++++++++----- utils.go | 10 ++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 4075d0a..9db6cb2 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,6 @@ import ( "io" "math/rand" "net/http" - "net/url" "os" "os/exec" "path/filepath" @@ -82,7 +81,7 @@ func GetAllHandler(w http.ResponseWriter, r *http.Request) { } func GetFileHandler(w http.ResponseWriter, r *http.Request) { - fileName, err := url.QueryUnescape(mux.Vars(r)["file"]) + fileName, err := DecodeBase64String(mux.Vars(r)["file"]) if err != nil { InternalServerError(w, err) return @@ -92,7 +91,7 @@ func GetFileHandler(w http.ResponseWriter, r *http.Request) { } func GetFileHashHandler(w http.ResponseWriter, r *http.Request) { - fileName, err := url.QueryUnescape(mux.Vars(r)["file"]) + fileName, err := DecodeBase64String(mux.Vars(r)["file"]) if err != nil { InternalServerError(w, err) return @@ -121,7 +120,11 @@ func GetFileHashHandler(w http.ResponseWriter, r *http.Request) { } func GetPlaylistHandler(w http.ResponseWriter, r *http.Request) { - playlistName := mux.Vars(r)["playlist"] + playlistName, err := DecodeBase64String(mux.Vars(r)["playlist"]) + if err != nil { + InternalServerError(w, err) + return + } songs, err := GetSongsByPlaylist(playlistName) if err != nil { @@ -177,7 +180,8 @@ func GetAllFilesHandler(w http.ResponseWriter, r *http.Request) { func GetEncodeFileHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) format := vars["format"] - fileName, err := url.QueryUnescape(vars["file"]) + + fileName, err := DecodeBase64String(vars["file"]) if err != nil { InternalServerError(w, err) return diff --git a/utils.go b/utils.go index 2339512..101c16b 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,7 @@ package main import ( + "encoding/base64" "log" "net/http" "os" @@ -56,3 +57,12 @@ func IsHidden(entry os.DirEntry) bool { func FsEntry2Name(entry os.DirEntry) string { return entry.Name() } + +func DecodeBase64String(str string) (string, error) { + data, err := base64.StdEncoding.DecodeString(str) + return string(data), err +} + +func EncodeBase64String(str string) string { + return base64.StdEncoding.EncodeToString([]byte(str)) +}