diff --git a/README.md b/README.md index 8685b3f..5f6e160 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ go run github.com/pb33f/libopenapi-validator/cmd/validate@latest --regexengine=e - ignorecase - multiline - explicitcapture -- compiled +- compiled (legacy no-op, equivalent to none) - singleline - ignorepatternwhitespace - righttoleft diff --git a/arazzo/expressions.go b/arazzo/expressions.go index 53107d2..64123a4 100644 --- a/arazzo/expressions.go +++ b/arazzo/expressions.go @@ -10,7 +10,7 @@ import ( "strconv" "strings" - "github.com/dlclark/regexp2" + "github.com/dlclark/regexp2/v2" "github.com/pb33f/jsonpath/pkg/jsonpath" jsonpathconfig "github.com/pb33f/jsonpath/pkg/jsonpath/config" "github.com/pb33f/libopenapi/arazzo/expression" diff --git a/arazzo/inputs.go b/arazzo/inputs.go index 88757f6..8f1a546 100644 --- a/arazzo/inputs.go +++ b/arazzo/inputs.go @@ -12,7 +12,7 @@ import ( "strings" "time" - "github.com/dlclark/regexp2" + "github.com/dlclark/regexp2/v2" "github.com/pb33f/jsonschema/v6" ) diff --git a/arazzo/regex_security_test.go b/arazzo/regex_security_test.go new file mode 100644 index 0000000..55ef3bb --- /dev/null +++ b/arazzo/regex_security_test.go @@ -0,0 +1,45 @@ +// Copyright 2023-2026 Princess Beef Heavy Industries, LLC / Dave Shanley +// SPDX-License-Identifier: MIT + +package arazzo + +import ( + "errors" + "testing" + + "github.com/dlclark/regexp2/v2" +) + +func TestInputRegexZeroWidthRepeatLimit(t *testing.T) { + // 60,000 repetitions exceed the default 100,000-slot backtracking limit + // while keeping this regression safe to run against the old engine. + for _, pattern := range []string{`(?:^){60000}`, `(?:^){60000}?`} { + t.Run(pattern, func(t *testing.T) { + v := contractValidation(nil) + state := &inputRegexpState{v: v} + re, err := state.compile(pattern) + if err != nil { + t.Fatal(err) + } + if re.MatchString("") || !errors.Is(state.err, regexp2.ErrBacktrackingStackLimit) { + t.Fatalf("expected bounded match failure, got %v", state.err) + } + reportInputCompile(v, state, state.err, "/workflows/0/inputs") + var tool *Error + if !errors.As(v.err, &tool) || tool.Kind != ErrorOperational || !errors.Is(v.err, regexp2.ErrBacktrackingStackLimit) { + t.Fatalf("regex limit cause was lost: %v", v.err) + } + }) + } +} + +func TestInputRegexEmptyRepeat(t *testing.T) { + state := &inputRegexpState{v: contractValidation(nil)} + re, err := state.compile(`(?:){60000}`) + if err != nil { + t.Fatal(err) + } + if !re.MatchString("") || state.err != nil { + t.Fatalf("literal empty repetition must remain valid: %v", state.err) + } +} diff --git a/arazzo/schema.go b/arazzo/schema.go index 915b615..c6130ba 100644 --- a/arazzo/schema.go +++ b/arazzo/schema.go @@ -97,7 +97,7 @@ type nonDollarPattern struct{} func (nonDollarPattern) String() string { return `^(?!\$).+$` } func (nonDollarPattern) MatchString(s string) bool { - return s != "" && s[0] != '$' && !strings.ContainsAny(s, "\r\n") + return s != "" && s[0] != '$' && !strings.ContainsAny(s, "\r\n\u2028\u2029") } func compileSchemaPattern(pattern string) (jsonschema.Regexp, error) { diff --git a/arazzo/validate_test.go b/arazzo/validate_test.go index 22e7b07..b30f70c 100644 --- a/arazzo/validate_test.go +++ b/arazzo/validate_test.go @@ -17,7 +17,7 @@ import ( "sync/atomic" "testing" - "github.com/dlclark/regexp2" + "github.com/dlclark/regexp2/v2" "github.com/pb33f/go-yaml" ) diff --git a/cmd/validate/main.go b/cmd/validate/main.go index 346a941..e4059be 100644 --- a/cmd/validate/main.go +++ b/cmd/validate/main.go @@ -1,3 +1,6 @@ +// Copyright 2023-2026 Princess Beef Heavy Industries, LLC / Dave Shanley +// SPDX-License-Identifier: MIT + package main import ( @@ -7,7 +10,7 @@ import ( "log/slog" "os" - "github.com/dlclark/regexp2" + "github.com/dlclark/regexp2/v2" "github.com/goccy/go-yaml" "github.com/pb33f/jsonschema/v6" "github.com/pb33f/libopenapi" @@ -28,7 +31,7 @@ func (re *customRegexp) String() string { } type regexEngine struct { - runtimeOption regexp2.RegexOptions + runtimeOption regexp2.CompileOption } func (e *regexEngine) run(s string) (jsonschema.Regexp, error) { @@ -39,16 +42,16 @@ func (e *regexEngine) run(s string) (jsonschema.Regexp, error) { return (*customRegexp)(re), nil } -var regexParsingOptionsMap = map[string]regexp2.RegexOptions{ +var regexParsingOptionsMap = map[string]regexp2.CompileOption{ "none": regexp2.None, "ignorecase": regexp2.IgnoreCase, "multiline": regexp2.Multiline, "explicitcapture": regexp2.ExplicitCapture, - "compiled": regexp2.Compiled, + "compiled": regexp2.None, // Legacy no-op; regexp2/v2 removed the unused flag. "singleline": regexp2.Singleline, "ignorepatternwhitespace": regexp2.IgnorePatternWhitespace, "righttoleft": regexp2.RightToLeft, - "debug": regexp2.Debug, + "debug": regexp2.OptionDebug(), "ecmascript": regexp2.ECMAScript, "re2": regexp2.RE2, "unicode": regexp2.Unicode, diff --git a/cmd/validate/main_test.go b/cmd/validate/main_test.go new file mode 100644 index 0000000..ca3feea --- /dev/null +++ b/cmd/validate/main_test.go @@ -0,0 +1,42 @@ +// Copyright 2023-2026 Princess Beef Heavy Industries, LLC / Dave Shanley +// SPDX-License-Identifier: MIT + +package main + +import "testing" + +func TestRegexEngineOptions(t *testing.T) { + for name, option := range regexParsingOptionsMap { + t.Run(name, func(t *testing.T) { + engine := ®exEngine{runtimeOption: option} + re, err := engine.run("abc") + if err != nil { + t.Fatal(err) + } + if re.String() != "abc" || !re.MatchString("abc") || re.MatchString("xyz") { + t.Fatal("regex option changed matching behavior") + } + if _, err := engine.run("["); err == nil { + t.Fatal("invalid regex compiled") + } + }) + } +} + +func TestRegexEngineZeroWidthRepeatLimit(t *testing.T) { + engine := ®exEngine{runtimeOption: regexParsingOptionsMap["ecmascript"]} + re, err := engine.run(`(?:^){60000}`) + if err != nil { + t.Fatal(err) + } + if re.MatchString("") { + t.Fatal("excessive zero-width repetition must fail closed") + } + empty, err := engine.run(`(?:){60000}`) + if err != nil { + t.Fatal(err) + } + if !empty.MatchString("") { + t.Fatal("literal empty repetition must remain valid") + } +} diff --git a/go.mod b/go.mod index cd88730..059fb0c 100644 --- a/go.mod +++ b/go.mod @@ -4,21 +4,21 @@ go 1.26.0 require ( github.com/basgys/goxml2json v1.1.1-0.20231018121955-e66ee54ceaad - github.com/dlclark/regexp2 v1.12.0 + github.com/dlclark/regexp2/v2 v2.12.0 github.com/go-openapi/jsonpointer v1.0.2 github.com/goccy/go-yaml v1.19.2 github.com/pb33f/go-yaml v0.1.1 - github.com/pb33f/jsonpath v0.8.4 + github.com/pb33f/jsonpath v0.8.5 github.com/pb33f/jsonschema/v6 v6.0.3 - github.com/pb33f/libopenapi v0.41.6 + github.com/pb33f/libopenapi v0.41.8 github.com/pb33f/testify v0.1.1 golang.org/x/text v0.43.0 ) require ( github.com/bahlo/generic-list-go v0.2.0 // indirect - github.com/buger/jsonparser v1.1.2 // indirect + github.com/buger/jsonparser v1.6.1 // indirect github.com/pb33f/ordered-map/v2 v2.3.2 // indirect - golang.org/x/net v0.50.0 // indirect + golang.org/x/net v0.62.0 // indirect golang.org/x/sync v0.24.0 // indirect ) diff --git a/go.sum b/go.sum index 76d1307..bf44035 100644 --- a/go.sum +++ b/go.sum @@ -4,13 +4,15 @@ github.com/basgys/goxml2json v1.1.1-0.20231018121955-e66ee54ceaad h1:3swAvbzgfaI github.com/basgys/goxml2json v1.1.1-0.20231018121955-e66ee54ceaad/go.mod h1:9+nBLYNWkvPcq9ep0owWUsPTLgL9ZXTsZWcCSVGGLJ0= github.com/bitly/go-simplejson v0.5.1 h1:xgwPbetQScXt1gh9BmoJ6j9JMr3TElvuIyjR8pgdoow= github.com/bitly/go-simplejson v0.5.1/go.mod h1:YOPVLzCfwK14b4Sff3oP1AmGhI9T9Vsg84etUnlyp+Q= -github.com/buger/jsonparser v1.1.2 h1:frqHqw7otoVbk5M8LlE/L7HTnIq2v9RX6EJ48i9AxJk= -github.com/buger/jsonparser v1.1.2/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= +github.com/buger/jsonparser v1.6.1 h1:I0phFv0PlbLHnM7TZAVjZ2MJ2/eWRTDyuO7GLR98IEs= +github.com/buger/jsonparser v1.6.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dlclark/regexp2 v1.12.0 h1:0j4c5qQmnC6XOWNjP3PIXURXN2gWx76rd3KvgdPkCz8= -github.com/dlclark/regexp2 v1.12.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= +github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/dlclark/regexp2/v2 v2.12.0 h1:GsCgDZZeHluUDHASr2yqQonZk4Tma5LFmLH9CSsFFEE= +github.com/dlclark/regexp2/v2 v2.12.0/go.mod h1:avUrQvPaLz2DrFNHJF0taWAFFX2C1GMSSoeiqFjcBmU= github.com/go-openapi/jsonpointer v1.0.2 h1:NbW7xT2b+l7x+JefiPGDsZWsrLk1ogrz4AHK1TTzMt8= github.com/go-openapi/jsonpointer v1.0.2/go.mod h1:N0rI4MbNKD7xAFTiHtB+5Nhrg2j/wwYg7m7Fmb4/jk8= github.com/go-openapi/testify/v2 v2.8.0 h1:19QDx5b57p8KjO2E4sgs/woL4Akp+FGLRlVm2xn0Rho= @@ -19,12 +21,12 @@ github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/pb33f/go-yaml v0.1.1 h1:yWGmhVdwzionRnSc/hEJwcfq6PaZL2QdYzyReXBbX7Q= github.com/pb33f/go-yaml v0.1.1/go.mod h1:QvWdkHP5VjFYAeHMQUkaPQqREaQnBNOXmWzAjNrnSdo= -github.com/pb33f/jsonpath v0.8.4 h1:Yx7fxsKl8scgERL22WhKDs2c6gYNNUonPSWEG8Y194s= -github.com/pb33f/jsonpath v0.8.4/go.mod h1:eO6mgdhw5RgzCxm/bXTi75VUs0/cCYJzdGjMCCUEI6M= +github.com/pb33f/jsonpath v0.8.5 h1:wsbwJbz6MPUTy5fAtiIlOyQb3oblaa5M7qjtdz6orPw= +github.com/pb33f/jsonpath v0.8.5/go.mod h1:eO6mgdhw5RgzCxm/bXTi75VUs0/cCYJzdGjMCCUEI6M= github.com/pb33f/jsonschema/v6 v6.0.3 h1:TMIkE1xCmnF8Cy+anIBs+r4ASfGtwzfFxYsrrIn0wSU= github.com/pb33f/jsonschema/v6 v6.0.3/go.mod h1:cYPShw+w1Jrs0YgpE2rk1FLdIAsKfWcotv/54G6ddFo= -github.com/pb33f/libopenapi v0.41.6 h1:W1X/5rOaAk3/Mjwrt+DHaiJO6buhruKWrbQ+pumONkA= -github.com/pb33f/libopenapi v0.41.6/go.mod h1:mUldESjO6ownR1TCcywvoYp7e4v/bCdQyiiIqsgWne0= +github.com/pb33f/libopenapi v0.41.8 h1:SCqrRbHNodEJjyLRFhvUkLyjQ1H+1W0fxhR+oCslRis= +github.com/pb33f/libopenapi v0.41.8/go.mod h1:IlfeXtmcY38KEnGJ4OMWFA8I62BCNamuQa8omLnk6R0= github.com/pb33f/ordered-map/v2 v2.3.2 h1:wDyaZ2Pv9QLh64X4utCeD5Zoi9nIv2aW3lwv172O5PQ= github.com/pb33f/ordered-map/v2 v2.3.2/go.mod h1:1OhFrXu3OYw3kM+FXF+Ug3ugmmZ9LE8z0JfQMLvtV0w= github.com/pb33f/testify v0.1.1 h1:mnHe7uxKt8dyNYEGUspow72VjD264DB5rOJq4bz5tJw= @@ -47,8 +49,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.13.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA= -golang.org/x/net v0.50.0 h1:ucWh9eiCGyDR3vtzso0WMQinm2Dnt8cFMuQa9K33J60= -golang.org/x/net v0.50.0/go.mod h1:UgoSli3F/pBgdJBHCTc+tp3gmrU4XswgGRgtnwWTfyM= +golang.org/x/net v0.62.0 h1:18ELalfM5+XNRwzM2nYqXsZQ9ByS1uhj2O/U7+5jqRY= +golang.org/x/net v0.62.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/validator_test.go b/validator_test.go index 6b1602a..3ba0b3a 100644 --- a/validator_test.go +++ b/validator_test.go @@ -17,7 +17,7 @@ import ( "testing" "unicode" - "github.com/dlclark/regexp2" + "github.com/dlclark/regexp2/v2" "github.com/pb33f/jsonschema/v6" "github.com/pb33f/libopenapi" "github.com/pb33f/testify/assert"