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

Don't allow editing trigger data for row action automations #14202

Merged
merged 6 commits into from
Jul 22, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@
options={value.enum}
getOptionLabel={(x, idx) =>
value.pretty ? value.pretty[idx] : x}
disabled={value.readonly}
/>
{:else if value.type === "json"}
<Editor
Expand All @@ -877,13 +878,15 @@
mode="json"
value={inputData[key]?.value}
on:change={e => onChange({ [key]: e.detail })}
readOnly={value.readonly}
/>
{:else if value.type === "boolean"}
<div style="margin-top: 10px">
<Checkbox
text={value.title}
value={inputData[key]}
on:change={e => onChange({ [key]: e.detail })}
disabled={value.readonly}
/>
</div>
{:else if value.type === "date"}
Expand All @@ -897,6 +900,7 @@
allowJS={true}
updateOnChange={false}
drawerLeft="260px"
disabled={value.readonly}
>
<DatePicker
value={inputData[key]}
Expand All @@ -908,6 +912,7 @@
on:change={e => onChange({ [key]: e.detail })}
value={inputData[key]}
options={Object.keys(table?.schema || {})}
disabled={value.readonly}
/>
{:else if value.type === "attachment" || value.type === "signature_single"}
<div class="attachment-field-wrapper">
Expand Down Expand Up @@ -1021,6 +1026,7 @@
{isTrigger}
value={inputData[key]}
on:change={e => onChange({ [key]: e.detail })}
disabled={value.readonly}
/>
{:else if value.customType === "webhookUrl"}
<WebhookDisplay value={inputData[key]} />
Expand Down
4 changes: 3 additions & 1 deletion packages/builder/src/stores/builder/automations.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ const automationActions = store => ({
if (!automation) {
return
}
delete newAutomation.definition.stepNames[blockId]
if (newAutomation.definition.stepNames) {
delete newAutomation.definition.stepNames[blockId]
}

await store.actions.save(newAutomation)
},
Expand Down
30 changes: 30 additions & 0 deletions packages/server/src/sdk/app/automations/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ export async function update(automation: Automation) {
const db = getDb()

const oldAutomation = await db.get<Automation>(automation._id)

guardInvalidUpdatesAndThrow(automation, oldAutomation)

automation = cleanAutomationInputs(automation)
automation = await checkForWebhooks({
oldAuto: oldAutomation,
Expand Down Expand Up @@ -251,3 +254,30 @@ async function checkForWebhooks({ oldAuto, newAuto }: any) {
}
return newAuto
}
function guardInvalidUpdatesAndThrow(
automation: Automation,
oldAutomation: Automation
) {
const stepDefinitions = [
automation.definition.trigger,
...automation.definition.steps,
]
const oldStepDefinitions = [
oldAutomation.definition.trigger,
...oldAutomation.definition.steps,
]
for (const step of stepDefinitions) {
const readonlyFields = Object.keys(
step.schema.inputs.properties || {}
).filter(k => step.schema.inputs.properties[k].readonly)
readonlyFields.forEach(readonlyField => {
const oldStep = oldStepDefinitions.find(i => i.id === step.id)
if (step.inputs[readonlyField] !== oldStep?.inputs[readonlyField]) {
throw new HTTPError(
`Field ${readonlyField} is readonly and it cannot be modified`,
400
)
}
})
}
}
55 changes: 55 additions & 0 deletions packages/server/src/sdk/app/automations/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { sample } from "lodash/fp"
import { Automation } from "@budibase/types"
import automationSdk from "../"
import { structures } from "../../../../api/routes/tests/utilities"
import TestConfiguration from "../../../../tests/utilities/TestConfiguration"

describe("automation sdk", () => {
const config = new TestConfiguration()

beforeAll(async () => {
await config.init()
})

describe("update", () => {
it.each([
["trigger", (a: Automation) => a.definition.trigger],
["step", (a: Automation) => a.definition.steps[0]],
])("can update input fields (for a %s)", async (_, getStep) => {
await config.doInContext(config.getAppId(), async () => {
const automation = structures.newAutomation()

const keyToUse = sample(Object.keys(getStep(automation).inputs))!
getStep(automation).inputs[keyToUse] = "anyValue"

const response = await automationSdk.create(automation)

const update = { ...response }
getStep(update).inputs[keyToUse] = "anyUpdatedValue"
const result = await automationSdk.update(update)
expect(getStep(result).inputs[keyToUse]).toEqual("anyUpdatedValue")
})
})

it.each([
["trigger", (a: Automation) => a.definition.trigger],
["step", (a: Automation) => a.definition.steps[0]],
])("cannot update readonly fields (for a %s)", async (_, getStep) => {
await config.doInContext(config.getAppId(), async () => {
const automation = structures.newAutomation()
getStep(automation).schema.inputs.properties["readonlyProperty"] = {
readonly: true,
}
getStep(automation).inputs["readonlyProperty"] = "anyValue"

const response = await automationSdk.create(automation)

const update = { ...response }
getStep(update).inputs["readonlyProperty"] = "anyUpdatedValue"
await expect(automationSdk.update(update)).rejects.toThrow(
"Field readonlyProperty is readonly and it cannot be modified"
)
})
})
})
})
3 changes: 2 additions & 1 deletion packages/server/src/sdk/app/rowActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function create(tableId: string, rowAction: { name: string }) {
definition: {
trigger: {
type: AutomationStepType.TRIGGER,
id: "TODO id",
id: "trigger",
tagline: "TODO tagline",
name: "Row Action",
description: "TODO description",
Expand All @@ -76,6 +76,7 @@ export async function create(tableId: string, rowAction: { name: string }) {
type: AutomationIOType.STRING,
customType: AutomationCustomIOType.TABLE,
title: "Table",
readonly: true,
},
},
required: ["tableId"],
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/documents/app/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ interface BaseIOStructure {
[key: string]: BaseIOStructure
}
required?: string[]
readonly?: true
}

export interface InputOutputBlock {
Expand Down
Loading