Skip to content

Commit

Permalink
fix(core): String in query values should be escaped (#2891)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbcfd authored Aug 11, 2023
1 parent cb82115 commit 6bd02d7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ describe('Should convert query filters', () => {
`select '${DATA_FIELD}' from 'test' where (((cast(${DATA_FIELD}->>'a' as numeric)=1.2)))`
)
})
test('that are composed of a single doc filter with in string query', () => {
const query = createQuery({
type: 'where',
value: {
a: { type: 'string', op: 'in', value: ['a', 'b'] },
},
})

expect(query).toEqual(
`select '${DATA_FIELD}' from 'test' where ((cast(${DATA_FIELD}->>'a' as varchar) in ('a','b')))`
)
})
test('that are composed of a single doc filter with null', () => {
const query = createQuery({
type: 'where',
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/indexing/__tests__/query-filter-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ describe('Should parse query filters', () => {
},
})
})
test('that are composed of a single in query', () => {
const parsed = parseQueryFilters({
where: {
a: { in: ['b', 'c'] },
},
})
expect(parsed).toEqual({
type: 'where',
value: {
a: { type: 'string', op: 'in', value: ['b', 'c'] },
},
})
})
test('that are composed of a null query', () => {
const parsed = parseQueryFilters({
where: {
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/indexing/query-filter-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,13 @@ function handleIn<T extends number | string>(
negated: boolean,
combinator?: Combinator
): DBQuery {
const arrValue = value.map((v) => v.toString()).join(',')
const cast = nonBooleanTypeAsCast(tpe)
let arrValue
if (cast == 'varchar') {
arrValue = value.map((v) => `'${v}'`).join(',')
} else {
arrValue = value.map((v) => `${v}`).join(',')
}
const inner = (bldr) => {
let op = ' in '
if (negated) {
Expand Down
4 changes: 3 additions & 1 deletion packages/http-client/src/dummy-pin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { PinApi, PublishOpts } from '@ceramicnetwork/common'
import { StreamID } from '@ceramicnetwork/streamid'

function warn(operation: string) {
console.warn(`You are using the ceramic.pin.${operation} API which has been removed and is now a no-op. This operation will not have any affect. If you want to change the pin state of streams please use the new ceramic.admin.pin API which requires a DID that has been granted admin access on the Ceramic node.`)
console.warn(
`You are using the ceramic.pin.${operation} API which has been removed and is now a no-op. This operation will not have any affect. If you want to change the pin state of streams please use the new ceramic.admin.pin API which requires a DID that has been granted admin access on the Ceramic node.`
)
}

export class DummyPinApi implements PinApi {
Expand Down
20 changes: 20 additions & 0 deletions packages/stream-tests/src/__tests__/basic-indexing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,26 @@ describe.each(envs)('Basic end-to-end indexing query test for $dbEngine', (env)
expect(results.length).toEqual(1)
expect(JSON.stringify(results[0].content)).toEqual(JSON.stringify(doc4.content))
})
test('Can query documents by string in array', async () => {
const doc1 = await ModelInstanceDocument.create(ceramic, CONTENT0, midMetadata)
const doc2 = await ModelInstanceDocument.create(ceramic, CONTENT1, midMetadata)
const doc3 = await ModelInstanceDocument.create(ceramic, CONTENT3, midMetadata)
const doc4 = await ModelInstanceDocument.create(ceramic, CONTENT5, midMetadata)
const doc5 = await ModelInstanceDocument.create(ceramic, CONTENT6, midMetadata)

const resultObj0 = await ceramic.index.query({
model: model.id,
last: 2,
queryFilters: {
where: { myString: { in: ['a', 'c'] } },
},
})

const results = extractDocuments(ceramic, resultObj0)
expect(results.length).toEqual(2)
expect(JSON.stringify(results[0].content)).toEqual(JSON.stringify(doc2.content))
expect(JSON.stringify(results[1].content)).toEqual(JSON.stringify(doc4.content))
})
test('Can query multiple documents by field', async () => {
const doc1 = await ModelInstanceDocument.create(ceramic, CONTENT0, midMetadata)
const doc2 = await ModelInstanceDocument.create(ceramic, CONTENT2, midMetadata)
Expand Down

0 comments on commit 6bd02d7

Please sign in to comment.