Skip to content

Commit

Permalink
fix: prose and pastes limit filesize uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Feb 3, 2025
1 parent c8eb96f commit cfe2c2b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 24 deletions.
27 changes: 17 additions & 10 deletions pastes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,25 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
if err == nil {
logger = logger.With("filename", post.Filename)
logger.Info("paste found")
parsedText, err := ParseText(post.Filename, post.Text)
if err != nil {
logger.Error("could not parse text", "err", err)
}
expiresAt := "never"
if post.ExpiresAt != nil {
expiresAt = post.ExpiresAt.Format(time.DateOnly)
}

unlisted := false
if post.Hidden {
unlisted = true
parsedText := ""
// we dont want to syntax highlight huge files
if post.FileSize > 1*utils.MB {
logger.Warn("paste too large to parse and apply syntax highlighting")
parsedText = post.Text
} else {
parsedText, err = ParseText(post.Filename, post.Text)
if err != nil {
logger.Error("could not parse text", "err", err)
}
if post.ExpiresAt != nil {
expiresAt = post.ExpiresAt.Format(time.DateOnly)
}

if post.Hidden {
unlisted = true
}
}

data = PostPageData{
Expand Down
23 changes: 12 additions & 11 deletions pastes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ func NewConfigSite() *shared.ConfigSite {
minioPass := utils.GetEnv("MINIO_ROOT_PASSWORD", "")

return &shared.ConfigSite{
Debug: debug == "1",
Domain: domain,
Port: port,
Protocol: protocol,
DbURL: dbURL,
StorageDir: storageDir,
MinioURL: minioURL,
MinioUser: minioUser,
MinioPass: minioPass,
Space: "pastes",
Logger: shared.CreateLogger("pastes"),
Debug: debug == "1",
Domain: domain,
Port: port,
Protocol: protocol,
DbURL: dbURL,
StorageDir: storageDir,
MinioURL: minioURL,
MinioUser: minioUser,
MinioPass: minioPass,
Space: "pastes",
Logger: shared.CreateLogger("pastes"),
MaxAssetSize: int64(3 * utils.MB),
}
}
11 changes: 10 additions & 1 deletion pastes/scp_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ type FileHooks struct {
func (p *FileHooks) FileValidate(s ssh.Session, data *filehandlers.PostMetaData) (bool, error) {
if !utils.IsTextFile(string(data.Text)) {
err := fmt.Errorf(
"WARNING: (%s) invalid file must be plain text (utf-8), skipping",
"ERROR: (%s) invalid file must be plain text (utf-8), skipping",
data.Filename,
)
return false, err
}

maxFileSize := int(p.Cfg.MaxAssetSize)
if data.FileSize > maxFileSize {
return false, fmt.Errorf(
"ERROR: file (%s) has exceeded maximum file size (%d bytes)",
data.Filename,
maxFileSize,
)
}

return true, nil
}

Expand Down
2 changes: 2 additions & 0 deletions prose/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/picosh/utils"
)

var MAX_FILE_SIZE = 3 * utils.MB

func NewConfigSite() *shared.ConfigSite {
debug := utils.GetEnv("PROSE_DEBUG", "0")
domain := utils.GetEnv("PROSE_DOMAIN", "prose.sh")
Expand Down
12 changes: 10 additions & 2 deletions prose/scp_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type MarkdownHooks struct {
func (p *MarkdownHooks) FileValidate(s ssh.Session, data *filehandlers.PostMetaData) (bool, error) {
if !utils.IsTextFile(data.Text) {
err := fmt.Errorf(
"WARNING: (%s) invalid file must be plain text (utf-8), skipping",
"ERROR: (%s) invalid file must be plain text (utf-8), skipping",
data.Filename,
)
return false, err
Expand All @@ -39,13 +39,21 @@ func (p *MarkdownHooks) FileValidate(s ssh.Session, data *filehandlers.PostMetaD
if !utils.IsExtAllowed(data.Filename, p.Cfg.AllowedExt) {
extStr := strings.Join(p.Cfg.AllowedExt, ",")
err := fmt.Errorf(
"WARNING: (%s) invalid file, format must be (%s), skipping",
"ERROR: (%s) invalid file, format must be (%s), skipping",
data.Filename,
extStr,
)
return false, err
}

if data.FileSize > MAX_FILE_SIZE {
return false, fmt.Errorf(
"ERROR: file (%s) has exceeded maximum file size (%d bytes)",
data.Filename,
MAX_FILE_SIZE,
)
}

return true, nil
}

Expand Down

0 comments on commit cfe2c2b

Please sign in to comment.