From 0e6cfd917231499478ff3d542bcececfdf9f52dd Mon Sep 17 00:00:00 2001 From: SYM01 <33443792+SYM01@users.noreply.github.com> Date: Wed, 13 Dec 2023 11:59:40 +0800 Subject: [PATCH] expose a cmd for quick use / test --- cmd/htmlsanitizer/main.go | 49 +++++++++++++++++++++++++++++++++++++++ sanitizer_test.go | 4 ++++ 2 files changed, 53 insertions(+) create mode 100644 cmd/htmlsanitizer/main.go diff --git a/cmd/htmlsanitizer/main.go b/cmd/htmlsanitizer/main.go new file mode 100644 index 0000000..e026e3f --- /dev/null +++ b/cmd/htmlsanitizer/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "flag" + "io" + "log" + "net/http" + "os" + "strings" + + "github.com/sym01/htmlsanitizer" +) + +var ( + srcFilePath = flag.String("src", "", "could be either source file path, or the source URL") +) + +func main() { + flag.Parse() + + if len(*srcFilePath) == 0 { + flag.CommandLine.Usage() + return + } + + var src io.ReadCloser + switch { + case strings.HasPrefix(*srcFilePath, "http://"), strings.HasPrefix(*srcFilePath, "https://"): + resp, err := http.Get(*srcFilePath) + if err != nil { + log.Fatalf("unable to fetch remote content: %s", err) + } + src = resp.Body + default: + file, err := os.OpenFile(*srcFilePath, os.O_RDONLY, 0755) + if err != nil { + log.Fatalf("unable to open src file: %s", err) + } + src = file + } + + defer src.Close() + + san := htmlsanitizer.NewHTMLSanitizer() + writer := san.NewWriter(os.Stdout) + if _, err := io.Copy(writer, src); err != nil { + log.Printf("unable to sanitize HTML content: %s", err) + } +} diff --git a/sanitizer_test.go b/sanitizer_test.go index 78a5c4a..a26fea2 100644 --- a/sanitizer_test.go +++ b/sanitizer_test.go @@ -498,6 +498,10 @@ var testCases = []struct { in: `XSS`, out: `XSS`, }, + { + in: `func RemoveAll `, + out: `func RemoveAll `, + }, { in: `