11import assert from "node:assert/strict" ;
2- import { mkdirSync , mkdtempSync , writeFileSync } from "node:fs" ;
2+ import { chmodSync , mkdirSync , mkdtempSync , statSync , writeFileSync } from "node:fs" ;
33import { tmpdir } from "node:os" ;
44import { join } from "node:path" ;
55import test from "node:test" ;
@@ -15,7 +15,8 @@ writeFileSync(
1515process . env . HOME = home ;
1616process . env . USERPROFILE = home ;
1717
18- const { whoami } = await import ( "../src/auth.mjs" ) ;
18+ const { saveCreds, whoami } = await import ( "../src/auth.mjs" ) ;
19+ const posixMode = process . platform === "win32" ? { skip : "POSIX permission bits" } : { } ;
1920
2021/** Run whoami against a canned app response and collect what it printed. */
2122async function whoamiAgainst ( { status, body } ) {
@@ -55,3 +56,11 @@ test("whoami still calls out an expired session on 401", async () => {
5556 const out = await whoamiAgainst ( { status : 401 , body : { error : "unauthorized" } } ) ;
5657 assert . match ( out , / s e s s i o n e x p i r e d / ) ;
5758} ) ;
59+
60+ test ( "saving credentials tightens a world-readable existing file" , posixMode , ( ) => {
61+ chmodSync ( join ( home , ".moshcode" , "credentials.json" ) , 0o644 ) ;
62+
63+ saveCreds ( { api : "https://app.example.test" , token : "tok_fresh" , email : "me@example.test" } ) ;
64+
65+ assert . equal ( statSync ( join ( home , ".moshcode" , "credentials.json" ) ) . mode & 0o777 , 0o600 ) ;
66+ } ) ;
0 commit comments