@farcaster/snap-hono 1.4.11 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -116,7 +116,10 @@ export function registerSnapHandler(app, snapFn, options = {}) {
116
116
  const skipJFSVerification = options.skipJFSVerification !== undefined
117
117
  ? options.skipJFSVerification
118
118
  : envSkipJFSVerification();
119
- const parsed = await parseRequest(raw, { skipJFSVerification });
119
+ const parsed = await parseRequest(raw, {
120
+ skipJFSVerification,
121
+ requestOrigin: snapOriginFromRequest(raw),
122
+ });
120
123
  if (!parsed.success) {
121
124
  const err = parsed.error;
122
125
  switch (err.type) {
@@ -127,6 +130,7 @@ export function registerSnapHandler(app, snapFn, options = {}) {
127
130
  case "validation":
128
131
  return c.json({ error: "invalid POST body", issues: err.issues }, 400);
129
132
  case "replay":
133
+ case "origin_mismatch":
130
134
  return c.json({ error: err.message }, 400);
131
135
  case "signature":
132
136
  return c.json({ error: err.message }, 401);
@@ -202,14 +206,15 @@ async function getFallbackHtml(request, snapFn, ogImageUrl, openGraph) {
202
206
  }
203
207
  function snapOriginFromRequest(request) {
204
208
  const fromEnv = process.env.SNAP_PUBLIC_BASE_URL?.trim();
205
- if (fromEnv)
206
- return fromEnv.replace(/\/$/, "");
207
- const proto = request.headers.get("x-forwarded-proto")?.trim() || "https";
208
- const host = request.headers.get("x-forwarded-host")?.trim() ||
209
- request.headers.get("host")?.trim();
210
- if (host)
211
- return `${proto}://${host}`.replace(/\/$/, "");
212
- return "https://docs.farcaster.xyz/snap";
209
+ if (fromEnv) {
210
+ try {
211
+ return new URL(fromEnv).origin;
212
+ }
213
+ catch {
214
+ return fromEnv.replace(/\/$/, "");
215
+ }
216
+ }
217
+ return new URL(request.url).origin;
213
218
  }
214
219
  function clientWantsSnapResponse(accept) {
215
220
  if (!accept || accept.trim() === "")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farcaster/snap-hono",
3
- "version": "1.4.11",
3
+ "version": "1.5.0",
4
4
  "description": "Hono integration for Farcaster Snap servers",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,7 +28,7 @@
28
28
  "dependencies": {
29
29
  "@resvg/resvg-wasm": "^2.6.2",
30
30
  "satori": "^0.10.0",
31
- "@farcaster/snap": "1.15.4"
31
+ "@farcaster/snap": "1.16.0"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "hono": ">=4.0.0"
package/src/index.ts CHANGED
@@ -193,7 +193,10 @@ export function registerSnapHandler(
193
193
  ? options.skipJFSVerification
194
194
  : envSkipJFSVerification();
195
195
 
196
- const parsed = await parseRequest(raw, { skipJFSVerification });
196
+ const parsed = await parseRequest(raw, {
197
+ skipJFSVerification,
198
+ requestOrigin: snapOriginFromRequest(raw),
199
+ });
197
200
 
198
201
  if (!parsed.success) {
199
202
  const err = parsed.error;
@@ -208,6 +211,7 @@ export function registerSnapHandler(
208
211
  400,
209
212
  );
210
213
  case "replay":
214
+ case "origin_mismatch":
211
215
  return c.json({ error: err.message }, 400);
212
216
  case "signature":
213
217
  return c.json({ error: err.message }, 401);
@@ -301,15 +305,15 @@ async function getFallbackHtml(
301
305
 
302
306
  function snapOriginFromRequest(request: Request): string {
303
307
  const fromEnv = process.env.SNAP_PUBLIC_BASE_URL?.trim();
304
- if (fromEnv) return fromEnv.replace(/\/$/, "");
305
-
306
- const proto = request.headers.get("x-forwarded-proto")?.trim() || "https";
307
- const host =
308
- request.headers.get("x-forwarded-host")?.trim() ||
309
- request.headers.get("host")?.trim();
310
- if (host) return `${proto}://${host}`.replace(/\/$/, "");
308
+ if (fromEnv) {
309
+ try {
310
+ return new URL(fromEnv).origin;
311
+ } catch {
312
+ return fromEnv.replace(/\/$/, "");
313
+ }
314
+ }
311
315
 
312
- return "https://docs.farcaster.xyz/snap";
316
+ return new URL(request.url).origin;
313
317
  }
314
318
 
315
319
  function clientWantsSnapResponse(accept: string | undefined): boolean {