Skip to content

Instantly share code, notes, and snippets.

@gregxsunday
Created February 20, 2023 15:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregxsunday/4b08ea3f4b3961ac9cefcc3673b7c3c5 to your computer and use it in GitHub Desktop.
Save gregxsunday/4b08ea3f4b3961ac9cefcc3673b7c3c5 to your computer and use it in GitHub Desktop.
Source code relevant to this video writeup: https://www.youtube.com/watch?v=H1TVk3HhL9E
<!--!> <h1 value="--><a href="javascript:alert(document.domain)">link
package main
import (
"fmt"
"io/ioutil"
"log"
"strings"
"golang.org/x/net/html"
)
func readHtmlFromFile(fileName string) (string, error) {
bs, err := ioutil.ReadFile(fileName)
if err != nil {
return "", err
}
return string(bs), nil
}
func is_allowed_html(text string) bool {
tkn := html.NewTokenizer(strings.NewReader(text))
for {
tt := tkn.Next()
switch {
case tt == html.ErrorToken:
return true
case tt == html.StartTagToken:
t := tkn.Token()
if t.Data == "h1" {
continue
} else {
return false
}
}
}
}
func main() {
fileName := "index.html"
text, err := readHtmlFromFile(fileName)
if err != nil {
log.Fatal(err)
}
valid := is_allowed_html(text)
fmt.Println(valid)
// html is safe, we can proceed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment