Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/auth.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ const b64url = (buf) => Buffer.from(buf).toString("base64url");
export function loadCreds() {
try { return JSON.parse(fs.readFileSync(credsPath, "utf8")); } catch { return null; }
}
function saveCreds(creds) {
export function saveCreds(creds) {
fs.mkdirSync(CREDS_DIR, { recursive: true });
fs.writeFileSync(credsPath, JSON.stringify(creds, null, 2), { mode: 0o600 });
fs.chmodSync(credsPath, 0o600);
}

function openBrowser(url) {
Expand Down
13 changes: 11 additions & 2 deletions test/auth.test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
import { chmodSync, mkdirSync, mkdtempSync, statSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
Expand All @@ -15,7 +15,8 @@ writeFileSync(
process.env.HOME = home;
process.env.USERPROFILE = home;

const { whoami } = await import("../src/auth.mjs");
const { saveCreds, whoami } = await import("../src/auth.mjs");
const posixMode = process.platform === "win32" ? { skip: "POSIX permission bits" } : {};

/** Run whoami against a canned app response and collect what it printed. */
async function whoamiAgainst({ status, body }) {
Expand Down Expand Up @@ -55,3 +56,11 @@ test("whoami still calls out an expired session on 401", async () => {
const out = await whoamiAgainst({ status: 401, body: { error: "unauthorized" } });
assert.match(out, /session expired/);
});

test("saving credentials tightens a world-readable existing file", posixMode, () => {
chmodSync(join(home, ".moshcode", "credentials.json"), 0o644);

saveCreds({ api: "https://app.example.test", token: "tok_fresh", email: "me@example.test" });

assert.equal(statSync(join(home, ".moshcode", "credentials.json")).mode & 0o777, 0o600);
});
Loading