The Great Linting Fixes, Drone configuration fix (again) and flatfile changes.

Great linting fixes has been applied, thanks to golangci-lint for
extensive reporting.

Fixed Drone configuration to use array for when-branch statement in
Docker plugin.

Flatfile storage from now will write files with 0600 permission for
greater security.
This commit is contained in:
2021-11-20 22:19:58 +05:00
parent 218e0bf667
commit 2b44a60ee7
14 changed files with 154 additions and 156 deletions

View File

@@ -35,12 +35,12 @@ import (
)
// Index of this site.
func indexGet(ec echo.Context) error {
func indexGet(ectx echo.Context) error {
// We should check if database connection available.
dbConn := c.Database.GetDatabaseConnection()
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
// nolint:wrapcheck
return ec.Redirect(http.StatusFound, "/database_not_available")
return ectx.Redirect(http.StatusFound, "/database_not_available")
}
// Generate list of available languages to highlight.
@@ -54,8 +54,8 @@ func indexGet(ec echo.Context) error {
// Captcha.
captchaString := captcha.NewCaptcha()
htmlData := templater.GetTemplate(ec, "index.html", map[string]string{"lexers": availableLexersSelectOpts, "captchaString": captchaString})
htmlData := templater.GetTemplate(ectx, "index.html", map[string]string{"lexers": availableLexersSelectOpts, "captchaString": captchaString})
// nolint:wrapcheck
return ec.HTML(http.StatusOK, htmlData)
return ectx.HTML(http.StatusOK, htmlData)
}

View File

@@ -72,8 +72,8 @@ func pasteGetData(pasteID int, timestamp int64, cookieValue string) (*structs.Pa
// GET for "/paste/PASTE_ID" and "/paste/PASTE_ID/TIMESTAMP" (private pastes).
// Web interface version.
func pasteGETWebInterface(ec echo.Context) error {
pasteIDRaw := ec.Param("id")
func pasteGETWebInterface(ectx echo.Context) error {
pasteIDRaw := ectx.Param("id")
// We already get numbers from string, so we will not check strconv.Atoi()
// error.
pasteID, _ := strconv.Atoi(regexInts.FindAllString(pasteIDRaw, 1)[0])
@@ -85,16 +85,16 @@ func pasteGETWebInterface(ec echo.Context) error {
// will show 404 Not Found error and spam about that in logs.
var timestamp int64
tsProvidedStr := ec.Param("timestamp")
tsProvidedStr := ectx.Param("timestamp")
if tsProvidedStr != "" {
tsProvided, err := strconv.ParseInt(tsProvidedStr, 10, 64)
if err != nil {
c.Logger.Error().Err(err).Int("paste ID", pasteID).Int64("provided timestamp", tsProvided).Msg("Invalid timestamp provided for getting private paste")
errtpl := templater.GetErrorTemplate(ec, "Paste #"+pasteIDStr+" not found")
errtpl := templater.GetErrorTemplate(ectx, "Paste #"+pasteIDStr+" not found")
// nolint:wrapcheck
return ec.HTML(http.StatusBadRequest, errtpl)
return ectx.HTML(http.StatusBadRequest, errtpl)
}
timestamp = tsProvided
@@ -104,7 +104,7 @@ func pasteGETWebInterface(ec echo.Context) error {
// for private pastes.
var cookieValue string
cookie, err1 := ec.Cookie("PASTE-" + pasteIDStr)
cookie, err1 := ectx.Cookie("PASTE-" + pasteIDStr)
if err1 == nil {
cookieValue = cookie.Value
}
@@ -113,10 +113,10 @@ func pasteGETWebInterface(ec echo.Context) error {
// For these cases we should return 404 Not Found page.
if err == pasteExpired || err == pasteNotFound || err == pasteTimestampInvalid {
errtpl := templater.GetErrorTemplate(ec, "Paste #"+pasteIDRaw+" not found")
errtpl := templater.GetErrorTemplate(ectx, "Paste #"+pasteIDRaw+" not found")
// nolint:wrapcheck
return ec.HTML(http.StatusNotFound, errtpl)
return ectx.HTML(http.StatusNotFound, errtpl)
}
// If passed cookie value was invalid - go to paste authorization
@@ -125,7 +125,7 @@ func pasteGETWebInterface(ec echo.Context) error {
c.Logger.Info().Int("paste ID", pasteID).Msg("Invalid cookie, redirecting to auth page")
// nolint:wrapcheck
return ec.Redirect(http.StatusMovedPermanently, "/paste/"+pasteIDStr+"/"+ec.Param("timestamp")+"/verify")
return ectx.Redirect(http.StatusMovedPermanently, "/paste/"+pasteIDStr+"/"+ectx.Param("timestamp")+"/verify")
}
// Format paste data map.
@@ -180,16 +180,16 @@ func pasteGETWebInterface(ec echo.Context) error {
pasteData["pastedata"] = buf.String()
// Get template and format it.
pasteHTML := templater.GetTemplate(ec, "paste.html", pasteData)
pasteHTML := templater.GetTemplate(ectx, "paste.html", pasteData)
// nolint:wrapcheck
return ec.HTML(http.StatusOK, pasteHTML)
return ectx.HTML(http.StatusOK, pasteHTML)
}
// GET for "/paste/PASTE_ID/TIMESTAMP/verify" - a password verify page.
func pastePasswordedVerifyGet(ec echo.Context) error {
pasteIDRaw := ec.Param("id")
timestampRaw := ec.Param("timestamp")
func pastePasswordedVerifyGet(ectx echo.Context) error {
pasteIDRaw := ectx.Param("id")
timestampRaw := ectx.Param("timestamp")
// We already get numbers from string, so we will not check strconv.Atoi()
// error.
pasteID, _ := strconv.Atoi(regexInts.FindAllString(pasteIDRaw, 1)[0])
@@ -199,14 +199,14 @@ func pastePasswordedVerifyGet(ec echo.Context) error {
if err1 != nil {
c.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste data")
errtpl := templater.GetErrorTemplate(ec, "Paste #"+pasteIDRaw+" not found")
errtpl := templater.GetErrorTemplate(ectx, "Paste #"+pasteIDRaw+" not found")
// nolint:wrapcheck
return ec.HTML(http.StatusBadRequest, errtpl)
return ectx.HTML(http.StatusBadRequest, errtpl)
}
// Check for auth cookie. If present - redirect to paste.
cookie, err := ec.Cookie("PASTE-" + strconv.Itoa(pasteID))
cookie, err := ectx.Cookie("PASTE-" + strconv.Itoa(pasteID))
if err == nil {
// No cookie, redirect to auth page.
c.Logger.Debug().Msg("Paste cookie found, checking it...")
@@ -218,7 +218,7 @@ func pastePasswordedVerifyGet(ec echo.Context) error {
c.Logger.Info().Msg("Valid cookie, redirecting to paste page...")
// nolint:wrapcheck
return ec.Redirect(http.StatusMovedPermanently, "/paste/"+pasteIDRaw+"/"+ec.Param("timestamp"))
return ectx.Redirect(http.StatusMovedPermanently, "/paste/"+pasteIDRaw+"/"+ectx.Param("timestamp"))
}
c.Logger.Debug().Msg("Invalid cookie, showing auth page")
@@ -229,24 +229,24 @@ func pastePasswordedVerifyGet(ec echo.Context) error {
htmlData["pasteID"] = strconv.Itoa(pasteID)
htmlData["pasteTimestamp"] = timestampRaw
verifyHTML := templater.GetTemplate(ec, "passworded_paste_verify.html", htmlData)
verifyHTML := templater.GetTemplate(ectx, "passworded_paste_verify.html", htmlData)
// nolint:wrapcheck
return ec.HTML(http.StatusOK, verifyHTML)
return ectx.HTML(http.StatusOK, verifyHTML)
}
// POST for "/paste/PASTE_ID/TIMESTAMP/verify" - a password verify page.
func pastePasswordedVerifyPost(ec echo.Context) error {
func pastePasswordedVerifyPost(ectx echo.Context) error {
// We should check if database connection available.
dbConn := c.Database.GetDatabaseConnection()
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
// nolint:wrapcheck
return ec.Redirect(http.StatusFound, "/database_not_available")
return ectx.Redirect(http.StatusFound, "/database_not_available")
}
pasteIDRaw := ec.Param("id")
timestampRaw := ec.Param("timestamp")
pasteIDRaw := ectx.Param("id")
timestampRaw := ectx.Param("timestamp")
// We already get numbers from string, so we will not check strconv.Atoi()
// error.
pasteID, _ := strconv.Atoi(regexInts.FindAllString(pasteIDRaw, 1)[0])
@@ -256,20 +256,20 @@ func pastePasswordedVerifyPost(ec echo.Context) error {
paste, err1 := c.Database.GetPaste(pasteID)
if err1 != nil {
c.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste")
errtpl := templater.GetErrorTemplate(ec, "Paste #"+strconv.Itoa(pasteID)+" not found")
errtpl := templater.GetErrorTemplate(ectx, "Paste #"+strconv.Itoa(pasteID)+" not found")
// nolint:wrapcheck
return ec.HTML(http.StatusBadRequest, errtpl)
return ectx.HTML(http.StatusBadRequest, errtpl)
}
params, err2 := ec.FormParams()
params, err2 := ectx.FormParams()
if err2 != nil {
c.Logger.Debug().Msg("No form parameters passed")
errtpl := templater.GetErrorTemplate(ec, "Paste #"+strconv.Itoa(pasteID)+" not found")
errtpl := templater.GetErrorTemplate(ectx, "Paste #"+strconv.Itoa(pasteID)+" not found")
// nolint:wrapcheck
return ec.HTML(http.StatusBadRequest, errtpl)
return ectx.HTML(http.StatusBadRequest, errtpl)
}
if paste.VerifyPassword(params["paste-password"][0]) {
@@ -279,29 +279,29 @@ func pastePasswordedVerifyPost(ec echo.Context) error {
cookie.Name = "PASTE-" + strconv.Itoa(pasteID)
cookie.Value = paste.GenerateCryptedCookieValue()
cookie.Expires = time.Now().Add(24 * time.Hour)
ec.SetCookie(cookie)
ectx.SetCookie(cookie)
// nolint:wrapcheck
return ec.Redirect(http.StatusFound, "/paste/"+strconv.Itoa(pasteID)+"/"+timestampRaw)
return ectx.Redirect(http.StatusFound, "/paste/"+strconv.Itoa(pasteID)+"/"+timestampRaw)
}
errtpl := templater.GetErrorTemplate(ec, "Invalid password. Please, try again.")
errtpl := templater.GetErrorTemplate(ectx, "Invalid password. Please, try again.")
// nolint:wrapcheck
return ec.HTML(http.StatusBadRequest, errtpl)
return ectx.HTML(http.StatusBadRequest, errtpl)
}
// GET for "/pastes/:id/raw", raw paste output.
// Web interface version.
func pasteRawGETWebInterface(ec echo.Context) error {
func pasteRawGETWebInterface(ectx echo.Context) error {
// We should check if database connection available.
dbConn := c.Database.GetDatabaseConnection()
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
// nolint:wrapcheck
return ec.Redirect(http.StatusFound, "/database_not_available/raw")
return ectx.Redirect(http.StatusFound, "/database_not_available/raw")
}
pasteIDRaw := ec.Param("id")
pasteIDRaw := ectx.Param("id")
// We already get numbers from string, so we will not check strconv.Atoi()
// error.
pasteID, _ := strconv.Atoi(regexInts.FindAllString(pasteIDRaw, 1)[0])
@@ -313,26 +313,26 @@ func pasteRawGETWebInterface(ec echo.Context) error {
c.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste from database")
// nolint:wrapcheck
return ec.HTML(http.StatusBadRequest, "Paste #"+pasteIDRaw+" does not exist.")
return ectx.HTML(http.StatusBadRequest, "Paste #"+pasteIDRaw+" does not exist.")
}
if paste.IsExpired() {
c.Logger.Error().Int("paste ID", pasteID).Msg("Paste is expired")
// nolint:wrapcheck
return ec.HTML(http.StatusBadRequest, "Paste #"+pasteIDRaw+" does not exist.")
return ectx.HTML(http.StatusBadRequest, "Paste #"+pasteIDRaw+" does not exist.")
}
// Check if we have a private paste and it's parameters are correct.
if paste.Private {
tsProvidedStr := ec.Param("timestamp")
tsProvidedStr := ectx.Param("timestamp")
tsProvided, err2 := strconv.ParseInt(tsProvidedStr, 10, 64)
if err2 != nil {
c.Logger.Error().Err(err2).Int("paste ID", pasteID).Str("provided timestamp", tsProvidedStr).Msg("Invalid timestamp provided for getting private paste")
// nolint:wrapcheck
return ec.String(http.StatusBadRequest, "Paste #"+pasteIDRaw+" not found")
return ectx.String(http.StatusBadRequest, "Paste #"+pasteIDRaw+" not found")
}
pasteTS := paste.CreatedAt.Unix()
@@ -340,7 +340,7 @@ func pasteRawGETWebInterface(ec echo.Context) error {
c.Logger.Error().Int("paste ID", pasteID).Int64("provided timestamp", tsProvided).Int64("paste timestamp", pasteTS).Msg("Incorrect timestamp provided for private paste")
// nolint:wrapcheck
return ec.String(http.StatusBadRequest, "Paste #"+pasteIDRaw+" not found")
return ectx.String(http.StatusBadRequest, "Paste #"+pasteIDRaw+" not found")
}
}
@@ -349,9 +349,9 @@ func pasteRawGETWebInterface(ec echo.Context) error {
// Return error for now.
if paste.Password != "" {
c.Logger.Error().Int("paste ID", pasteID).Msg("Cannot render paste as raw: passworded paste. Patches welcome!")
return ec.String(http.StatusBadRequest, "Paste #"+pasteIDRaw+" not found")
return ectx.String(http.StatusBadRequest, "Paste #"+pasteIDRaw+" not found")
}
// nolint:wrapcheck
return ec.String(http.StatusOK, paste.Data)
return ectx.String(http.StatusOK, paste.Data)
}

View File

@@ -20,22 +20,22 @@ const KeepPastesForever = "forever"
// POST for "/paste/" which will create new paste and redirect to
// "/pastes/CREATED_PASTE_ID". This handler will do all the job for
// requests comes from browsers via web interface.
func pastePOSTWebInterface(ec echo.Context) error {
func pastePOSTWebInterface(ectx echo.Context) error {
// We should check if database connection available.
dbConn := c.Database.GetDatabaseConnection()
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
// nolint:wrapcheck
return ec.Redirect(http.StatusFound, "/database_not_available")
return ectx.Redirect(http.StatusFound, "/database_not_available")
}
params, err := ec.FormParams()
params, err := ectx.FormParams()
if err != nil {
c.Logger.Error().Msg("Passed paste form is empty")
errtpl := templater.GetErrorTemplate(ec, "Cannot create empty paste")
errtpl := templater.GetErrorTemplate(ectx, "Cannot create empty paste")
// nolint:wrapcheck
return ec.HTML(http.StatusBadRequest, errtpl)
return ectx.HTML(http.StatusBadRequest, errtpl)
}
c.Logger.Debug().Msgf("Received parameters: %+v", params)
@@ -44,29 +44,29 @@ func pastePOSTWebInterface(ec echo.Context) error {
if len(params["paste-contents"][0]) == 0 {
c.Logger.Debug().Msg("Empty paste submitted, ignoring")
errtpl := templater.GetErrorTemplate(ec, "Empty pastes aren't allowed.")
errtpl := templater.GetErrorTemplate(ectx, "Empty pastes aren't allowed.")
// nolint:wrapcheck
return ec.HTML(http.StatusBadRequest, errtpl)
return ectx.HTML(http.StatusBadRequest, errtpl)
}
if !strings.ContainsAny(params["paste-keep-for"][0], "Mmhd") && params["paste-keep-for"][0] != KeepPastesForever {
c.Logger.Debug().Str("field value", params["paste-keep-for"][0]).Msg("'Keep paste for' field have invalid value")
errtpl := templater.GetErrorTemplate(ec, "Invalid 'Paste should be available for' parameter passed. Please do not try to hack us ;).")
errtpl := templater.GetErrorTemplate(ectx, "Invalid 'Paste should be available for' parameter passed. Please do not try to hack us ;).")
// nolint:wrapcheck
return ec.HTML(http.StatusBadRequest, errtpl)
return ectx.HTML(http.StatusBadRequest, errtpl)
}
// Verify captcha.
if !captcha.Verify(params["paste-captcha-id"][0], params["paste-captcha-solution"][0]) {
c.Logger.Debug().Str("captcha ID", params["paste-captcha-id"][0]).Str("captcha solution", params["paste-captcha-solution"][0]).Msg("Invalid captcha solution")
errtpl := templater.GetErrorTemplate(ec, "Invalid captcha solution.")
errtpl := templater.GetErrorTemplate(ectx, "Invalid captcha solution.")
// nolint:wrapcheck
return ec.HTML(http.StatusBadRequest, errtpl)
return ectx.HTML(http.StatusBadRequest, errtpl)
}
// nolint:exhaustivestruct
@@ -101,10 +101,10 @@ func pastePOSTWebInterface(ec echo.Context) error {
} else {
c.Logger.Debug().Err(err).Msg("Failed to parse 'Keep for' integer")
errtpl := templater.GetErrorTemplate(ec, "Invalid 'Paste should be available for' parameter passed. Please do not try to hack us ;).")
errtpl := templater.GetErrorTemplate(ectx, "Invalid 'Paste should be available for' parameter passed. Please do not try to hack us ;).")
// nolint:wrapcheck
return ec.HTML(http.StatusBadRequest, errtpl)
return ectx.HTML(http.StatusBadRequest, errtpl)
}
}
@@ -138,25 +138,25 @@ func pastePOSTWebInterface(ec echo.Context) error {
_ = paste.CreatePassword(pastePassword[0])
}
id, err2 := c.Database.SavePaste(paste)
pasteID, err2 := c.Database.SavePaste(paste)
if err2 != nil {
c.Logger.Error().Err(err2).Msg("Failed to save paste")
errtpl := templater.GetErrorTemplate(ec, "Failed to save paste. Please, try again later.")
errtpl := templater.GetErrorTemplate(ectx, "Failed to save paste. Please, try again later.")
// nolint:wrapcheck
return ec.HTML(http.StatusBadRequest, errtpl)
return ectx.HTML(http.StatusBadRequest, errtpl)
}
newPasteIDAsString := strconv.FormatInt(id, 10)
newPasteIDAsString := strconv.FormatInt(pasteID, 10)
c.Logger.Debug().Msg("Paste saved, URL: /paste/" + newPasteIDAsString)
// Private pastes have it's timestamp in URL.
if paste.Private {
// nolint:wrapcheck
return ec.Redirect(http.StatusFound, "/paste/"+newPasteIDAsString+"/"+strconv.FormatInt(paste.CreatedAt.Unix(), 10))
return ectx.Redirect(http.StatusFound, "/paste/"+newPasteIDAsString+"/"+strconv.FormatInt(paste.CreatedAt.Unix(), 10))
}
// nolint:wrapcheck
return ec.Redirect(http.StatusFound, "/paste/"+newPasteIDAsString)
return ectx.Redirect(http.StatusFound, "/paste/"+newPasteIDAsString)
}

View File

@@ -37,15 +37,15 @@ import (
// GET for "/pastes/", a list of publicly available pastes.
// Web interface version.
func pastesGET(ec echo.Context) error {
func pastesGET(ectx echo.Context) error {
// We should check if database connection available.
dbConn := c.Database.GetDatabaseConnection()
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
// nolint:wrapcheck
return ec.Redirect(http.StatusFound, "/database_not_available")
return ectx.Redirect(http.StatusFound, "/database_not_available")
}
pageFromParamRaw := ec.Param("page")
pageFromParamRaw := ectx.Param("page")
page := 1
@@ -66,34 +66,34 @@ func pastesGET(ec echo.Context) error {
if err3 != nil {
c.Logger.Error().Err(err3).Msg("Failed to get pastes list from database")
noPastesToShowTpl := templater.GetErrorTemplate(ec, "No pastes to show.")
noPastesToShowTpl := templater.GetErrorTemplate(ectx, "No pastes to show.")
// nolint:wrapcheck
return ec.HTML(http.StatusOK, noPastesToShowTpl)
return ectx.HTML(http.StatusOK, noPastesToShowTpl)
}
if len(pastes) > 0 {
pastesString = ""
for i := range pastes {
for _, paste := range pastes {
pasteDataMap := make(map[string]string)
pasteDataMap["pasteID"] = strconv.Itoa(pastes[i].ID)
pasteDataMap["pasteTitle"] = pastes[i].Title
pasteDataMap["pasteDate"] = pastes[i].CreatedAt.Format("2006-01-02 @ 15:04:05") + " UTC"
pasteDataMap["pasteID"] = strconv.Itoa(paste.ID)
pasteDataMap["pasteTitle"] = paste.Title
pasteDataMap["pasteDate"] = paste.CreatedAt.Format("2006-01-02 @ 15:04:05") + " UTC"
// Get max 4 lines of each paste.
pasteDataSplitted := strings.Split(pastes[i].Data, "\n")
pasteDataSplitted := strings.Split(paste.Data, "\n")
var pasteData string
if len(pasteDataSplitted) < 4 {
pasteData = pastes[i].Data
pasteData = paste.Data
} else {
pasteData = strings.Join(pasteDataSplitted[0:4], "\n")
}
pasteDataMap["pasteData"] = pasteData
pasteTpl := templater.GetRawTemplate(ec, "pastelist_paste.html", pasteDataMap)
pasteTpl := templater.GetRawTemplate(ectx, "pastelist_paste.html", pasteDataMap)
pastesString += pasteTpl
}
@@ -104,8 +104,8 @@ func pastesGET(ec echo.Context) error {
c.Logger.Debug().Int("total pages", pages).Int("current page", page).Msg("Paging data")
paginationHTML := pagination.CreateHTML(page, pages, "/pastes/")
pasteListTpl := templater.GetTemplate(ec, "pastelist_list.html", map[string]string{"pastes": pastesString, "pagination": paginationHTML})
pasteListTpl := templater.GetTemplate(ectx, "pastelist_list.html", map[string]string{"pastes": pastesString, "pagination": paginationHTML})
// nolint:wrapcheck
return ec.HTML(http.StatusOK, pasteListTpl)
return ectx.HTML(http.StatusOK, pasteListTpl)
}