Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile pattern on validate #375

Merged
merged 9 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ jobs:
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]

- run: go test ./...
- run: go test -v -run TestRaceyPatternSchema -race ./...
env:
CGO_ENABLED: '1'
- run: |
cd openapi3/testdata
go get -u -v github.com/getkin/kin-openapi
Expand Down
26 changes: 26 additions & 0 deletions openapi3/race_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package openapi3_test

import (
"context"
"testing"

"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
)

func TestRaceyPatternSchema(t *testing.T) {
schema := openapi3.Schema{
Pattern: "^test|for|race|condition$",
}

err := schema.Validate(context.Background())
require.NoError(t, err)

visit := func() {
err := schema.VisitJSONString("test")
require.NoError(t, err)
}

go visit()
visit()
}
27 changes: 20 additions & 7 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,11 @@ func (schema *Schema) validate(ctx context.Context, stack []*Schema) (err error)
case "byte", "binary", "date", "date-time", "password":
// In JSON Draft-07 (not validated yet though):
case "regex":
if !SchemaFormatValidationDisabled {
if err = schema.compilePattern(); err != nil {
fenollp marked this conversation as resolved.
Show resolved Hide resolved
return err
}
}
case "time", "email", "idn-email":
case "hostname", "idn-hostname", "ipv4", "ipv6":
case "uri", "uri-reference", "iri", "iri-reference", "uri-template":
Expand Down Expand Up @@ -1141,13 +1146,7 @@ func (schema *Schema) visitJSONString(settings *schemaValidationSettings, value
// "pattern"
if pattern := schema.Pattern; pattern != "" && schema.compiledPattern == nil {
var err error
if schema.compiledPattern, err = regexp.Compile(pattern); err != nil {
err = &SchemaError{
Value: value,
Schema: schema,
SchemaField: "pattern",
Reason: fmt.Sprintf("cannot compile pattern %q: %v", pattern, err),
}
if err = schema.compilePattern(); err != nil {
if !settings.multiError {
return err
}
Expand Down Expand Up @@ -1461,6 +1460,20 @@ func (schema *Schema) expectedType(settings *schemaValidationSettings, typ strin
}
}

func (schema *Schema) compilePattern() (err error) {
if schema.Pattern == "" {
return nil
}
if schema.compiledPattern, err = regexp.Compile(schema.Pattern); err != nil {
return &SchemaError{
Schema: schema,
SchemaField: "pattern",
Reason: fmt.Sprintf("cannot compile pattern %q: %v", schema.Pattern, err),
}
}
return nil
}

type SchemaError struct {
Value interface{}
reversePath []string
Expand Down