Skip to content

Commit

Permalink
Merge pull request #3 from SYM01/develop
Browse files Browse the repository at this point in the history
Optimise the support for <script>, <style> and other similar non-html elements
  • Loading branch information
SYM01 authored Dec 13, 2023
2 parents 39525a9 + a2afdf9 commit 87a0f56
Show file tree
Hide file tree
Showing 11 changed files with 405 additions and 92 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Golang HTML Sanitizer
# Golang HTML Sanitizer / Filter

![Go](https://github.com/SYM01/htmlsanitizer/workflows/Go/badge.svg)
[![Go Reference](https://pkg.go.dev/badge/github.com/sym01/htmlsanitizer.svg)](https://pkg.go.dev/github.com/sym01/htmlsanitizer)
[![Go](https://github.com/SYM01/htmlsanitizer/workflows/Go/badge.svg)](https://github.com/SYM01/htmlsanitizer/actions/workflows/go.yml)
[![codecov](https://codecov.io/gh/SYM01/htmlsanitizer/branch/master/graph/badge.svg)](https://codecov.io/gh/SYM01/htmlsanitizer)


htmlsanitizer is a super fast, allowlist-based HTML sanitizer written in Golang. A built-in, secure-by-default allowlist helps you filter out any dangerous HTML content.
htmlsanitizer is a super fast, allowlist-based HTML sanitizer (HTML filter) written in Golang. A built-in, secure-by-default allowlist helps you filter out any dangerous HTML content.

Why use htmlsanitizer?

Expand Down
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)
}
}
Loading

0 comments on commit 87a0f56

Please sign in to comment.