@muhkoo/connect 0.7.0-alpha.3 → 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/dist/connect.d.ts CHANGED
@@ -2316,6 +2316,16 @@ declare class HostedAuth {
2316
2316
  redirectUri: string;
2317
2317
  redirect?: boolean;
2318
2318
  }): Promise<string>;
2319
+ /**
2320
+ * (App side) Send the user to centralized account & security management on
2321
+ * the hosted page (auth.muhkoo.dev/security) — manage passkeys, recovery
2322
+ * phrase, email, Google, change password, and remove login methods, all in
2323
+ * one place. `returnUri` (default: the current URL) gives the hosted page a
2324
+ * "Back to app" target. Redirects the browser; resolves to the URL.
2325
+ */
2326
+ manageAccount(opts?: {
2327
+ returnUri?: string;
2328
+ }): Promise<string>;
2319
2329
  /** Whether the current URL looks like a hosted-auth callback (has code + state). */
2320
2330
  isCallback(): boolean;
2321
2331
  /**
@@ -2685,6 +2695,8 @@ declare class HttpError extends Error {
2685
2695
  }
2686
2696
  declare class HttpClient {
2687
2697
  readonly baseUrl: string;
2698
+ /** Origin of {@link baseUrl} — credentials are only attached same-origin. */
2699
+ private readonly baseOrigin;
2688
2700
  private readonly apiKey;
2689
2701
  private readonly getSessionToken;
2690
2702
  private readonly onUnauthorized;
@@ -2696,6 +2708,8 @@ declare class HttpClient {
2696
2708
  * up credentials without knowing about them.
2697
2709
  */
2698
2710
  readonly fetch: typeof fetch;
2711
+ /** True when `input` resolves to {@link baseOrigin} (relative URLs always do). */
2712
+ private isSameOrigin;
2699
2713
  get<T = unknown>(path: string): Promise<T>;
2700
2714
  post<T = unknown>(path: string, body?: unknown): Promise<T>;
2701
2715
  patch<T = unknown>(path: string, body?: unknown): Promise<T>;
@@ -1734,16 +1734,15 @@ class DoubleRatchetManager {
1734
1734
  }
1735
1735
  async registerZK(clientId, secret, salt, ecdsaPub) {
1736
1736
  await this._authenticator.initializeZK();
1737
- console.log(`Registering ZK for ${clientId}...`);
1738
- console.log('ECDSA Public Key:', ecdsaPub);
1739
1737
  const ecdsaJwk = await crypto.subtle.exportKey('jwk', ecdsaPub);
1740
1738
  const ecdsaHex = toHex(fromBase64Url(ecdsaJwk.x)).slice(0, 64);
1741
1739
  const ecdsaPubField = new Field(BigInt('0x' + ecdsaHex));
1742
1740
  const ecdsaPubHash = await Poseidon.hash([ecdsaPubField]);
1743
1741
  const commitment = await Poseidon.hash([secret, salt, ecdsaPubHash]);
1744
- console.log(`Commitment for ${clientId}:`, { commitment, salt });
1745
1742
  this.registeredUsers.set(clientId, { commitment, salt });
1746
- appLogger.debug(`Registered ${clientId}: commitment=${commitment.toString()}, salt=${salt.toString()}`);
1743
+ // NB: never log `secret`, `salt`, or `commitment` — `salt` is a private
1744
+ // witness for the ZK circuit and `commitment` is the user's stable id.
1745
+ appLogger.debug(`Registered ZK identity for ${clientId}`);
1747
1746
  }
1748
1747
  async performHandshake(senderId, recipientId, zkProof, publicInput, clientPublicKey, // ECDH pub for session
1749
1748
  clientAuthPublicKey, // ECDSA pub for auth
@@ -2969,7 +2968,7 @@ function base64UrlDecode(s) {
2969
2968
  * expect (and isn't serving a stale cached bundle). Keep this in sync with
2970
2969
  * `package.json` `version`.
2971
2970
  */
2972
- const VERSION = "0.7.0-alpha.3";
2971
+ const VERSION = "0.7.0-alpha.4";
2973
2972
 
2974
2973
  /**
2975
2974
  * `HttpClient` — the one place the unified {@link Client} talks to the
@@ -3005,6 +3004,8 @@ class HttpError extends Error {
3005
3004
  }
3006
3005
  class HttpClient {
3007
3006
  baseUrl;
3007
+ /** Origin of {@link baseUrl} — credentials are only attached same-origin. */
3008
+ baseOrigin;
3008
3009
  apiKey;
3009
3010
  getSessionToken;
3010
3011
  onUnauthorized;
@@ -3013,6 +3014,12 @@ class HttpClient {
3013
3014
  if (!opts?.baseUrl)
3014
3015
  throw new Error("HttpClient: `baseUrl` is required");
3015
3016
  this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
3017
+ try {
3018
+ this.baseOrigin = new URL(this.baseUrl).origin;
3019
+ }
3020
+ catch {
3021
+ this.baseOrigin = "";
3022
+ }
3016
3023
  this.apiKey = opts.apiKey ?? null;
3017
3024
  this.getSessionToken = opts.getSessionToken ?? (() => null);
3018
3025
  this.onUnauthorized = opts.onUnauthorized ?? null;
@@ -3029,13 +3036,34 @@ class HttpClient {
3029
3036
  */
3030
3037
  fetch = (input, init) => {
3031
3038
  const headers = new Headers(init?.headers);
3032
- if (this.apiKey)
3033
- headers.set("X-Muhkoo-Key", this.apiKey);
3034
- const token = this.getSessionToken();
3035
- if (token)
3036
- headers.set("X-Muhkoo-Session", token);
3039
+ // Only attach credentials when the request stays on the accelerator's
3040
+ // origin. An absolute URL pointing elsewhere (e.g. a caller-supplied or
3041
+ // server-reflected path that escapes `baseUrl`) must NOT receive the app
3042
+ // key or session token, or it becomes a token-exfiltration vector.
3043
+ if (this.isSameOrigin(input)) {
3044
+ if (this.apiKey)
3045
+ headers.set("X-Muhkoo-Key", this.apiKey);
3046
+ const token = this.getSessionToken();
3047
+ if (token)
3048
+ headers.set("X-Muhkoo-Session", token);
3049
+ }
3037
3050
  return this.fetchFn(input, { ...init, headers });
3038
3051
  };
3052
+ /** True when `input` resolves to {@link baseOrigin} (relative URLs always do). */
3053
+ isSameOrigin(input) {
3054
+ try {
3055
+ const urlStr = typeof input === "string" ? input
3056
+ : input instanceof URL ? input.href
3057
+ : input.url;
3058
+ // Relative URLs are resolved against baseUrl downstream → same origin.
3059
+ if (!/^https?:\/\//i.test(urlStr))
3060
+ return true;
3061
+ return new URL(urlStr).origin === this.baseOrigin;
3062
+ }
3063
+ catch {
3064
+ return true;
3065
+ }
3066
+ }
3039
3067
  // -------------------------------------------------------------------------
3040
3068
  // JSON convenience helpers (path is relative to baseUrl)
3041
3069
  // -------------------------------------------------------------------------
@@ -3529,6 +3557,19 @@ class HostedAuth {
3529
3557
  this.location().assign(url);
3530
3558
  return url;
3531
3559
  }
3560
+ /**
3561
+ * (App side) Send the user to centralized account & security management on
3562
+ * the hosted page (auth.muhkoo.dev/security) — manage passkeys, recovery
3563
+ * phrase, email, Google, change password, and remove login methods, all in
3564
+ * one place. `returnUri` (default: the current URL) gives the hosted page a
3565
+ * "Back to app" target. Redirects the browser; resolves to the URL.
3566
+ */
3567
+ async manageAccount(opts = {}) {
3568
+ const ret = opts.returnUri ?? this.location().href;
3569
+ const url = `${this.deps.authBaseUrl}/security?` + new URLSearchParams({ return: ret }).toString();
3570
+ this.location().assign(url);
3571
+ return url;
3572
+ }
3532
3573
  /** Whether the current URL looks like a hosted-auth callback (has code + state). */
3533
3574
  isCallback() {
3534
3575
  try {