repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
b5a85a0
parent
9f8590c
author
Eric Bower
date
2022-07-30 15:04:00 +0000 UTC
fix: cleanup file validate logic
9 files changed,  +76, -56
M cmd/lists/ssh/main.go
+2, -2
 1@@ -65,10 +65,10 @@ func main() {
 2 	dbh := postgres.NewDB(&cfg.ConfigCms)
 3 	defer dbh.Close()
 4 
 5-	fileHandler := lists.ListsHandler{
 6+	hooks := &lists.ListHooks{
 7 		Cfg: cfg,
 8 	}
 9-	handler := filehandlers.NewScpPostHandler(dbh, cfg, &fileHandler)
10+	handler := filehandlers.NewScpPostHandler(dbh, cfg, hooks)
11 
12 	sshServer := &SSHServer{}
13 	s, err := wish.NewServer(
M cmd/pastes/ssh/main.go
+2, -2
 1@@ -64,10 +64,10 @@ func main() {
 2 	logger := cfg.Logger
 3 	dbh := postgres.NewDB(&cfg.ConfigCms)
 4 	defer dbh.Close()
 5-	fileHandler := pastes.PastesHandler{
 6+	hooks := &pastes.FileHooks{
 7 		Cfg: cfg,
 8 	}
 9-	handler := filehandlers.NewScpPostHandler(dbh, cfg, &fileHandler)
10+	handler := filehandlers.NewScpPostHandler(dbh, cfg, hooks)
11 
12 	sshServer := &SSHServer{}
13 	s, err := wish.NewServer(
M cmd/prose/ssh/main.go
+2, -2
 1@@ -64,10 +64,10 @@ func main() {
 2 	logger := cfg.Logger
 3 	dbh := postgres.NewDB(&cfg.ConfigCms)
 4 	defer dbh.Close()
 5-	fileHandler := &prose.ProseHandler{
 6+	hooks := &prose.MarkdownHooks{
 7 		Cfg: cfg,
 8 	}
 9-	handler := filehandlers.NewScpPostHandler(dbh, cfg, fileHandler)
10+	handler := filehandlers.NewScpPostHandler(dbh, cfg, hooks)
11 
12 	sshServer := &SSHServer{}
13 	s, err := wish.NewServer(
R lists/scp_handler.go => lists/scp_hooks.go
+13, -5
 1@@ -10,15 +10,23 @@ import (
 2 	"golang.org/x/exp/slices"
 3 )
 4 
 5-type ListsHandler struct {
 6+type ListHooks struct {
 7 	Cfg *shared.ConfigSite
 8 }
 9 
10-func (p *ListsHandler) FileValidate(text string, filename string) (bool, error) {
11-	if !shared.IsTextFile(text, filename, p.Cfg.AllowedExt) {
12+func (p *ListHooks) FileValidate(text string, filename string) (bool, error) {
13+	if !shared.IsTextFile(text) {
14+		err := fmt.Errorf(
15+			"WARNING: (%s) invalid file must be plain text (utf-8), skipping",
16+			filename,
17+		)
18+		return false, err
19+	}
20+
21+	if !shared.IsExtAllowed(filename, p.Cfg.AllowedExt) {
22 		extStr := strings.Join(p.Cfg.AllowedExt, ",")
23 		err := fmt.Errorf(
24-			"WARNING: (%s) invalid file, format must be (%s) and the contents must be plain text, skipping",
25+			"WARNING: (%s) invalid file, format must be (%s), skipping",
26 			filename,
27 			extStr,
28 		)
29@@ -28,7 +36,7 @@ func (p *ListsHandler) FileValidate(text string, filename string) (bool, error)
30 	return true, nil
31 }
32 
33-func (p *ListsHandler) FileMeta(text string, data *filehandlers.PostMetaData) error {
34+func (p *ListHooks) FileMeta(text string, data *filehandlers.PostMetaData) error {
35 	parsedText := pkg.ParseText(text)
36 
37 	if parsedText.MetaData.Title != "" {
D pastes/scp_handler.go
+0, -28
 1@@ -1,28 +0,0 @@
 2-package pastes
 3-
 4-import (
 5-	"fmt"
 6-
 7-	"git.sr.ht/~erock/pico/filehandlers"
 8-	"git.sr.ht/~erock/pico/shared"
 9-)
10-
11-type PastesHandler struct {
12-	Cfg *shared.ConfigSite
13-}
14-
15-func (p *PastesHandler) FileValidate(text string, filename string) (bool, error) {
16-	if !shared.IsTextFile(text, filename, p.Cfg.AllowedExt) {
17-		err := fmt.Errorf(
18-			"WARNING: (%s) invalid file, the contents must be plain text, skipping",
19-			filename,
20-		)
21-		return false, err
22-	}
23-
24-	return true, nil
25-}
26-
27-func (p *PastesHandler) FileMeta(text string, data *filehandlers.PostMetaData) error {
28-	return nil
29-}
A pastes/scp_hooks.go
+28, -0
 1@@ -0,0 +1,28 @@
 2+package pastes
 3+
 4+import (
 5+	"fmt"
 6+
 7+	"git.sr.ht/~erock/pico/filehandlers"
 8+	"git.sr.ht/~erock/pico/shared"
 9+)
10+
11+type FileHooks struct {
12+	Cfg *shared.ConfigSite
13+}
14+
15+func (p *FileHooks) FileValidate(text string, filename string) (bool, error) {
16+	if !shared.IsTextFile(text) {
17+		err := fmt.Errorf(
18+			"WARNING: (%s) invalid file must be plain text (utf-8), skipping",
19+			filename,
20+		)
21+		return false, err
22+	}
23+
24+	return true, nil
25+}
26+
27+func (p *FileHooks) FileMeta(text string, data *filehandlers.PostMetaData) error {
28+	return nil
29+}
M prose/config.go
+1, -1
1@@ -43,7 +43,7 @@ func NewConfigSite() *shared.ConfigSite {
2 			Description: "a blog platform for hackers.",
3 			IntroText:   intro,
4 			Space:       "prose",
5-			AllowedExt:  []string{".md", ".css"},
6+			AllowedExt:  []string{".md"},
7 			HiddenPosts: []string{"_readme.md", "_styles.css"},
8 			Logger:      shared.CreateLogger(),
9 		},
R prose/scp_handler.go => prose/scp_hooks.go
+18, -8
 1@@ -9,18 +9,28 @@ import (
 2 	"golang.org/x/exp/slices"
 3 )
 4 
 5-// var hiddenPosts = []string{"_readme.md", "_styles.css"}
 6-// var allowedExtensions = []string{".md", ".css"}
 7-
 8-type ProseHandler struct {
 9+type MarkdownHooks struct {
10 	Cfg *shared.ConfigSite
11 }
12 
13-func (p *ProseHandler) FileValidate(text string, filename string) (bool, error) {
14-	if !shared.IsTextFile(text, filename, p.Cfg.AllowedExt) {
15+func (p *MarkdownHooks) FileValidate(text string, filename string) (bool, error) {
16+	if !shared.IsTextFile(text) {
17+		err := fmt.Errorf(
18+			"WARNING: (%s) invalid file must be plain text (utf-8), skipping",
19+			filename,
20+		)
21+		return false, err
22+	}
23+
24+	// special styles css file we want to permit but no other css file
25+	if filename == "_styles.css" {
26+		return true, nil
27+	}
28+
29+	if !shared.IsExtAllowed(filename, p.Cfg.AllowedExt) {
30 		extStr := strings.Join(p.Cfg.AllowedExt, ",")
31 		err := fmt.Errorf(
32-			"WARNING: (%s) invalid file, format must be (%s) and the contents must be plain text, skipping",
33+			"WARNING: (%s) invalid file, format must be (%s), skipping",
34 			filename,
35 			extStr,
36 		)
37@@ -30,7 +40,7 @@ func (p *ProseHandler) FileValidate(text string, filename string) (bool, error)
38 	return true, nil
39 }
40 
41-func (p *ProseHandler) FileMeta(text string, data *filehandlers.PostMetaData) error {
42+func (p *MarkdownHooks) FileMeta(text string, data *filehandlers.PostMetaData) error {
43 	parsedText, err := ParseText(text)
44 	if err != nil {
45 		return err
M shared/util.go
+10, -8
 1@@ -70,18 +70,20 @@ func IsText(s string) bool {
 2 	return true
 3 }
 4 
 5+func IsExtAllowed(filename string, allowedExt []string) bool {
 6+	ext := pathpkg.Ext(filename)
 7+	if !slices.Contains(allowedExt, ext) {
 8+		return false
 9+	}
10+
11+	return true
12+}
13+
14 // IsTextFile reports whether the file has a known extension indicating
15 // a text file, or if a significant chunk of the specified file looks like
16 // correct UTF-8; that is, if it is likely that the file contains human-
17 // readable text.
18-func IsTextFile(text string, filename string, allowedExtensions []string) bool {
19-	if len(allowedExtensions) > 0 {
20-		ext := pathpkg.Ext(filename)
21-		if !slices.Contains(allowedExtensions, ext) {
22-			return false
23-		}
24-	}
25-
26+func IsTextFile(text string) bool {
27 	num := math.Min(float64(len(text)), 1024)
28 	return IsText(text[0:int(num)])
29 }