mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-09-05 11:06:23 -07:00
Show an error if the post has a banned filename, checksum, or name
This commit is contained in:
parent
f2c3a6b1ac
commit
8edb0cfeb4
2 changed files with 29 additions and 41 deletions
|
@ -15,57 +15,30 @@ import (
|
|||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
func showBanpage(ban gcsql.Ban, banType string, upload *gcsql.Upload, post *gcsql.Post, postBoard *gcsql.Board, writer http.ResponseWriter, request *http.Request) {
|
||||
// TODO: possibly split file/username/filename bans into separate page template
|
||||
tmplMap := map[string]interface{}{
|
||||
func showBanpage(ban *gcsql.IPBan, post *gcsql.Post, postBoard *gcsql.Board, writer http.ResponseWriter, request *http.Request) {
|
||||
banPageBuffer := bytes.NewBufferString("")
|
||||
err := serverutil.MinifyTemplate(gctemplates.Banpage, map[string]interface{}{
|
||||
"systemCritical": config.GetSystemCriticalConfig(),
|
||||
"siteConfig": config.GetSiteConfig(),
|
||||
"boardConfig": config.GetBoardConfig(postBoard.Dir),
|
||||
"ban": ban,
|
||||
"board": postBoard,
|
||||
}
|
||||
if upload != nil {
|
||||
tmplMap["filename"] = upload.OriginalFilename
|
||||
}
|
||||
ipb, ok := ban.(*gcsql.IPBan)
|
||||
if ok {
|
||||
tmplMap["permanent"] = ipb.Permanent
|
||||
tmplMap["expires"] = ipb.ExpiresAt
|
||||
}
|
||||
banPageBuffer := bytes.NewBufferString("")
|
||||
err := serverutil.MinifyTemplate(gctemplates.Banpage, tmplMap, banPageBuffer, "text/html")
|
||||
"permanent": ban.Permanent,
|
||||
"expires": ban.ExpiresAt,
|
||||
}, banPageBuffer, "text/html")
|
||||
if err != nil {
|
||||
gcutil.LogError(err).
|
||||
Str("IP", post.IP).
|
||||
Str("building", "minifier").
|
||||
Str("banType", banType).
|
||||
Str("template", "banpage.html").Send()
|
||||
serverutil.ServeErrorPage(writer, "Error minifying page: "+err.Error())
|
||||
return
|
||||
}
|
||||
writer.Write(banPageBuffer.Bytes())
|
||||
ev := gcutil.LogInfo().
|
||||
gcutil.LogWarning().
|
||||
Str("IP", post.IP).
|
||||
Str("boardDir", postBoard.Dir).
|
||||
Str("banType", banType)
|
||||
switch banType {
|
||||
case "ip":
|
||||
ev.Msg("Rejected post from banned IP")
|
||||
case "username":
|
||||
ev.
|
||||
Str("name", post.Name).
|
||||
Str("tripcode", post.Tripcode).
|
||||
Msg("Rejected post with banned name/tripcode")
|
||||
case "filename":
|
||||
ev.
|
||||
Str("filename", upload.OriginalFilename).
|
||||
Msg("Rejected post with banned filename")
|
||||
case "checksum":
|
||||
ev.
|
||||
Str("filename", upload.OriginalFilename).
|
||||
Str("checksum", upload.Checksum).
|
||||
Msg("Rejected post with banned checksum")
|
||||
}
|
||||
Msg("Rejected post from banned IP")
|
||||
}
|
||||
|
||||
// checks the post for spam. It returns true if a ban page or an error page was served (causing MakePost() to return)
|
||||
|
@ -83,7 +56,7 @@ func checkIpBan(post *gcsql.Post, postBoard *gcsql.Board, writer http.ResponseWr
|
|||
return false // ip is not banned and there were no errors, keep going
|
||||
}
|
||||
// IP is banned
|
||||
showBanpage(ipBan, "ip", nil, post, postBoard, writer, request)
|
||||
showBanpage(ipBan, post, postBoard, writer, request)
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -105,7 +78,13 @@ func checkUsernameBan(formName string, post *gcsql.Post, postBoard *gcsql.Board,
|
|||
if nameBan == nil {
|
||||
return false // name is not banned
|
||||
}
|
||||
showBanpage(nameBan, "username", nil, post, postBoard, writer, request)
|
||||
serverutil.ServeError(writer, "Name or tripcode not allowed", serverutil.IsRequestingJSON(request), map[string]interface{}{})
|
||||
gcutil.LogWarning().
|
||||
Str("IP", post.IP).
|
||||
Str("boardDir", postBoard.Dir).
|
||||
Str("name", post.Name).
|
||||
Str("tripcode", post.Tripcode).
|
||||
Msg("Rejected post with banned name/tripcode")
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -123,7 +102,10 @@ func checkFilenameBan(upload *gcsql.Upload, post *gcsql.Post, postBoard *gcsql.B
|
|||
if filenameBan == nil {
|
||||
return false
|
||||
}
|
||||
showBanpage(filenameBan, "filename", upload, post, postBoard, writer, request)
|
||||
serverutil.ServeError(writer, "Filename not allowed", serverutil.IsRequestingJSON(request), map[string]interface{}{})
|
||||
gcutil.LogWarning().
|
||||
Str("originalFilename", upload.OriginalFilename).
|
||||
Msg("File rejected for having a banned filename")
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -141,7 +123,11 @@ func checkChecksumBan(upload *gcsql.Upload, post *gcsql.Post, postBoard *gcsql.B
|
|||
if fileBan == nil {
|
||||
return false
|
||||
}
|
||||
showBanpage(fileBan, "checksum", upload, post, postBoard, writer, request)
|
||||
serverutil.ServeError(writer, "File not allowed", serverutil.IsRequestingJSON(request), map[string]interface{}{})
|
||||
gcutil.LogWarning().
|
||||
Str("originalFilename", upload.OriginalFilename).
|
||||
Str("checksum", upload.Checksum).
|
||||
Msg("File rejected for having a banned checksum")
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -287,7 +287,8 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
|
|||
}
|
||||
|
||||
if checkFilenameBan(upload, &post, postBoard, writer, request) {
|
||||
// if checkFilenameBan returns true, a ban page or error was displayed
|
||||
// If checkFilenameBan returns true, an error occured or the file was
|
||||
// rejected for having a banned filename, and the incident was logged either way
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -304,7 +305,8 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
|
|||
// Calculate image checksum
|
||||
upload.Checksum = fmt.Sprintf("%x", md5.Sum(data))
|
||||
if checkChecksumBan(upload, &post, postBoard, writer, request) {
|
||||
// checkChecksumBan returns true, a ban page or error was displayed
|
||||
// If checkChecksumBan returns true, an error occured or the file was
|
||||
// rejected for having a banned checksum, and the incident was logged either way
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue