@muhkoo/connect 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/dist/connect.d.ts CHANGED
@@ -2695,6 +2695,8 @@ declare class HttpError extends Error {
2695
2695
  }
2696
2696
  declare class HttpClient {
2697
2697
  readonly baseUrl: string;
2698
+ /** Origin of {@link baseUrl} — credentials are only attached same-origin. */
2699
+ private readonly baseOrigin;
2698
2700
  private readonly apiKey;
2699
2701
  private readonly getSessionToken;
2700
2702
  private readonly onUnauthorized;
@@ -2706,6 +2708,8 @@ declare class HttpClient {
2706
2708
  * up credentials without knowing about them.
2707
2709
  */
2708
2710
  readonly fetch: typeof fetch;
2711
+ /** True when `input` resolves to {@link baseOrigin} (relative URLs always do). */
2712
+ private isSameOrigin;
2709
2713
  get<T = unknown>(path: string): Promise<T>;
2710
2714
  post<T = unknown>(path: string, body?: unknown): Promise<T>;
2711
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
@@ -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
  // -------------------------------------------------------------------------