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

Streamshop rebased #974

Closed
wants to merge 1 commit into from
Closed
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
42 changes: 5 additions & 37 deletions website/loaders/asset.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,22 @@
import { forbidden } from "@deco/deco";
import { fetchSafe, STALE } from "../../utils/fetch.ts";
import { shortcircuit } from "@deco/deco";
interface Props {
/**
* @description Asset src like: https://fonts.gstatic.com/...
*/
src: string;
}

const loader = async (props: Props, request: Request): Promise<Response> => {
const loader = async (props: Props) => {
const url = new URL(props.src);

// Whitelist allowed protocols
const allowedProtocols = ["https:", "http:"];
if (!allowedProtocols.includes(url.protocol)) {
forbidden({
message: "Only HTTP and HTTPS protocols are allowed",
});
if (url.protocol === "file:") {
shortcircuit(new Response("Forbidden", { status: 403 }));
}

const original = await fetchSafe(url.href, STALE);
const response = new Response(original.body, original);

// Check if the request's Accept header includes "text/html"
const acceptHeader = request.headers.get("accept");
if (acceptHeader && acceptHeader.includes("text/html")) {
forbidden({
message: "Forbidden: text/html not accepted",
});
}

const contentType = response.headers.get("Content-Type");
if (contentType && contentType.includes("text/html")) {
forbidden({
message: "Forbidden: text/html not accepted as a response",
});
}

// Set strict Content-Security-Policy
response.headers.set(
"Content-Security-Policy",
"default-src 'none'; style-src 'unsafe-inline'",
);

// Set cache control headers
response.headers.set(
"Cache-Control",
"cache-control",
"public, s-maxage=15552000, max-age=15552000, immutable",
);

return response;
};

export default loader;
Loading