Skip to content

Commit 6bb7e52

Browse files
committed
Meta: more correct handling of HTML-unescaping
It turns out that straight byte-based replacements of unicode escape characters back to their ascii representations is invalid if the unicode escape character itself is escaped (e.g. "\u003c" => "\\u003c" => "\<"). To solve this, we will instead unmarshal Meta objects to map[string]interface{}, extract the expected Meta fields from the map, and then use a JSON encoder with SetEscapeHTML(false) to re-encode the map back to JSON to be stored in Meta.Blob. Signed-off-by: Joe Lanford <[email protected]>
1 parent 0481f3f commit 6bb7e52

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

alpha/declcfg/declcfg.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"fmt"
88
"strings"
99

10-
"go4.org/bytereplacer"
11-
1210
"github.com/operator-framework/operator-registry/alpha/property"
1311
)
1412

@@ -100,25 +98,27 @@ func (m Meta) MarshalJSON() ([]byte, error) {
10098
}
10199

102100
func (m *Meta) UnmarshalJSON(blob []byte) error {
103-
blob = bytereplacer.New(`\u003c`, "<", `\u003e`, ">", `\u0026`, "&").Replace(blob)
104-
105-
type tmp struct {
106-
Schema string `json:"schema"`
107-
Package string `json:"package,omitempty"`
108-
Name string `json:"name,omitempty"`
109-
Properties []property.Property `json:"properties,omitempty"`
101+
blobMap := map[string]interface{}{}
102+
if err := json.Unmarshal(blob, &blobMap); err != nil {
103+
return err
104+
}
105+
for key, ptr := range map[string]*string{"schema": &m.Schema, "package": &m.Package, "name": &m.Name} {
106+
if _, ok := blobMap[key]; !ok {
107+
continue
108+
}
109+
v, ok := blobMap[key].(string)
110+
if !ok {
111+
return fmt.Errorf("expected value for key %q to be a string, got %t: %v", key, blobMap[key], blobMap[key])
112+
}
113+
*ptr = v
110114
}
111-
var t tmp
112-
if err := json.Unmarshal(blob, &t); err != nil {
113-
// TODO: return an error that includes the the full JSON message,
114-
// the offset of the error, and the error message. Let callers
115-
// decide how to format it.
116-
return errors.New(resolveUnmarshalErr(blob, err))
115+
buf := bytes.Buffer{}
116+
enc := json.NewEncoder(&buf)
117+
enc.SetEscapeHTML(false)
118+
if err := enc.Encode(blobMap); err != nil {
119+
return err
117120
}
118-
m.Schema = t.Schema
119-
m.Package = t.Package
120-
m.Name = t.Name
121-
m.Blob = blob
121+
m.Blob = buf.Bytes()
122122
return nil
123123
}
124124

0 commit comments

Comments
 (0)