-
Notifications
You must be signed in to change notification settings - Fork 12
fix(agent-bff): enforce the action input contract on execute #1860
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
Tonours
wants to merge
18
commits into
main
Choose a base branch
from
fix/prd-1096-action-input-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8137a46
fix(agent-bff): enforce the action input contract on execute
Tonours 48d7461
refactor(agent-bff): guard enum membership without a widening cast
Tonours bb95de0
fix(agent-bff): distinguish file value errors from agent outages
Tonours d5c6e4d
style(agent-bff): wrap the file value error fixture line
Tonours 4c56138
fix(agent-bff): enforce list item types and align the unfolded enum s…
Tonours f06835d
fix(agent-bff): validate record picker fields as packed id strings
Tonours 314a4a2
fix(agent-bff): validate legacy list types and gate doc enums on enum…
Tonours 0259962
refactor(agent-bff): extract execute validation tests and shared fixt…
Tonours 6287f3f
test(agent-bff): cover the unconstrained item list expectation
Tonours 8a76ac8
refactor(agent-bff): share the field shape, enum and required rules
Tonours 79175da
refactor(agent-bff): split the column type algebra out of the cache
Tonours 0ffb932
test(agent-bff): assert the list validator blocks execute
Tonours 087a40e
fix(agent-bff): log execute rejections and name the failing list index
Tonours e4abbbe
fix(agent-client): reject a file inside an array on a non-file field
Tonours 8b95b2a
fix(agent-bff): reject unknown keys on the action request body
Tonours 7aed983
test(agent-client): group the array file cases with the non-file field
Tonours 5a18bbf
fix(agent-bff): publish a malformed action-field reference as null
Tonours 2cd3169
fix(agent-bff): pass logger in enum list mapper test
Tonours File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/agent-bff/src/action/action-values-validator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import type { ActionForm } from './agent-action-client'; | ||
| import type { Logger } from '../ports/logger-port'; | ||
| import type { FieldType, PrimitiveShape } from '../read-model/field-type'; | ||
|
|
||
| import { missingRequiredFieldNames } from './action-form-mapper'; | ||
| import { | ||
| enumOptionsOf, | ||
| isEnumFieldType, | ||
| normalizeFieldType, | ||
| primitiveShapeOf, | ||
| } from '../read-model/field-type'; | ||
| import { invalidActionValue, missingRequiredActionFields } from '../validation/validation-errors'; | ||
|
|
||
| const PACKED_ID_EXPECTATION = 'a string holding a packed record id'; | ||
|
|
||
| function shapeOf(type: FieldType): PrimitiveShape { | ||
| return typeof type === 'string' ? primitiveShapeOf(type) : 'any'; | ||
| } | ||
|
|
||
| interface ValueCheck { | ||
| expected: string; | ||
| matches(value: unknown): boolean; | ||
| } | ||
|
|
||
| const SHAPE_CHECKS: Record<PrimitiveShape, ValueCheck> = { | ||
| any: { expected: 'any value', matches: () => true }, | ||
| boolean: { expected: 'a boolean', matches: value => typeof value === 'boolean' }, | ||
| number: { | ||
| expected: 'a number', | ||
| matches: value => typeof value === 'number' && Number.isFinite(value), | ||
| }, | ||
| string: { expected: 'a string', matches: value => typeof value === 'string' }, | ||
| }; | ||
|
|
||
| function checkFor(type: FieldType, options: string[] | undefined): ValueCheck { | ||
| if (options === undefined) return SHAPE_CHECKS[shapeOf(type)]; | ||
|
|
||
| return { | ||
| expected: `one of: ${options.join(', ')}`, | ||
| matches: value => typeof value === 'string' && options.includes(value), | ||
| }; | ||
| } | ||
|
|
||
| function expectedWhenViolated( | ||
| type: FieldType, | ||
| value: unknown, | ||
| options: string[] | undefined, | ||
| ): string | undefined { | ||
| if (Array.isArray(type)) { | ||
| const [itemType] = type; | ||
|
|
||
| if (!Array.isArray(value)) return `an array of ${checkFor(itemType, options).expected}`; | ||
|
|
||
| const bad = value | ||
| .map((item, index) => ({ index, expected: expectedWhenViolated(itemType, item, options) })) | ||
| .filter((item): item is { index: number; expected: string } => item.expected !== undefined); | ||
|
|
||
| if (bad.length === 0) return undefined; | ||
|
|
||
| const label = bad.length === 1 ? 'index' : 'indexes'; | ||
|
|
||
| return `${label} ${bad.map(item => item.index).join(', ')} to be ${bad[0].expected}`; | ||
| } | ||
|
|
||
| const check = checkFor(type, options); | ||
|
|
||
| return check.matches(value) ? undefined : check.expected; | ||
| } | ||
|
|
||
| export default function assertActionValuesExecutable(action: ActionForm, logger: Logger): void { | ||
| const fields = action.getFields(); | ||
| const missing = missingRequiredFieldNames(fields); | ||
|
|
||
| if (missing.length > 0) { | ||
| logger('Warn', 'Action execute rejected: required fields left empty', { fields: missing }); | ||
|
|
||
| throw missingRequiredActionFields(missing); | ||
| } | ||
|
|
||
| const violations: { field: string; expected: string }[] = []; | ||
|
|
||
| const provided = fields.filter( | ||
| field => field.getValue() !== undefined && field.getValue() !== null, | ||
| ); | ||
|
|
||
| for (const field of provided) { | ||
| const name = field.getName(); | ||
| const value = field.getValue(); | ||
|
|
||
| if (field.getReference()) { | ||
| if (typeof value !== 'string') { | ||
| violations.push({ field: name, expected: PACKED_ID_EXPECTATION }); | ||
| } | ||
| } else { | ||
| const type = normalizeFieldType(field.getType()); | ||
| const options = enumOptionsOf(type, action.getEnumField(name).getOptions()); | ||
|
|
||
| if (options === undefined && isEnumFieldType(type)) { | ||
| logger('Warn', 'Action enum field accepted without its options', { field: name }); | ||
| } | ||
|
|
||
| const expected = expectedWhenViolated(type, value, options); | ||
|
|
||
| if (expected) violations.push({ field: name, expected }); | ||
| } | ||
| } | ||
|
|
||
| if (violations.length > 0) { | ||
| logger('Warn', 'Action execute rejected: invalid values', { | ||
| fields: violations.map(violation => violation.field), | ||
| }); | ||
|
|
||
| throw invalidActionValue(violations); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.