Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/fuzzy-store-variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli': patch
---

Reject invalid variable values before store execute requests are sent.
41 changes: 41 additions & 0 deletions packages/store/src/cli/services/store/execute/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ describe('prepareStoreExecuteRequest', () => {
expect(request.parsedOperation.operationDefinition.operation).toBe('mutation')
})

test.each(['null', '[]', '"value"', 'true', '42'])('throws when inline variables are %s', async (variables) => {
await expect(
prepareStoreExecuteRequest({
query: 'query { shop { name } }',
variables,
}),
).rejects.toThrow('expected a non-null JSON object')
})

test('throws when inline variables are empty', async () => {
await expect(
prepareStoreExecuteRequest({
query: 'query { shop { name } }',
variables: ' ',
}),
).rejects.toThrow('--variables')
})

test('throws when variables contain invalid JSON', async () => {
await expect(
prepareStoreExecuteRequest({
Expand All @@ -119,6 +137,29 @@ describe('prepareStoreExecuteRequest', () => {
).rejects.toThrow('Invalid JSON')
})

test.each(['null', '[]', '"value"', 'true', '42'])('throws when file variables are %s', async (variables) => {
await inTemporaryDirectory(async (tmpDir) => {
const variableFile = joinPath(tmpDir, 'variables.json')
await writeFile(variableFile, variables)

await expect(
prepareStoreExecuteRequest({
query: 'query { shop { name } }',
variableFile,
}),
).rejects.toThrow('expected a non-null JSON object')
})
})

test('accepts an empty variable object', async () => {
await expect(
prepareStoreExecuteRequest({
query: 'query { shop { name } }',
variables: '{}',
}),
).resolves.toMatchObject({parsedVariables: {}})
})

test('reads variables from a file', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
Expand Down
29 changes: 26 additions & 3 deletions packages/store/src/cli/services/store/execute/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,36 @@ async function readQuery(input: {query?: string; queryFile?: string}): Promise<s
)
}

function validateVariables(
value: unknown,
source: ReturnType<typeof outputToken.yellow> | ReturnType<typeof outputToken.path>,
): {[key: string]: unknown} {
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
throw new AbortError(
outputContent`Invalid variables in ${source}: expected a non-null JSON object.`,
'Please provide a JSON object for variables.',
)
}

return value as {[key: string]: unknown}
}

async function parseVariables(
variables?: string,
variableFile?: string,
): Promise<{[key: string]: unknown} | undefined> {
if (variables) {
if (variables !== undefined) {
if (!variables.trim()) {
throw new AbortError(
outputContent`The ${outputToken.yellow('--variables')} flag value is empty.`,
'Please provide a non-null JSON object for variables.',
)
}

try {
return JSON.parse(variables)
return validateVariables(JSON.parse(variables), outputToken.yellow('--variables'))
} catch (error) {
if (error instanceof AbortError) throw error
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
throw new AbortError(
outputContent`Invalid JSON in ${outputToken.yellow('--variables')} flag: ${errorMessage}`,
Expand All @@ -72,8 +94,9 @@ async function parseVariables(

const fileContent = await readFile(variableFile, {encoding: 'utf8'})
try {
return JSON.parse(fileContent)
return validateVariables(JSON.parse(fileContent), outputToken.path(variableFile))
} catch (error) {
if (error instanceof AbortError) throw error
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
throw new AbortError(
outputContent`Invalid JSON in variable file ${outputToken.path(variableFile)}: ${errorMessage}`,
Expand Down
Loading