@sinch/functions-runtime 0.4.17 → 0.4.19

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/dist/index.d.ts CHANGED
@@ -2694,6 +2694,22 @@ export declare class SecretsLoader {
2694
2694
  * Helper to get function name from sinch.json
2695
2695
  */
2696
2696
  private getFunctionNameFromConfig;
2697
+ /**
2698
+ * Read a plaintext credential field from the active CLI profile.
2699
+ * Scoped to the canonical pair (keySecretPlaintext, applicationSecretPlaintext)
2700
+ * that sinch-cli writes when the OS keychain is unreachable. See
2701
+ * sinch-cli/src/lib/credentials.ts store() for the writer side.
2702
+ */
2703
+ private readProfilePlaintext;
2704
+ /**
2705
+ * Active write/read/delete round-trip to verify the OS keychain is reachable.
2706
+ * Used to distinguish "keychain works but no entries stored" from "keychain
2707
+ * unreachable" so error messages can be specific. Cost: 3 secret-tool /
2708
+ * security / PowerShell invocations, only when no secrets were loaded.
2709
+ */
2710
+ private probeKeychain;
2711
+ private platformHint;
2712
+ private warnPlaintext;
2697
2713
  /**
2698
2714
  * Load custom secrets added via 'sinch functions secrets' command
2699
2715
  */
package/dist/index.js CHANGED
@@ -3260,6 +3260,7 @@ function getTunnelClient(localPort = 3e3) {
3260
3260
  import fs4 from "fs";
3261
3261
  import path5 from "path";
3262
3262
  import os from "os";
3263
+ import crypto2 from "crypto";
3263
3264
 
3264
3265
  // src/secrets/keychain.ts
3265
3266
  import { execFile, spawn } from "child_process";
@@ -3434,6 +3435,14 @@ var macKeychain = {
3434
3435
  }
3435
3436
  }
3436
3437
  };
3438
+ function debugKeychain(op, err) {
3439
+ if (!process.env.DEBUG?.includes("sinch:keychain")) return;
3440
+ const e = err;
3441
+ const stderr = e.stderr?.toString() ?? "";
3442
+ const code = e.code ?? e.exitCode ?? "?";
3443
+ const message = e.message ?? String(err);
3444
+ console.debug(`[keychain] secret-tool ${op} failed (code=${code}): ${stderr || message}`);
3445
+ }
3437
3446
  var linuxKeychain = {
3438
3447
  async getPassword(service, account) {
3439
3448
  try {
@@ -3443,7 +3452,8 @@ var linuxKeychain = {
3443
3452
  { timeout: 15e3 }
3444
3453
  );
3445
3454
  return stdout.trimEnd();
3446
- } catch {
3455
+ } catch (err) {
3456
+ debugKeychain("lookup", err);
3447
3457
  return null;
3448
3458
  }
3449
3459
  },
@@ -3454,14 +3464,31 @@ var linuxKeychain = {
3454
3464
  ["store", "--label", `${service}/${account}`, "service", service, "account", account],
3455
3465
  { stdio: ["pipe", "pipe", "pipe"] }
3456
3466
  );
3467
+ let stderr = "";
3468
+ child.stderr?.on("data", (chunk) => {
3469
+ stderr += chunk.toString();
3470
+ });
3457
3471
  child.stdin.write(password);
3458
3472
  child.stdin.end();
3459
3473
  await new Promise((resolve, reject) => {
3460
- child.on(
3461
- "close",
3462
- (code) => code === 0 ? resolve() : reject(new Error("secret-tool store failed"))
3463
- );
3464
- child.on("error", reject);
3474
+ child.on("close", (code) => {
3475
+ if (code === 0) {
3476
+ resolve();
3477
+ } else {
3478
+ const err = Object.assign(
3479
+ new Error(
3480
+ `secret-tool store failed (exit ${code})${stderr ? ": " + stderr.trim() : ""}`
3481
+ ),
3482
+ { stderr, exitCode: code }
3483
+ );
3484
+ debugKeychain("store", err);
3485
+ reject(err);
3486
+ }
3487
+ });
3488
+ child.on("error", (err) => {
3489
+ debugKeychain("store", err);
3490
+ reject(err);
3491
+ });
3465
3492
  });
3466
3493
  },
3467
3494
  async deletePassword(service, account) {
@@ -3472,7 +3499,8 @@ var linuxKeychain = {
3472
3499
  { timeout: 15e3 }
3473
3500
  );
3474
3501
  return true;
3475
- } catch {
3502
+ } catch (err) {
3503
+ debugKeychain("clear", err);
3476
3504
  return false;
3477
3505
  }
3478
3506
  }
@@ -3491,6 +3519,12 @@ var backend = getBackend();
3491
3519
  async function getPassword(service, account) {
3492
3520
  return backend.getPassword(service, account);
3493
3521
  }
3522
+ async function setPassword(service, account, password) {
3523
+ return backend.setPassword(service, account, password);
3524
+ }
3525
+ async function deletePassword(service, account) {
3526
+ return backend.deletePassword(service, account);
3527
+ }
3494
3528
 
3495
3529
  // src/secrets/index.ts
3496
3530
  var SecretsLoader = class {
@@ -3532,22 +3566,41 @@ var SecretsLoader = class {
3532
3566
  }
3533
3567
  let secretsLoaded = 0;
3534
3568
  if (secretsToLoad.includes("PROJECT_ID_API_SECRET")) {
3535
- const apiSecret = await getPassword(this.SERVICE_NAME, `${this.username}-keySecret`);
3569
+ let apiSecret = await getPassword(this.SERVICE_NAME, `${this.username}-keySecret`);
3570
+ let fromPlaintext = false;
3571
+ if (!apiSecret) {
3572
+ apiSecret = this.readProfilePlaintext("keySecretPlaintext");
3573
+ fromPlaintext = !!apiSecret;
3574
+ }
3536
3575
  if (apiSecret) {
3537
3576
  process.env.PROJECT_ID_API_SECRET = apiSecret;
3538
- console.log("\u2705 Loaded PROJECT_ID_API_SECRET from secure storage");
3577
+ if (fromPlaintext) {
3578
+ this.warnPlaintext("PROJECT_ID_API_SECRET");
3579
+ } else {
3580
+ console.log("\u2705 Loaded PROJECT_ID_API_SECRET from secure storage");
3581
+ }
3539
3582
  secretsLoaded++;
3540
3583
  }
3541
3584
  }
3542
3585
  if (secretsToLoad.includes("VOICE_APPLICATION_SECRET")) {
3543
3586
  const applicationKey = process.env.VOICE_APPLICATION_KEY || this.getApplicationKeyFromConfig();
3587
+ let appSecret = null;
3588
+ let fromPlaintext = false;
3544
3589
  if (applicationKey) {
3545
- const appSecret = await getPassword(this.SERVICE_NAME, applicationKey);
3546
- if (appSecret) {
3547
- process.env.VOICE_APPLICATION_SECRET = appSecret;
3590
+ appSecret = await getPassword(this.SERVICE_NAME, applicationKey);
3591
+ }
3592
+ if (!appSecret) {
3593
+ appSecret = this.readProfilePlaintext("applicationSecretPlaintext");
3594
+ fromPlaintext = !!appSecret;
3595
+ }
3596
+ if (appSecret) {
3597
+ process.env.VOICE_APPLICATION_SECRET = appSecret;
3598
+ if (fromPlaintext) {
3599
+ this.warnPlaintext("VOICE_APPLICATION_SECRET");
3600
+ } else {
3548
3601
  console.log("\u2705 Loaded VOICE_APPLICATION_SECRET from secure storage");
3549
- secretsLoaded++;
3550
3602
  }
3603
+ secretsLoaded++;
3551
3604
  }
3552
3605
  }
3553
3606
  const functionName = this.getFunctionNameFromConfig();
@@ -3568,9 +3621,17 @@ var SecretsLoader = class {
3568
3621
  }
3569
3622
  }
3570
3623
  if (secretsLoaded === 0) {
3571
- console.log("\u2139\uFE0F No secrets found in secure storage for declared variables");
3572
- console.log("\u{1F4A1} To configure Sinch auth: sinch auth login");
3573
- console.log("\u{1F4A1} To add custom secrets: sinch secrets add <KEY> <VALUE>");
3624
+ const probe = await this.probeKeychain();
3625
+ if (!probe.ok) {
3626
+ console.warn(`\u26A0\uFE0F Keychain unreachable: ${probe.reason}`);
3627
+ if (probe.hint) console.warn(` ${probe.hint}`);
3628
+ console.warn(" Sinch CLI may have stored credentials in plaintext config instead.");
3629
+ console.warn(" Run `sinch auth status` to verify, or re-run `sinch auth login`.");
3630
+ } else {
3631
+ console.log("\u2139\uFE0F No secrets found in secure storage for declared variables");
3632
+ console.log("\u{1F4A1} To configure Sinch auth: sinch auth login");
3633
+ console.log("\u{1F4A1} To add custom secrets: sinch secrets add <KEY> <VALUE>");
3634
+ }
3574
3635
  }
3575
3636
  return secretsLoaded > 0;
3576
3637
  } catch (error) {
@@ -3609,6 +3670,87 @@ var SecretsLoader = class {
3609
3670
  }
3610
3671
  return null;
3611
3672
  }
3673
+ /**
3674
+ * Read a plaintext credential field from the active CLI profile.
3675
+ * Scoped to the canonical pair (keySecretPlaintext, applicationSecretPlaintext)
3676
+ * that sinch-cli writes when the OS keychain is unreachable. See
3677
+ * sinch-cli/src/lib/credentials.ts store() for the writer side.
3678
+ */
3679
+ readProfilePlaintext(field) {
3680
+ try {
3681
+ const home = os.homedir();
3682
+ const metaPath = path5.join(home, ".sinch", "config.json");
3683
+ let activeProfile = "default";
3684
+ if (fs4.existsSync(metaPath)) {
3685
+ const meta = JSON.parse(fs4.readFileSync(metaPath, "utf8"));
3686
+ if (typeof meta.activeProfile === "string" && meta.activeProfile) {
3687
+ activeProfile = meta.activeProfile;
3688
+ }
3689
+ }
3690
+ if (/[\\/]/.test(activeProfile) || activeProfile.includes("..")) {
3691
+ return null;
3692
+ }
3693
+ const profilePath = path5.join(home, ".sinch", "profiles", `${activeProfile}.json`);
3694
+ if (!fs4.existsSync(profilePath)) return null;
3695
+ const profile = JSON.parse(fs4.readFileSync(profilePath, "utf8"));
3696
+ const value = profile[field];
3697
+ return typeof value === "string" && value.length > 0 ? value : null;
3698
+ } catch (err) {
3699
+ console.debug("[Secrets] Could not read plaintext profile fallback:", String(err));
3700
+ return null;
3701
+ }
3702
+ }
3703
+ /**
3704
+ * Active write/read/delete round-trip to verify the OS keychain is reachable.
3705
+ * Used to distinguish "keychain works but no entries stored" from "keychain
3706
+ * unreachable" so error messages can be specific. Cost: 3 secret-tool /
3707
+ * security / PowerShell invocations, only when no secrets were loaded.
3708
+ */
3709
+ async probeKeychain() {
3710
+ const sentinelAccount = `__sinch_probe_${crypto2.randomUUID()}`;
3711
+ const sentinelValue = crypto2.randomUUID();
3712
+ try {
3713
+ await setPassword(this.SERVICE_NAME, sentinelAccount, sentinelValue);
3714
+ } catch (err) {
3715
+ return {
3716
+ ok: false,
3717
+ reason: `write failed (${err instanceof Error ? err.message : String(err)})`,
3718
+ hint: this.platformHint()
3719
+ };
3720
+ }
3721
+ let readBack = null;
3722
+ try {
3723
+ readBack = await getPassword(this.SERVICE_NAME, sentinelAccount);
3724
+ } catch {
3725
+ }
3726
+ try {
3727
+ await deletePassword(this.SERVICE_NAME, sentinelAccount);
3728
+ } catch {
3729
+ }
3730
+ if (readBack !== sentinelValue) {
3731
+ return {
3732
+ ok: false,
3733
+ reason: "write reported success but value cannot be read back",
3734
+ hint: this.platformHint()
3735
+ };
3736
+ }
3737
+ return { ok: true };
3738
+ }
3739
+ platformHint() {
3740
+ if (process.platform === "linux") {
3741
+ return 'On Linux/WSL: sudo apt install libsecret-tools gnome-keyring dbus-x11 && eval "$(gnome-keyring-daemon --start --components=secrets)"';
3742
+ }
3743
+ if (process.platform === "darwin") {
3744
+ return "On macOS: unlock the login keychain (Keychain Access app) and retry";
3745
+ }
3746
+ return "On Windows: ensure Credential Manager is accessible from this user session";
3747
+ }
3748
+ warnPlaintext(envVar) {
3749
+ console.warn(
3750
+ `\u26A0\uFE0F Loaded ${envVar} from plaintext config (~/.sinch/profiles/<active>.json) \u2014 keychain unavailable.`
3751
+ );
3752
+ console.warn(` ${this.platformHint()}`);
3753
+ }
3612
3754
  /**
3613
3755
  * Load custom secrets added via 'sinch functions secrets' command
3614
3756
  */