Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(pgs): switch from allowlist to denylist for files #119

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 12 additions & 8 deletions filehandlers/assets/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/slog"
"os"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -360,14 +361,17 @@ func (h *UploadAssetHandler) validateAsset(data *FileData) (bool, error) {
return true, nil
}

if !shared.IsExtAllowed(fname, h.Cfg.AllowedExt) {
extStr := strings.Join(h.Cfg.AllowedExt, ",")
err := fmt.Errorf(
"ERROR: (%s) invalid file, format must be (%s), skipping",
fname,
extStr,
)
return false, err
dotFileRe := regexp.MustCompile(`/\..+`)
// TODO: let user control this list somehow
denylist := []*regexp.Regexp{dotFileRe}
for _, denyRe := range denylist {
if denyRe.MatchString(data.Filepath) {
err := fmt.Errorf(
"ERROR: (%s) file rejected, https://pico.sh/pgs#file-denylist",
data.Filepath,
)
return false, err
}
}

return true, nil
Expand Down
66 changes: 12 additions & 54 deletions pgs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,60 +38,18 @@ func NewConfigSite() *shared.ConfigSite {
UseImgProxy: useImgProxy == "1",
Secret: secret,
ConfigCms: config.ConfigCms{
Domain: domain,
Email: email,
Port: port,
Protocol: protocol,
DbURL: dbURL,
StorageDir: storageDir,
MinioURL: minioURL,
MinioUser: minioUser,
MinioPass: minioPass,
Description: "A zero-install static site hosting service for hackers",
IntroText: intro,
Space: "pgs",
// IMPORTANT: make sure `shared.GetMimeType` has the extensions being
// added here.
AllowedExt: []string{
".jpg",
".jpeg",
".png",
".gif",
".webp",
".svg",
".ico",
".html",
".htm",
".css",
".js",
".pdf",
".txt",
".otf",
".ttf",
".woff",
".woff2",
".json",
".md",
".rss",
".xml",
".atom",
".map",
".webmanifest",
".avif",
".heif",
".heic",
".opus",
".wav",
".mp3",
".mp4",
".mpeg",
".wasm",
".xsl",
".opml",
".eot",
".yml",
".yaml",
},
Domain: domain,
Email: email,
Port: port,
Protocol: protocol,
DbURL: dbURL,
StorageDir: storageDir,
MinioURL: minioURL,
MinioUser: minioUser,
MinioPass: minioPass,
Description: "A zero-install static site hosting service for hackers",
IntroText: intro,
Space: "pgs",
MaxSize: maxSize,
MaxAssetSize: maxAssetSize,
Logger: shared.CreateLogger(debug == "1"),
Expand Down