@muhkoo/cli 0.7.0-alpha.4 → 0.7.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@muhkoo/cli",
3
- "version": "0.7.0-alpha.4",
3
+ "version": "0.7.0-alpha.5",
4
4
  "description": "Command-line interface for the Muhkoo platform — provision backends, deploy apps, and manage agents, functions, databases, and domains.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -38,7 +38,7 @@
38
38
  "prepublishOnly": "node scripts/sync-version.mjs --check"
39
39
  },
40
40
  "dependencies": {
41
- "@muhkoo/connect": "0.7.0-alpha.4"
41
+ "@muhkoo/connect": "0.7.0-alpha.5"
42
42
  },
43
43
  "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
44
44
  }
@@ -14,7 +14,7 @@ import { join, relative, sep } from "node:path";
14
14
  import { resolveBase, loadConfig, resolveToken } from "../lib/config.js";
15
15
  import { skCall, ensure } from "../lib/http.js";
16
16
  import { firstOf } from "../lib/args.js";
17
- import { step, ok, info, die } from "../lib/ui.js";
17
+ import { step, ok, info, die, redact } from "../lib/ui.js";
18
18
 
19
19
  export const help = `muhkoo deploy — deploy a built client to Muhkoo hosting
20
20
 
@@ -81,7 +81,7 @@ export default async function deploy(args) {
81
81
  let deduped = 0;
82
82
  for (const [sha, bytes] of blobs) {
83
83
  const r = await skCall(baseUrl, auth, "PUT", `/api/apps/${appId}/hosting/blob/${sha}`, bytes, { raw: true });
84
- if (!r.ok) die(`Upload failed for ${sha} (${r.status}): ${await r.res.text()}`);
84
+ if (!r.ok) die(`Upload failed for ${sha} (${r.status}): ${redact(await r.res.text())}`);
85
85
  const body = await r.res.json().catch(() => ({}));
86
86
  if (body.dedup) deduped++;
87
87
  else uploaded++;
@@ -15,7 +15,7 @@
15
15
  * }
16
16
  */
17
17
 
18
- import { readFile, writeFile, access } from "node:fs/promises";
18
+ import { readFile, writeFile, access, chmod } from "node:fs/promises";
19
19
  import { devContext } from "../lib/config.js";
20
20
  import { devCall, appKeyGet } from "../lib/http.js";
21
21
  import { appsSuffixFor } from "../lib/bases.js";
@@ -33,7 +33,9 @@ async function readJson(path) {
33
33
  return JSON.parse(await readFile(path, "utf8"));
34
34
  }
35
35
  async function writeJson(path, obj) {
36
- await writeFile(path, JSON.stringify(obj, null, 2) + "\n");
36
+ // .muhkoo-app.json carries the app's secret (sk) keys — keep it owner-only.
37
+ await writeFile(path, JSON.stringify(obj, null, 2) + "\n", { mode: 0o600 });
38
+ await chmod(path, 0o600).catch(() => {}); // tighten if the file already existed
37
39
  }
38
40
  async function fileExists(path) {
39
41
  try {
package/src/lib/config.js CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  import { homedir } from "node:os";
8
8
  import { join } from "node:path";
9
- import { readFile, writeFile, mkdir, chmod } from "node:fs/promises";
9
+ import { readFile, writeFile, mkdir, rename, chmod } from "node:fs/promises";
10
10
  import { resolveBaseUrl } from "./bases.js";
11
11
  import { firstOf } from "./args.js";
12
12
  import { die } from "./ui.js";
@@ -27,10 +27,15 @@ export async function loadConfig() {
27
27
  }
28
28
 
29
29
  export async function saveConfig(next) {
30
- await mkdir(configDir(), { recursive: true });
31
- await writeFile(configPath(), JSON.stringify(next, null, 2) + "\n");
32
- // Contains a session token — keep it owner-only.
33
- await chmod(configPath(), 0o600).catch(() => {});
30
+ // The config holds a session token, so it must never be world-readable —
31
+ // not even briefly. Create the dir owner-only, write to a temp file with
32
+ // mode 0o600, then atomically rename it into place (the renamed file keeps
33
+ // its restrictive perms, with no truncation/permission window).
34
+ await mkdir(configDir(), { recursive: true, mode: 0o700 });
35
+ await chmod(configDir(), 0o700).catch(() => {}); // tighten an existing dir
36
+ const tmp = join(configDir(), `.config.${process.pid}.tmp`);
37
+ await writeFile(tmp, JSON.stringify(next, null, 2) + "\n", { mode: 0o600 });
38
+ await rename(tmp, configPath());
34
39
  }
35
40
 
36
41
  /**
package/src/lib/http.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * → "needs a paid plan") without re-parsing.
5
5
  */
6
6
 
7
- import { die } from "./ui.js";
7
+ import { die, redact } from "./ui.js";
8
8
 
9
9
  async function request(baseUrl, method, path, { auth, body, raw } = {}) {
10
10
  const headers = {};
@@ -49,11 +49,12 @@ export async function appKeyGet(baseUrl, appKey, path) {
49
49
  /** Exit with a formatted message when a `{ ok, status, body }` result failed. */
50
50
  export function ensure(result, what) {
51
51
  if (result.ok) return result;
52
- const detail =
52
+ const detail = redact(
53
53
  typeof result.body === "string"
54
54
  ? result.body
55
55
  : result.body
56
56
  ? JSON.stringify(result.body)
57
- : "";
57
+ : "",
58
+ );
58
59
  die(`${what} failed (${result.status})${detail ? `: ${detail}` : ""}`);
59
60
  }
package/src/lib/ui.js CHANGED
@@ -41,6 +41,17 @@ export function json(obj) {
41
41
  console.log(JSON.stringify(obj, null, 2));
42
42
  }
43
43
 
44
+ /**
45
+ * Mask secret-shaped substrings (app keys, long bearer-style tokens) in
46
+ * untrusted text — e.g. a server error body — before printing it, so a
47
+ * reflected key/token can't leak into the terminal or CI logs.
48
+ */
49
+ export function redact(s) {
50
+ return String(s)
51
+ .replace(/mk_[a-z0-9]+_[a-z0-9]+_[A-Za-z0-9]+/gi, (m) => m.slice(0, 11) + "…")
52
+ .replace(/\b[A-Za-z0-9._-]{40,}\b/g, (m) => m.slice(0, 6) + "…[redacted]");
53
+ }
54
+
44
55
  /** Render a simple aligned table from rows of strings. */
45
56
  export function table(headers, rows) {
46
57
  const widths = headers.map((h, i) =>
@@ -21,7 +21,7 @@ import http from "node:http";
21
21
  import { generatePkce, randomState } from "./pkce.js";
22
22
  import { openBrowser } from "./browser.js";
23
23
  import { authBaseFor } from "./bases.js";
24
- import { step, info, die, c } from "./ui.js";
24
+ import { step, info, die, redact, c } from "./ui.js";
25
25
 
26
26
  /** The CLI's first-party client id. Override with --app-id / $MUHKOO_CLI_APP_ID. */
27
27
  export const DEFAULT_CLI_APP_ID = "muhkoo-cli";
@@ -29,10 +29,16 @@ export const DEFAULT_CLI_APP_ID = "muhkoo-cli";
29
29
  /** Loopback ports tried in order — each must be registered in FIRST_PARTY_REDIRECT_URIS. */
30
30
  const PORTS = [8976, 8977, 8978];
31
31
 
32
+ /** HTML-escape untrusted text before interpolating it into the loopback page. */
33
+ function escapeHtml(s) {
34
+ return String(s).replace(/[&<>"']/g, (c) =>
35
+ ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[c]);
36
+ }
37
+
32
38
  function successPage(message, isError) {
33
39
  const color = isError ? "#c0392b" : "#0a1929";
34
40
  const title = isError ? "Sign-in failed" : "Signed in to the Muhkoo CLI";
35
- const body = isError ? message : "You can close this tab and return to your terminal.";
41
+ const body = isError ? escapeHtml(message) : "You can close this tab and return to your terminal.";
36
42
  return `<!doctype html><html><head><meta charset="utf-8"><title>${title}</title>
37
43
  <style>body{font-family:Inter,system-ui,sans-serif;background:#f6f8fa;color:${color};
38
44
  display:flex;min-height:100vh;align-items:center;justify-content:center;margin:0}
@@ -133,7 +139,7 @@ export async function webLogin({ baseUrl, appId }) {
133
139
  headers: { "Content-Type": "application/json" },
134
140
  body: JSON.stringify({ code, codeVerifier: verifier, appId }),
135
141
  });
136
- if (!res.ok) die(`Token exchange failed (${res.status}): ${await res.text()}`);
142
+ if (!res.ok) die(`Token exchange failed (${res.status}): ${redact(await res.text())}`);
137
143
  const body = await res.json().catch(() => null);
138
144
  if (!body?.sessionToken) die("No session token returned from the token exchange.");
139
145
  return { token: body.sessionToken, username: body.username, commitment: body.commitment };