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: allow unsafe (less strict CSP header) content from other domain #97

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions app/(api)/preview/[iid]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { API_URL } from "../../../../lib/constants";
import { getFontSize } from "../../../../lib/utils";
import { InscriptionResponse } from "../../../../lib/types";
import { redirect } from "next/navigation";
import { API_URL, UNSAFE_API_URL } from "../../../../lib/constants";
import { InscriptionResponse } from "../../../../lib/types";
import { getFontSize } from "../../../../lib/utils";

export async function GET(
request: Request,
Expand Down
2 changes: 1 addition & 1 deletion app/(explorer)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Footer from "../../components/Footer";
import Header from "../../components/Header";

export const metadata: Metadata = {
metadataBase: new URL('https://ordinals.hiro.so'),
metadataBase: new URL("https://ordinals.hiro.so"),
title: {
template: "%s | Hiro Ordinals Explorer",
default: "Hiro Ordinals Explorer",
Expand Down
3 changes: 2 additions & 1 deletion components/Thumbnail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import IconImage from "./icons/IconImage";
import IconText from "./icons/IconText";
import IconVideo from "./icons/IconVideo";
import Iframe from "./inscriptions/Iframe";
import { UNSAFE_API_URL } from "../lib/constants";

// todo: add more types
const safeTypes = ["image/jpeg", "image/png", "image/gif", "image/webp"];
Expand Down Expand Up @@ -34,7 +35,7 @@ export const ThumbnailIcon = ({
if (showImage && safeTypes.includes(inscription.content_type.toLowerCase()))
return (
<Iframe
src={`${process.env.NEXT_PUBLIC_PREVIEW_URL}/preview/${inscription.id}`}
src={`${UNSAFE_API_URL}/inscriptions/${inscription.id}/unsafe`}
className="pointer-events-none"
/>
);
Expand Down
22 changes: 13 additions & 9 deletions components/inscriptions/InscriptionRender.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UNSAFE_API_URL } from "../../lib/constants";
import { InscriptionResponse } from "../../lib/types";
import Iframe from "./Iframe";
import InscriptionRenderImage from "./InscriptionRenderImage";
Expand All @@ -10,6 +11,18 @@ const InscriptionRender = (props: {
inscription: InscriptionResponse;
className?: string;
}) => {
if (
props.inscription.content_type.startsWith("text/html") ||
props.inscription.content_type.startsWith("image/svg+xml")
) {
return (
<Iframe
{...props}
src={`${UNSAFE_API_URL}/inscriptions/${props.inscription.id}/unsafe`}
/>
);
}

if (props.inscription.content_type.startsWith("image/")) {
return <InscriptionRenderImage {...props} />;
}
Expand All @@ -18,15 +31,6 @@ const InscriptionRender = (props: {
return WithContentJson(props, InscriptionRenderJson);
}

if (props.inscription.content_type.startsWith("text/html")) {
return (
<Iframe
{...props}
src={`${process.env.NEXT_PUBLIC_PREVIEW_URL}/preview/${props.inscription.id}`}
/>
);
}

if (props.inscription.content_type.startsWith("text/")) {
// also handles json parseable content from plain text
return <InscriptionRenderText {...props} />;
Expand Down
4 changes: 4 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ export const API_URL =
process.env.NEXT_PUBLIC_API_URL ?? "https://api.hiro.so/ordinals/v1";

export const API_BETA_URL = process.env.NEXT_PUBLIC_API_BETA_URL ?? API_URL;

export const UNSAFE_API_URL =
process.env.NEXT_PUBLIC_UNSAFE_API_URL ??
"https://ordinals-preview.vercel.app";