testKeptSignInsAreTheUsersAlone

tcmts.atproto.test.testKeptSignInsAreTheUsersAlone · #3v6m67dg · term

Kept sign-ins are read only from the user's own file: not a link (refused), and not one others can read (made private); they're written without following a link left in the way.

import { keepSession, readSessions, sessionsFile } from "#codebase/tcmts/atproto";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

export const testKeptSignInsAreTheUsersAlone = () => {
  const was = process.env.XDG_CONFIG_HOME;
  process.env.XDG_CONFIG_HOME = fs.mkdtempSync(path.join(os.tmpdir(), "kept sign-ins "));
  try {
    const file = sessionsFile();
    fs.mkdirSync(path.dirname(file), { recursive: true });
    const elsewhere = path.join(process.env.XDG_CONFIG_HOME, "elsewhere.json");
    fs.writeFileSync(elsewhere, '{"sessions":{}}');
    fs.symlinkSync(elsewhere, file);
    assert.throws(() => readSessions(), /isn't a file of yours/);
    fs.rmSync(file);
    keepSession("did:plc:x", undefined);
    fs.chmodSync(file, 0o644);
    readSessions();
    assert.equal(fs.statSync(file).mode & 0o777, 0o600);
    assert.equal(fs.statSync(path.dirname(file)).mode & 0o777, 0o700);
  } finally {
    if (was === undefined) delete process.env.XDG_CONFIG_HOME;
    else process.env.XDG_CONFIG_HOME = was;
  }
}

uses