Skip to content

Commit

Permalink
fix: Disallow images and icons in footers
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Aug 29, 2024
1 parent 51c1db1 commit 6612283
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
22 changes: 22 additions & 0 deletions packages/snaps-sdk/src/jsx/validation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ describe('FooterStruct', () => {
<Button name="cancel">Cancel</Button>
<Button name="confirm">Confirm</Button>
</Footer>,
<Footer>
<Button name="cancel">Cancel {true && 'foo'}</Button>
</Footer>,
])('validates a footer element', (value) => {
expect(is(value, FooterStruct)).toBe(true);
});
Expand All @@ -541,6 +544,25 @@ describe('FooterStruct', () => {
<Row label="label">
<Image src="<svg />" alt="alt" />
</Row>,
<Footer>
<Button name="confirm">
<Icon name="warning" />
</Button>
</Footer>,
<Footer>
<Button name="cancel">
<Image src="<svg />" />
</Button>
<Button name="confirm">
<Image src="<svg />" />
</Button>
</Footer>,
<Footer>
<Button name="confirm">
<Icon name="warning" />
<Icon name="warning" />
</Button>
</Footer>,
])('does not validate "%p"', (value) => {
expect(is(value, FooterStruct)).toBe(false);
});
Expand Down
30 changes: 28 additions & 2 deletions packages/snaps-sdk/src/jsx/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
record,
string,
tuple,
refine,
} from '@metamask/superstruct';
import {
hasProperty,
Expand Down Expand Up @@ -412,6 +413,31 @@ export const BoxStruct: Describe<BoxElement> = element('Box', {
),
});

const FooterButtonStruct = refine(ButtonStruct, 'FooterButton', (value) => {
if (
typeof value.props.children === 'string' ||
typeof value.props.children === 'boolean' ||
value.props.children === null
) {
return true;
}

if (Array.isArray(value.props.children)) {
const hasNonTextElements = value.props.children.some(
(child) =>
typeof child !== 'string' &&
typeof child !== 'boolean' &&
child !== null,
);

if (!hasNonTextElements) {
return true;
}
}

return 'Footer buttons must contain text';
});

/**
* A struct for the {@link SectionElement} type.
*/
Expand All @@ -434,8 +460,8 @@ export const SectionStruct: Describe<SectionElement> = element('Section', {
* This set should include a single button or a tuple of two buttons.
*/
export const FooterChildStruct = nullUnion([
tuple([ButtonStruct, ButtonStruct]),
ButtonStruct,
tuple([FooterButtonStruct, FooterButtonStruct]),
FooterButtonStruct,
]);

/**
Expand Down

0 comments on commit 6612283

Please sign in to comment.