Skip to content

Commit

Permalink
expose a cmd for quick use / test
Browse files Browse the repository at this point in the history
  • Loading branch information
SYM01 committed Dec 13, 2023
1 parent 38e3bd6 commit 0e6cfd9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
49 changes: 49 additions & 0 deletions cmd/htmlsanitizer/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 4 additions & 0 deletions sanitizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,10 @@ var testCases = []struct {
in: `<A HREF="javascript:document.location='http://www.google.com/'">XSS</A>`,
out: `<a>XSS</a>`,
},
{
in: `<span>func <a class= "Documentation-source" href="https://cs.opensource.google/go/go/+/go1.21.5:src/os/path.go;l=66">RemoveAll</a> <a class="Documentation-idLink" href="#RemoveAll" aria-label="Go to RemoveAll">¶</a></span>`,
out: `<span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.21.5:src/os/path.go;l=66">RemoveAll</a> <a class="Documentation-idLink" href="#RemoveAll">¶</a></span>`,
},
{
in: `
<Img src = x onerror = "javascript: window.onerror = alert; throw XSS">
Expand Down

0 comments on commit 0e6cfd9

Please sign in to comment.