Skip to content

Fix: escape properties in regexp pattern generation #1231

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

didierdemoniere
Copy link

@didierdemoniere didierdemoniere commented Apr 18, 2025

Hi,

In some scenarios properties with special chars (eg: $limit or $skip) are evaluated in the regexp pattern generated by KeyOfPattern.

it doesn't always happen because KeyOfPattern is not used in every cases, but it happen in the following case for example:

test('should pass but fail', () => {
    const value = { 
      $like: 'foo', 
      $not: { $like: 'bar' } 
    };

    const schema = t.Recursive((t1) => {
      return t.Intersect([
        t.Object({ 
          $or: t.Optional(t.Array(t1))
        }),
        t.Recursive(t2 => {
          return t.Object({ 
            $not: t.Optional(t2),
            $like: t.Optional(t.String()) 
          })
        })
      ], { unevaluatedProperties: false });
    });

    expect(Value.Errors(schema, value).First()).toEqual(undefined);
  });

  test('workaround pass', () => {
    const value = { 
      $like: 'foo', 
      $not: { $like: 'bar' } 
    };

    const schema2 = t.Recursive((t1) => {
      return t.Intersect([
        t.Object({ 
          ['\\$or']: t.Optional(t.Array(t1))
        }),
        t.Recursive(t2 => {
          return t.Object({ 
            ['\\$not']: t.Optional(t2),
            ['\\$like']: t.Optional(t.String()) 
          })
        })
      ], { unevaluatedProperties: false });
    });

    expect(Value.Errors(schema2, value).First()).toEqual(undefined);
  });

this PR just escape all properties to prevent regexp evaluation of special chars.

didierdemoniere added a commit to didierdemoniere/criterium that referenced this pull request Apr 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants