santhosh-tekuri / jsonschema

JSONSchema (draft 2020-12, draft 2019-09, draft-7, draft-6, draft-4) Validation using Go
Apache License 2.0
957 stars 98 forks source link

False positive error for a valid regex pattern #113

Closed altego371 closed 1 year ago

altego371 commented 1 year ago

The package returns a false positive error for a valid regex pattern in a schema. Pattern .{1,1024}$ is valid, but CompileString method returns an error. Empirically established that an error is returned for any value greater than 1000. For a value less than or equal to 1000, the error does not occur. The package version is 5.2.0 The code below shows the bug

package bug

import (
    "github.com/santhosh-tekuri/jsonschema/v5"
    "github.com/stretchr/testify/assert"
    "strings"
    "testing"
)

func TestBugInJsonschemaPackage(t *testing.T) {
    validSchema := `
{
    "type": "object",
    "properties": {
        "description": {"type": "string", "pattern": ".{1,1024}$"}
    }
}
`
    _, falsePositiveError := jsonschema.CompileString("", validSchema)
    assert.ErrorContains(t, falsePositiveError, `'.{1,1024}$' is not valid 'regex'`)

    updatedSchema := strings.Replace(validSchema, `".{1,1024}$"`, `".{1,1000}$"`, 1)
    _, err := jsonschema.CompileString("", updatedSchema)
    assert.NoError(t, err)

}
santhosh-tekuri commented 1 year ago

WONT_FIX

this package uses golang regexp package which has this limitation.

https://github.com/google/re2/wiki/Syntax clearly says:

Implementation restriction: The counting forms x{n,m}, x{n,}, and x{n} reject forms
that create a minimum or maximum repetition count above 1000. 
Unlimited repetitions are not subject to this restriction.