Skip to content

Commit

Permalink
add multiple policy checks
Browse files Browse the repository at this point in the history
  • Loading branch information
S Riemersma committed Sep 19, 2022
1 parent 0e36152 commit 6bc6069
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 5 additions & 2 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,13 @@ import (
//
// The JSON null value unmarshals into an interface, map, pointer, or slice
// by setting that Go value to nil. Because null is often used in JSON to mean
// ``not present,'' unmarshaling a JSON null into any other Go type has no effect
// not present, unmarshaling a JSON null into any other Go type has no effect
// on the value and produces no error.
//
// When unmarshaling quoted strings, invalid UTF-8 or
// invalid UTF-16 surrogate pairs are not treated as an error.
// Instead, they are replaced by the Unicode replacement
// character U+FFFD.
//
func Unmarshal(data []byte, v interface{}) error {
// Check for well-formedness.
// Avoids filling out half a data structure
Expand Down Expand Up @@ -1210,6 +1209,10 @@ func (d *decodeState) literalInterface() interface{} {
if !ok {
panic(phasePanicMsg)
}

if d.useBlueMonday {
s = d.blueMonday.Sanitize(s)
}
return s

default: // number
Expand Down
9 changes: 8 additions & 1 deletion stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ func (dec *Decoder) UseBlueMonday() *Decoder {
return dec
}

// UseBlueMonday causes the Decoder to unmarshal a string literal and escape
// any XSS using the blue monday package
func (dec *Decoder) WithPolicy(policy *bluemonday.Policy) *Decoder {
dec.d.useBlueMonday = true
dec.d.blueMonday = policy
return dec
}

// UseIgnoreEmpty causes the Decoder to ignore empty objects in json.
func (dec *Decoder) IgnoreEmptyObject() { dec.d.ignoreEmpty = true }

Expand Down Expand Up @@ -331,7 +339,6 @@ var _ Unmarshaler = (*RawMessage)(nil)
// Number, for JSON numbers
// string, for JSON string literals
// nil, for JSON null
//
type Token interface{}

const (
Expand Down

0 comments on commit 6bc6069

Please sign in to comment.