-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from hashicorp/check-valid-regexp-pattern
Only sanitize invalid `pattern` regexes
- Loading branch information
Showing
3 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,57 @@ | ||
package cfschema | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var ( | ||
sanitizePattern = regexp.MustCompile(`(?m)^(\s+"pattern"\s*:\s*)".*"`) | ||
sanitizePattern = regexp.MustCompile(`^(\s*"pattern"\s*:\s*)"(.*)"(\s*,?\s*)$`) | ||
sanitizePatternProperties = regexp.MustCompile(`(?m)^(\s+"patternProperties"\s*:\s*{\s*)".*?"`) | ||
) | ||
|
||
// Sanitize returns a sanitized copy of the specified JSON Schema document. | ||
// The santized copy rewrites all pattern and patternProperty regexes to the empty string, | ||
// working around any problems with JSON Schema regex validation. | ||
func Sanitize(document string) string { | ||
document = sanitizePattern.ReplaceAllString(document, `$1""`) | ||
// The sanitized copy works around any problems with JSON Schema regex validation by | ||
// - Rewriting all patternProperty regexes to the empty string (the regex is never used anyway) | ||
// - Rewriting all unsupported (valid for ECMA-262 but not for Go) pattern regexes to the empty string | ||
func Sanitize(document string) (string, error) { | ||
document = sanitizePatternProperties.ReplaceAllString(document, `$1""`) | ||
|
||
return document | ||
var sb strings.Builder | ||
scanner := bufio.NewScanner(strings.NewReader(document)) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
|
||
if v := sanitizePattern.FindStringSubmatch(line); len(v) == 4 { | ||
if expr := v[2]; expr != "" && !isSupportedRegexp(expr) { | ||
line = v[1] + "\"\"" + v[3] | ||
} | ||
} | ||
if _, err := sb.WriteString(line); err != nil { | ||
return "", err | ||
} | ||
if _, err := sb.WriteString("\n"); err != nil { | ||
return "", err | ||
} | ||
} | ||
if err := scanner.Err(); err != nil { | ||
return "", err | ||
} | ||
|
||
return sb.String(), nil | ||
} | ||
|
||
func isSupportedRegexp(expr string) bool { | ||
// github.com/xeipuuv/gojsonschema attempts to compile the regex after it has been unmarshaled from JSON. | ||
var v string | ||
b := []byte("\"" + expr + "\"") | ||
|
||
if err := json.Unmarshal(b, &v); err != nil { | ||
return false | ||
} | ||
|
||
_, err := regexp.Compile(v) | ||
return err == nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters