Spaces:
Running
Running
add origin check
Browse files- routes/generate/index.js +21 -0
- routes/root.js +1 -3
routes/generate/index.js
CHANGED
|
@@ -33,6 +33,23 @@ const IMAGE_SIZES = {
|
|
| 33 |
},
|
| 34 |
};
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
module.exports = async function (fastify, opts) {
|
| 37 |
fastify.get("/:inputs", async function (request, reply) {
|
| 38 |
let { inputs } = request.params;
|
|
@@ -51,6 +68,10 @@ module.exports = async function (fastify, opts) {
|
|
| 51 |
return reply.header("Content-Type", "image/jpeg").send(file);
|
| 52 |
}
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
const { height, width } =
|
| 55 |
IMAGE_SIZES[format ?? "square"] ?? IMAGE_SIZES["square"];
|
| 56 |
|
|
|
|
| 33 |
},
|
| 34 |
};
|
| 35 |
|
| 36 |
+
const ALLOWED_ORIGINS = [
|
| 37 |
+
"huggingface.co",
|
| 38 |
+
"enzostvs-deepsite.hf.space",
|
| 39 |
+
"hf.co",
|
| 40 |
+
"localhost:3001",
|
| 41 |
+
];
|
| 42 |
+
|
| 43 |
+
function isOriginAllowed(request) {
|
| 44 |
+
const origin = request.headers.origin ?? request.headers.host;
|
| 45 |
+
const referer = request.headers.referer;
|
| 46 |
+
|
| 47 |
+
return (
|
| 48 |
+
ALLOWED_ORIGINS.some((allowed) => origin.includes(allowed)) ||
|
| 49 |
+
ALLOWED_ORIGINS.some((allowed) => referer?.startsWith(allowed))
|
| 50 |
+
);
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
module.exports = async function (fastify, opts) {
|
| 54 |
fastify.get("/:inputs", async function (request, reply) {
|
| 55 |
let { inputs } = request.params;
|
|
|
|
| 68 |
return reply.header("Content-Type", "image/jpeg").send(file);
|
| 69 |
}
|
| 70 |
|
| 71 |
+
if (!isOriginAllowed(request)) {
|
| 72 |
+
return reply.code(403).send({ error: "Forbidden" });
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
const { height, width } =
|
| 76 |
IMAGE_SIZES[format ?? "square"] ?? IMAGE_SIZES["square"];
|
| 77 |
|
routes/root.js
CHANGED
|
@@ -200,9 +200,7 @@ module.exports = async function (fastify, opts) {
|
|
| 200 |
<div class="code-block">
|
| 201 |
<code>GET /generate/<span class="param">{your-prompt}</span>?format=<span class="param">{format}</span></code>
|
| 202 |
</div>
|
| 203 |
-
|
| 204 |
-
<a href="/generate/a-cute-robot-painting-a-sunset" class="example-link">Try an Example →</a>
|
| 205 |
-
</section>
|
| 206 |
|
| 207 |
<section>
|
| 208 |
<h2>Available Formats</h2>
|
|
|
|
| 200 |
<div class="code-block">
|
| 201 |
<code>GET /generate/<span class="param">{your-prompt}</span>?format=<span class="param">{format}</span></code>
|
| 202 |
</div>
|
| 203 |
+
</section>
|
|
|
|
|
|
|
| 204 |
|
| 205 |
<section>
|
| 206 |
<h2>Available Formats</h2>
|