Skip to content

Commit

Permalink
fix: Do not escape < > in jsonify
Browse files Browse the repository at this point in the history
Closes #38
  • Loading branch information
aeneasr committed Feb 18, 2020
1 parent e433390 commit 579c552
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
31 changes: 31 additions & 0 deletions api/v1alpha1/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package v1alpha1

import (
"bytes"
"encoding/json"
)

func unescapedMarshalIndent(in interface{}, prefix, indent string) ([]byte, error) {
var b bytes.Buffer
enc := json.NewEncoder(&b)
enc.SetEscapeHTML(false)
enc.SetIndent(prefix, indent)
if err := enc.Encode(in); err != nil {
return nil, err
}

result := b.Bytes()
return result[:len(result)-1], nil
}

func unescapedMarshal(in interface{}) ([]byte, error) {
var b bytes.Buffer
enc := json.NewEncoder(&b)
enc.SetEscapeHTML(false)
if err := enc.Encode(in); err != nil {
return nil, err
}

result := b.Bytes()
return result[:len(result)-1], nil
}
4 changes: 1 addition & 3 deletions api/v1alpha1/rule_json.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package v1alpha1

import "encoding/json"

// RuleJson is a representation of an Oathkeeper rule.
type RuleJSON struct {
ID string `json:"id"`
Expand All @@ -13,7 +11,7 @@ func (rj RuleJSON) MarshalJSON() ([]byte, error) {

type Alias RuleJSON

return json.Marshal(&struct {
return unescapedMarshal(&struct {
Upstream *UpstreamJSON `json:"upstream"`
Alias
}{
Expand Down
7 changes: 4 additions & 3 deletions api/v1alpha1/rule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ limitations under the License.
package v1alpha1

import (
"encoding/json"
"fmt"
"github.com/ory/oathkeeper-maester/internal/validation"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

"github.com/ory/oathkeeper-maester/internal/validation"
)

const (
Expand Down Expand Up @@ -139,7 +140,7 @@ func (rl RuleList) ToOathkeeperRules() ([]byte, error) {
rules[i] = rl.Items[i].ToRuleJSON()
}

return json.MarshalIndent(rules, "", " ")
return unescapedMarshalIndent(rules, "", " ")
}

// FilterNotValid filters out Rules which doesn't pass validation due to being not processed yet or due to negative result of validation. It returns a list of Rules which passed validation successfully.
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/rule_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func TestToOathkeeperRules(t *testing.T) {
newStringPtr("/api/v1"),
nil,
newBoolPtr(true),
[]*Authenticator{&Authenticator{h1}},
[]*Authenticator{{h1}},
nil,
[]*Mutator{{h1}, {h2}})

Expand All @@ -203,7 +203,7 @@ func TestToOathkeeperRules(t *testing.T) {
nil,
nil,
newBoolPtr(false),
[]*Authenticator{&Authenticator{h1}, {h2}},
[]*Authenticator{{h1}, {h2}},
nil,
nil)

Expand Down

0 comments on commit 579c552

Please sign in to comment.