File size: 4,010 Bytes
9d4a3b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import assert from "node:assert/strict";
import test from "node:test";
import { NextRequest } from "next/server";
import { POST } from "../app/api/auth/send-code/route.ts";

const ENVIRONMENT_KEYS = [
  "PUBLIC_CLONE_AUTH_MODE",
  "PUBLIC_CLONE_AUTH_REDIS_REST_URL",
  "PUBLIC_CLONE_AUTH_RESEND_API_URL",
  "PUBLIC_CLONE_AUTH_SITE_ID",
  "PUBLIC_CLONE_AUTH_SITE_LABEL",
  "PUBLIC_CLONE_SMOKE_TOKEN",
];

function sendCodeRequest(email, smokeToken) {
  const headers = {
    "content-type": "application/json",
    cookie: `wb_public_session=${"l".repeat(48)}`,
    origin: "http://linkedin.test",
    "x-websitebench-client-ip": "198.51.100.93",
  };
  if (smokeToken !== undefined) headers["x-public-clone-smoke-token"] = smokeToken;
  return new NextRequest("http://linkedin.test/api/auth/send-code", {
    method: "POST",
    headers,
    body: JSON.stringify({ email }),
  });
}

test("LinkedIn send-code exposes the OTP only to the exact private deployment token", async () => {
  const originalFetch = globalThis.fetch;
  const originalEnvironment = Object.fromEntries(
    ENVIRONMENT_KEYS.map((key) => [key, process.env[key]]),
  );
  const resendPayloads = [];
  process.env.PUBLIC_CLONE_AUTH_MODE = "redis-resend";
  process.env.PUBLIC_CLONE_AUTH_REDIS_REST_URL = "http://redis.internal";
  process.env.PUBLIC_CLONE_AUTH_RESEND_API_URL = "http://resend.internal/emails";
  process.env.PUBLIC_CLONE_AUTH_SITE_ID = "linkedin";
  process.env.PUBLIC_CLONE_AUTH_SITE_LABEL = "LinkedIn";
  globalThis.fetch = async (input, init = {}) => {
    const url = String(input);
    if (url.startsWith("http://redis.internal/")) {
      const commands = JSON.parse(String(init.body || "[]"));
      return Response.json(commands.map(([operation]) => ({
        result: operation === "SET" ? "OK" : 1,
      })));
    }
    if (url === "http://resend.internal/emails") {
      resendPayloads.push(JSON.parse(String(init.body || "{}")));
      return Response.json({ id: `email_${resendPayloads.length}` }, { status: 200 });
    }
    throw new Error(`Unexpected smoke-token test request: ${url}`);
  };

  try {
    process.env.PUBLIC_CLONE_SMOKE_TOKEN = "";
    const unconfigured = await POST(sendCodeRequest("smoke-unconfigured@example.test"));
    assert.equal(unconfigured.status, 202);
    assert.equal("smoke_code" in await unconfigured.json(), false);

    process.env.PUBLIC_CLONE_SMOKE_TOKEN = "b".repeat(63);
    const malformed = await POST(sendCodeRequest("smoke-malformed@example.test", "b".repeat(63)));
    assert.equal(malformed.status, 202);
    assert.equal("smoke_code" in await malformed.json(), false);

    const configuredToken = "a".repeat(64);
    process.env.PUBLIC_CLONE_SMOKE_TOKEN = configuredToken;
    const ordinary = await POST(sendCodeRequest("smoke-ordinary@example.test"));
    assert.equal(ordinary.status, 202);
    const ordinaryBody = await ordinary.json();
    assert.equal("smoke_code" in ordinaryBody, false);
    assert.equal("dev_code" in ordinaryBody, false);

    const wrong = await POST(sendCodeRequest("smoke-wrong@example.test", `${"a".repeat(63)}b`));
    assert.equal(wrong.status, 202);
    const wrongBody = await wrong.json();
    assert.equal("smoke_code" in wrongBody, false);
    assert.equal("dev_code" in wrongBody, false);

    const authorized = await POST(sendCodeRequest("smoke-authorized@example.test", configuredToken));
    assert.equal(authorized.status, 202);
    const authorizedBody = await authorized.json();
    assert.match(String(authorizedBody.smoke_code || ""), /^[0-9]{6}$/u);
    assert.equal("dev_code" in authorizedBody, false);
    const deliveredText = String(resendPayloads.at(-1)?.text || "");
    assert.match(deliveredText, new RegExp(`\\b${authorizedBody.smoke_code}\\b`, "u"));
  } finally {
    globalThis.fetch = originalFetch;
    for (const key of ENVIRONMENT_KEYS) {
      const value = originalEnvironment[key];
      if (value === undefined) delete process.env[key];
      else process.env[key] = value;
    }
  }
});