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

fix: default frame renderer security improvements #506

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/friendly-eyes-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@frames.js/render": patch
---

fix: enforce http(s) protocol on redirect URLs
5 changes: 5 additions & 0 deletions .changeset/giant-jobs-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@frames.js/render": patch
---

fix: don't render data URIs that are SVG or not images
18 changes: 17 additions & 1 deletion packages/render/src/ui/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ function createDefaultComponents<TStylingProps extends Record<string, unknown>>(
Image(props, stylingProps) {
const aspectRatio = props.aspectRatio.replace(":", "/");

let sanitizedSrc =
props.status === "frame-loading" ? undefined : props.src;

// Don't allow data URLs that are not images -- we don't want to allow arbitrary data to be loaded
if (
sanitizedSrc?.startsWith("data:") &&
!sanitizedSrc?.startsWith("data:image")
) {
sanitizedSrc = "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty string src is invalid afaik - might be better to throw some sort of error on the frame/display an error screen

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced with undefined for now. we don't have error handling at this level so will have to refactor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could maybe move this check to ImageContainer and render an error there or return an error component here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could maybe move this check to ImageContainer and render an error there or return an error component here

would probably be best, as silent errors is not ideal + neither is logging here

}

// Don't allow SVG data URLs -- could contain malicious code
if (sanitizedSrc?.startsWith("data:image/svg")) {
sanitizedSrc = "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sanitizedSrc is not passed in as src to the img element

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, ty

}

return createElement("img", {
...stylingProps,
"data-aspect-ratio": aspectRatio,
Expand Down Expand Up @@ -211,7 +227,7 @@ type FrameUIProps<TStylingProps extends Record<string, unknown>> = Omit<
};

export function FrameUI<
TStylingProps extends Record<string, unknown> = StylingProps,
TStylingProps extends Record<string, unknown> = StylingProps
>(props: FrameUIProps<TStylingProps>): JSX.Element {
const defaultComponents = useMemo(
() => createDefaultComponents<TStylingProps>(props.createElement),
Expand Down
16 changes: 13 additions & 3 deletions packages/render/src/use-fetch-frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function defaultErrorHandler(error: Error): void {
export function useFetchFrame<
TSignerStorageType = Record<string, unknown>,
TFrameActionBodyType extends FrameActionBodyPayload = FrameActionBodyPayload,
TFrameContextType extends FrameContext = FarcasterFrameContext,
TFrameContextType extends FrameContext = FarcasterFrameContext
>({
stackAPI,
stackDispatch,
Expand Down Expand Up @@ -314,6 +314,16 @@ export function useFetchFrame<
// check the URL is valid
const locationUrl = new URL(location);

// Reject non-http(s) URLs
if (
locationUrl.protocol !== "http:" &&
locationUrl.protocol !== "https:"
) {
throw new Error(
`Redirect location ${location} is not a valid HTTP or HTTPS URL.`
);
}

onRedirect(locationUrl);

stackAPI.markAsDoneWithRedirect({
Expand Down Expand Up @@ -959,7 +969,7 @@ function getResponseBody(response: Response): Promise<unknown> {
type SignAndGetFrameActionPayloadOptions<
TSignerStorageType,
TFrameActionBodyType extends FrameActionBodyPayload,
TFrameContextType extends FrameContext,
TFrameContextType extends FrameContext
> = {
signerStateActionContext: SignerStateActionContext<
TSignerStorageType,
Expand All @@ -977,7 +987,7 @@ type SignAndGetFrameActionPayloadOptions<
async function signAndGetFrameActionBodyPayload<
TSignerStorageType,
TFrameActionBodyType extends FrameActionBodyPayload,
TFrameContextType extends FrameContext,
TFrameContextType extends FrameContext
>({
signerStateActionContext,
signFrameAction,
Expand Down
Loading