Skip to content

A very small library for html content policy and sanitization, focused on email html

License

Notifications You must be signed in to change notification settings

sonalys/sanitize

Repository files navigation

Sanitize

Go Reference Tests

Sanitize is a sanitization library.
It's purpose is to allow content policy enforcement. This library currently supports HTML.

Examples

package sanitize_test

import (
	"bytes"
	"fmt"
	"strings"

	"github.com/sonalys/sanitize"
)

const testEmail = `` +
	`<html><head>
<title>My Email</title>
</head>
<body>
<script>
	alert('not allowed');
</script>
<img onload="alert('not allowed')" src="a" />
<a href="http://visit.me">click here</a></body></html>
`

func ExampleHTML() {
	r := strings.NewReader(testEmail)
	w := bytes.NewBuffer(make([]byte, 0, len(testEmail)))

	err := sanitize.HTML(r, w,
		sanitize.SecureEmailPolicies(),
	)
	if err != nil {
		panic(err)
	}

	fmt.Print(w.String())
	// Output:
	// <html><head>
	// <title>My Email</title>
	// </head>
	// <body>
	//
	// <img/>
	// <a href="http://visit.me" rel="noreferrer nofollow">click here</a>
	// </body></html>
}

License

This repository is licensed under the MIT License

About

A very small library for html content policy and sanitization, focused on email html

Topics

Resources

License

Stars

Watchers

Forks

Languages