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

fix(core): avoid attribute names being scanned as symbols. Fixes #5673 #6624

Merged
merged 6 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions packages/@sanity/mutator/src/jsonpath/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ class Tokenizer {
return null
}
if (str === this.source.slice(this.i, this.i + str.length)) {
// When checking symbols that consist of valid attribute characters, we
// need to make sure we don't inadvertently treat an attribute as a
// symbol. For example, an attribute 'trueCustomerField' should not be
// scanned as the boolean symbol "true".
if (str[0].match(attributeCharMatcher)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we know what the perf hit is on looking ahead on each match?

Copy link
Member Author

@runeb runeb May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would only do this extra check when it encounters a symbol match which is all attribute-valid chars, meaning "true" and "false"

// check that char following the symbol match is not also an attribute char
if (this.length > this.i + str.length) {
const nextChar = this.source[this.i + str.length]
if (nextChar && nextChar.match(attributeCharMatcher)) {
return null
}
}
}
this.i += str.length
return str
}
Expand Down
4 changes: 4 additions & 0 deletions packages/@sanity/mutator/test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ const cases = {
},
],
},
trueOrFalseField: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a test that fails if this name is just true or false to make sure we didn't just break something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added explicit tests for parsing symbols versus attributes/identifiers

type: 'attribute',
name: 'trueOrFalseField',
},
}

Object.keys(cases).forEach((path) => {
Expand Down
32 changes: 32 additions & 0 deletions packages/@sanity/mutator/test/tokenize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,38 @@ const cases = {
type: 'paren',
},
],
trueish: [
{
type: 'identifier',
name: 'trueish',
},
],
'trueOrFalse[trueField == true]': [
{
type: 'identifier',
name: 'trueOrFalse',
},
{
type: 'paren',
symbol: '[',
},
{
type: 'identifier',
name: 'trueField',
},
{
type: 'comparator',
symbol: '==',
},
{
symbol: 'true',
type: 'boolean',
},
{
type: 'paren',
symbol: ']',
},
],
}

test('Tokenization of jsonpath', () => {
Expand Down
Loading