~james/core

core/avatar/src/index.js -rw-r--r-- 8.9 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { gradientSvg } from "./gradient.js";
import { widthFor } from "./size.js";

const SANDBOX_CSP =
  "default-src 'none'; style-src 'unsafe-inline'; sandbox";

const contentTypes = {
  webp: "image/webp",
  jpeg: "image/jpeg",
  png: "image/png",
};

const rasterAvatarType = /^image\/(?:png|jpeg|webp|avif|gif)$/;

class ServedAvatarType {
  #header;
  static fromUpstream(raw) {
    const header = (raw ?? "").split(";")[0].trim().toLowerCase();
    return rasterAvatarType.test(header) ? new ServedAvatarType(header) : null;
  }
  static resized(format) {
    const header = contentTypes[format];
    return header ? new ServedAvatarType(header) : null;
  }
  constructor(header) {
    this.#header = header;
  }
  get header() {
    return this.#header;
  }
}

class SandboxedResponse extends Response {
  constructor(body, init = {}) {
    super(body, init);
    this.headers.set("X-Content-Type-Options", "nosniff");
    this.headers.set("Content-Security-Policy", SANDBOX_CSP);
  }
}


export default {
  async fetch(request, env) {
    // Helper function to fetch Tangled profile from PDS
    const getTangledAvatarFromPDS = async (actor) => {
      try {
        const identityResponse = await fetch(
          `${env.BOBBIN_URL.replace(/\/$/, "")}/xrpc/blue.microcosm.identity.resolveMiniDoc?identifier=${encodeURIComponent(actor)}`,
        );
        const identity = identityResponse.ok ? await identityResponse.json() : null;
        if (!identity?.pds) {
          console.log({
            level: "debug",
            message: "failed to resolve identity",
            actor: actor,
          });
          return null;
        }

        const did = identity.did;
        const pdsEndpoint = identity.pds.replace(/\/$/, ""); // Remove trailing slash

        if (!pdsEndpoint) {
          console.log({
            level: "debug",
            message: "no PDS endpoint found",
            actor: actor,
            did: did,
          });
          return null;
        }

        const profileUrl = `${pdsEndpoint}/xrpc/com.atproto.repo.getRecord?repo=${did}&collection=org.tangled.actor.profile&rkey=self`;

        // Fetch the Tangled profile record from PDS
        const profileResponse = await fetch(profileUrl);

        if (!profileResponse.ok) {
          console.log({
            level: "debug",
            message: "no Tangled profile found on PDS",
            actor: actor,
            status: profileResponse.status,
          });
          return null;
        }

        const profileData = await profileResponse.json();
        const avatarBlob = profileData?.value?.avatar;

        if (!avatarBlob) {
          console.log({
            level: "debug",
            message: "Tangled profile has no avatar",
            actor: actor,
          });
          return null;
        }

        // Extract CID from blob reference object
        // The ref might be an object with $link property or a string
        let avatarCID;
        if (typeof avatarBlob.ref === "string") {
          avatarCID = avatarBlob.ref;
        } else if (avatarBlob.ref?.$link) {
          avatarCID = avatarBlob.ref.$link;
        } else if (typeof avatarBlob === "string") {
          avatarCID = avatarBlob;
        }

        if (!avatarCID || typeof avatarCID !== "string") {
          console.log({
            level: "warn",
            message: "could not extract valid CID from avatar blob",
            actor: actor,
            avatarBlob: avatarBlob,
            avatarBlobRef: avatarBlob.ref,
          });
          return null;
        }

        // Construct blob URL (pdsEndpoint already has trailing slash removed)
        const blobUrl = `${pdsEndpoint}/xrpc/com.atproto.sync.getBlob?did=${did}&cid=${avatarCID}`;

        return blobUrl;
      } catch (e) {
        console.log({
          level: "warn",
          message: "error fetching Tangled avatar from PDS",
          actor: actor,
          error: e.message,
        });
        return null;
      }
    };

    // the old site showed the bluesky avatar when the tangled profile had none
    const getBlueskyAvatar = async (actor) => {
      try {
        const profileResponse = await fetch(
          `https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile?actor=${encodeURIComponent(actor)}`,
        );
        if (!profileResponse.ok) return null;
        return (await profileResponse.json()).avatar ?? null;
      } catch {
        return null;
      }
    };

    const url = new URL(request.url);
    const { pathname, searchParams } = url;

    if (!pathname || pathname === "/") {
      return new Response(
        `This is Tangled's avatar service. It fetches your pretty avatar from your PDS, or generates a placeholder.
You can't use this directly unfortunately since all requests are signed and may only originate from the appview.`,
      );
    }

    const width = widthFor(searchParams.get("size"));
    const format = searchParams.get("format") || "webp";
    const validFormats = ["webp", "jpeg", "png"];
    const outputFormat = validFormats.includes(format) ? format : "webp";
    


    const cache = caches.default;
    let cacheKey = request.url;
    let response = await cache.match(cacheKey);
    if (response) return new SandboxedResponse(response.body, response);

    const pathParts = pathname.slice(1).split("/");
    if (pathParts.length < 2) {
      return new Response("Bad URL", { status: 400 });
    }

    const [signatureHex, actor] = pathParts;
    const actorBytes = new TextEncoder().encode(actor);

    const key = await crypto.subtle.importKey(
      "raw",
      new TextEncoder().encode(env.AVATAR_SHARED_SECRET),
      { name: "HMAC", hash: "SHA-256" },
      false,
      ["sign", "verify"],
    );

    const computedSigBuffer = await crypto.subtle.sign("HMAC", key, actorBytes);
    const computedSig = Array.from(new Uint8Array(computedSigBuffer))
      .map((b) => b.toString(16).padStart(2, "0"))
      .join("");

    console.log({
      level: "debug",
      message: "avatar request for: " + actor,
      computedSignature: computedSig,
      providedSignature: signatureHex,
    });

    const sigBytes = Uint8Array.from(
      signatureHex.match(/.{2}/g).map((b) => parseInt(b, 16)),
    );
    const valid = await crypto.subtle.verify("HMAC", key, sigBytes, actorBytes);

    if (!valid) {
      return new Response("Invalid signature", { status: 403 });
    }

    try {
      const cfOptions = outputFormat !== "webp" || width ? {
        cf: {
          image: {
            format: outputFormat,
            ...(width ? { width, height: width, fit: "cover" } : {}),
          },
        },
      }: {};
      const fetchAvatar = (avatarUrl) =>
        fetch(avatarUrl, cfOptions).catch(() => null);

      const tangledUrl = await getTangledAvatarFromPDS(actor);
      let avatarResponse = tangledUrl ? await fetchAvatar(tangledUrl) : null;
      const status = avatarResponse?.status ?? 502;
      // a missing blob won't come back soon. an outage or a rate limit might
      const gone =
        !tangledUrl ||
        (status >= 400 && status < 500 && status !== 408 && status !== 429);

      // liar profiles can name a blob that no pds ever stored
      if (!avatarResponse?.ok && gone) {
        const blueskyUrl = await getBlueskyAvatar(actor);
        if (blueskyUrl) avatarResponse = await fetchAvatar(blueskyUrl);
      }

      if (!avatarResponse?.ok && !tangledUrl) {
        console.log({
          level: "debug",
          message: "no avatar found, generating placeholder",
          actor: actor,
        });

        const size = width ?? 128;
        const svg = gradientSvg(actor, size);
        const svgData = new TextEncoder().encode(svg);

        response = new SandboxedResponse(svgData, {
          headers: {
            "Content-Type": "image/svg+xml",
            "Cache-Control": "public, max-age=43200",
          },
        });
        await cache.put(cacheKey, response.clone());
        return response;
      }

      const served = cfOptions.cf
        ? ServedAvatarType.resized(outputFormat)
        : avatarResponse?.ok
          ? ServedAvatarType.fromUpstream(avatarResponse.headers.get("Content-Type"))
          : null;

      if (!avatarResponse?.ok || !served) {
        response = new Response(`failed to fetch avatar for ${actor}.`, {
          status: avatarResponse?.ok ? 415 : status,
          headers: {
            "Cache-Control": `public, max-age=${gone ? 3600 : 120}`,
          },
        });
        await cache.put(cacheKey, response.clone());
        return response;
      }

      const avatarData = await avatarResponse.arrayBuffer();

      response = new SandboxedResponse(avatarData, {
        headers: {
          "Content-Type": served.header,
          "Cache-Control": "public, max-age=43200",
        },
      });

      await cache.put(cacheKey, response.clone());
      return response;
    } catch (error) {
      return new Response(`error fetching avatar: ${error.message}`, {
        status: 500,
      });
    }
  },
};