Skip to content

Commit

Permalink
GoogleCloudPlatform#484 improved JSON backslash unescaping
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykorwat committed May 1, 2020
1 parent f59f788 commit 8921ac7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
24 changes: 12 additions & 12 deletions providers/aws/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (g *WafGenerator) loadWebACL(svc *waf.Client) error {
for _, acl := range output.WebACLs {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*acl.WebACLId,
*acl.Name,
*acl.Name+"_"+(*acl.WebACLId)[0:8],
"aws_waf_web_acl",
"aws",
wafAllowEmptyValues))
Expand All @@ -98,7 +98,7 @@ func (g *WafGenerator) loadByteMatchSet(svc *waf.Client) error {
for _, byteMatchSet := range output.ByteMatchSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*byteMatchSet.ByteMatchSetId,
*byteMatchSet.Name,
*byteMatchSet.Name+"_"+(*byteMatchSet.ByteMatchSetId)[0:8],
"aws_waf_byte_match_set",
"aws",
wafAllowEmptyValues))
Expand All @@ -114,7 +114,7 @@ func (g *WafGenerator) loadGeoMatchSet(svc *waf.Client) error {
for _, matchSet := range output.GeoMatchSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*matchSet.GeoMatchSetId,
*matchSet.Name,
*matchSet.Name+"_"+(*matchSet.GeoMatchSetId)[0:8],
"aws_waf_geo_match_set",
"aws",
wafAllowEmptyValues))
Expand All @@ -130,7 +130,7 @@ func (g *WafGenerator) loadIPSet(svc *waf.Client) error {
for _, IPSet := range output.IPSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*IPSet.IPSetId,
*IPSet.Name,
*IPSet.Name+"_"+(*IPSet.IPSetId)[0:8],
"aws_waf_ipset",
"aws",
wafAllowEmptyValues))
Expand All @@ -146,7 +146,7 @@ func (g *WafGenerator) loadRateBasedRules(svc *waf.Client) error {
for _, rule := range output.Rules {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*rule.RuleId,
*rule.Name,
*rule.Name+"_"+(*rule.RuleId)[0:8],
"aws_waf_rate_based_rule",
"aws",
wafAllowEmptyValues))
Expand All @@ -162,7 +162,7 @@ func (g *WafGenerator) loadRegexMatchSets(svc *waf.Client) error {
for _, regexMatchSet := range output.RegexMatchSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*regexMatchSet.RegexMatchSetId,
*regexMatchSet.Name,
*regexMatchSet.Name+"_"+(*regexMatchSet.RegexMatchSetId)[0:8],
"aws_waf_regex_match_set",
"aws",
wafAllowEmptyValues))
Expand All @@ -178,7 +178,7 @@ func (g *WafGenerator) loadRegexPatternSets(svc *waf.Client) error {
for _, regexPatternSet := range output.RegexPatternSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*regexPatternSet.RegexPatternSetId,
*regexPatternSet.Name,
*regexPatternSet.Name+"_"+(*regexPatternSet.RegexPatternSetId)[0:8],
"aws_waf_regex_pattern_set",
"aws",
wafAllowEmptyValues))
Expand All @@ -194,7 +194,7 @@ func (g *WafGenerator) loadWafRules(svc *waf.Client) error {
for _, rule := range output.Rules {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*rule.RuleId,
*rule.Name,
*rule.Name+"_"+(*rule.RuleId)[0:8],
"aws_waf_rule",
"aws",
wafAllowEmptyValues))
Expand All @@ -210,7 +210,7 @@ func (g *WafGenerator) loadWafRuleGroups(svc *waf.Client) error {
for _, ruleGroup := range output.RuleGroups {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*ruleGroup.RuleGroupId,
*ruleGroup.Name,
*ruleGroup.Name+"_"+(*ruleGroup.RuleGroupId)[0:8],
"aws_waf_rule_group",
"aws",
wafAllowEmptyValues))
Expand All @@ -226,7 +226,7 @@ func (g *WafGenerator) loadSizeConstraintSets(svc *waf.Client) error {
for _, sizeConstraintSet := range output.SizeConstraintSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*sizeConstraintSet.SizeConstraintSetId,
*sizeConstraintSet.Name,
*sizeConstraintSet.Name+"_"+(*sizeConstraintSet.SizeConstraintSetId)[0:8],
"aws_waf_size_constraint_set",
"aws",
wafAllowEmptyValues))
Expand All @@ -242,7 +242,7 @@ func (g *WafGenerator) loadSqlInjectionMatchSets(svc *waf.Client) error {
for _, sqlInjectionMatchSet := range output.SqlInjectionMatchSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*sqlInjectionMatchSet.SqlInjectionMatchSetId,
*sqlInjectionMatchSet.Name,
*sqlInjectionMatchSet.Name+"_"+(*sqlInjectionMatchSet.SqlInjectionMatchSetId)[0:8],
"aws_waf_sql_injection_match_set",
"aws",
wafAllowEmptyValues))
Expand All @@ -258,7 +258,7 @@ func (g *WafGenerator) loadXssMatchSet(svc *waf.Client) error {
for _, xssMatchSet := range output.XssMatchSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*xssMatchSet.XssMatchSetId,
*xssMatchSet.Name,
*xssMatchSet.Name+"_"+(*xssMatchSet.XssMatchSetId)[0:8],
"aws_waf_xss_match_set",
"aws",
wafAllowEmptyValues))
Expand Down
24 changes: 12 additions & 12 deletions providers/aws/waf_regional.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (g *WafRegionalGenerator) loadWebACL(svc *wafregional.Client) error {
for _, acl := range output.WebACLs {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*acl.WebACLId,
*acl.Name,
*acl.Name+"_"+(*acl.WebACLId)[0:8],
"aws_wafregional_web_acl",
"aws",
wafAllowEmptyValues))
Expand All @@ -97,7 +97,7 @@ func (g *WafRegionalGenerator) loadByteMatchSet(svc *wafregional.Client) error {
for _, byteMatchSet := range output.ByteMatchSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*byteMatchSet.ByteMatchSetId,
*byteMatchSet.Name,
*byteMatchSet.Name+"_"+(*byteMatchSet.ByteMatchSetId)[0:8],
"aws_wafregional_byte_match_set",
"aws",
wafAllowEmptyValues))
Expand All @@ -113,7 +113,7 @@ func (g *WafRegionalGenerator) loadGeoMatchSet(svc *wafregional.Client) error {
for _, matchSet := range output.GeoMatchSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*matchSet.GeoMatchSetId,
*matchSet.Name,
*matchSet.Name+"_"+(*matchSet.GeoMatchSetId)[0:8],
"aws_wafregional_geo_match_set",
"aws",
wafAllowEmptyValues))
Expand All @@ -129,7 +129,7 @@ func (g *WafRegionalGenerator) loadIPSet(svc *wafregional.Client) error {
for _, IPSet := range output.IPSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*IPSet.IPSetId,
*IPSet.Name,
*IPSet.Name+"_"+(*IPSet.IPSetId)[0:8],
"aws_wafregional_ipset",
"aws",
wafAllowEmptyValues))
Expand All @@ -145,7 +145,7 @@ func (g *WafRegionalGenerator) loadRateBasedRules(svc *wafregional.Client) error
for _, rule := range output.Rules {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*rule.RuleId,
*rule.Name,
*rule.Name+"_"+(*rule.RuleId)[0:8],
"aws_wafregional_rate_based_rule",
"aws",
wafAllowEmptyValues))
Expand All @@ -161,7 +161,7 @@ func (g *WafRegionalGenerator) loadRegexMatchSets(svc *wafregional.Client) error
for _, regexMatchSet := range output.RegexMatchSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*regexMatchSet.RegexMatchSetId,
*regexMatchSet.Name,
*regexMatchSet.Name+"_"+(*regexMatchSet.RegexMatchSetId)[0:8],
"aws_wafregional_regex_match_set",
"aws",
wafAllowEmptyValues))
Expand All @@ -177,7 +177,7 @@ func (g *WafRegionalGenerator) loadRegexPatternSets(svc *wafregional.Client) err
for _, regexPatternSet := range output.RegexPatternSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*regexPatternSet.RegexPatternSetId,
*regexPatternSet.Name,
*regexPatternSet.Name+"_"+(*regexPatternSet.RegexPatternSetId)[0:8],
"aws_wafregional_regex_pattern_set",
"aws",
wafAllowEmptyValues))
Expand All @@ -193,7 +193,7 @@ func (g *WafRegionalGenerator) loadWafRules(svc *wafregional.Client) error {
for _, rule := range output.Rules {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*rule.RuleId,
*rule.Name,
*rule.Name+"_"+(*rule.RuleId)[0:8],
"aws_wafregional_rule",
"aws",
wafAllowEmptyValues))
Expand All @@ -209,7 +209,7 @@ func (g *WafRegionalGenerator) loadWafRuleGroups(svc *wafregional.Client) error
for _, ruleGroup := range output.RuleGroups {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*ruleGroup.RuleGroupId,
*ruleGroup.Name,
*ruleGroup.Name+"_"+(*ruleGroup.RuleGroupId)[0:8],
"aws_wafregional_rule_group",
"aws",
wafAllowEmptyValues))
Expand All @@ -225,7 +225,7 @@ func (g *WafRegionalGenerator) loadSizeConstraintSets(svc *wafregional.Client) e
for _, sizeConstraintSet := range output.SizeConstraintSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*sizeConstraintSet.SizeConstraintSetId,
*sizeConstraintSet.Name,
*sizeConstraintSet.Name+"_"+(*sizeConstraintSet.SizeConstraintSetId)[0:8],
"aws_wafregional_size_constraint_set",
"aws",
wafAllowEmptyValues))
Expand All @@ -241,7 +241,7 @@ func (g *WafRegionalGenerator) loadSqlInjectionMatchSets(svc *wafregional.Client
for _, sqlInjectionMatchSet := range output.SqlInjectionMatchSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*sqlInjectionMatchSet.SqlInjectionMatchSetId,
*sqlInjectionMatchSet.Name,
*sqlInjectionMatchSet.Name+"_"+(*sqlInjectionMatchSet.SqlInjectionMatchSetId)[0:8],
"aws_wafregional_sql_injection_match_set",
"aws",
wafAllowEmptyValues))
Expand All @@ -257,7 +257,7 @@ func (g *WafRegionalGenerator) loadXssMatchSet(svc *wafregional.Client) error {
for _, xssMatchSet := range output.XssMatchSets {
g.Resources = append(g.Resources, terraform_utils.NewSimpleResource(
*xssMatchSet.XssMatchSetId,
*xssMatchSet.Name,
*xssMatchSet.Name+"_"+(*xssMatchSet.XssMatchSetId)[0:8],
"aws_wafregional_xss_match_set",
"aws",
wafAllowEmptyValues))
Expand Down
15 changes: 13 additions & 2 deletions terraform_utils/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"regexp"
"strings"
)

Expand All @@ -15,8 +16,18 @@ func jsonPrint(data interface{}) ([]byte, error) {
}
// We don't need to escape > or <
s := strings.Replace(string(dataJsonBytes), "\\u003c", "<", -1)
s = strings.Replace(s, "\\<", "<", -1) // fix broken double escaping
s = regexp.MustCompile(`.?\\<`).ReplaceAllStringFunc(s, escapingBackslashReplacer("<"))
s = strings.Replace(s, "\\u003e", ">", -1)
s = strings.Replace(s, "\\>", ">", -1) // fix broken double escaping
s = regexp.MustCompile(`.?\\>`).ReplaceAllStringFunc(s, escapingBackslashReplacer(">"))
return []byte(s), nil
}

func escapingBackslashReplacer(backslashedCharacter string) func(string) string {
return func(match string) string {
if strings.HasPrefix(match, "\\\\") {
return match // Don't replace regular backslashes
} else {
return strings.Replace(match, "\\"+backslashedCharacter, backslashedCharacter, 1)
}
}
}

0 comments on commit 8921ac7

Please sign in to comment.