@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.
@@ -1807,16 +1807,15 @@ class DoubleRatchetManager {
1807
1807
  }
1808
1808
  async registerZK(clientId, secret, salt, ecdsaPub) {
1809
1809
  await this._authenticator.initializeZK();
1810
- console.log(`Registering ZK for ${clientId}...`);
1811
- console.log('ECDSA Public Key:', ecdsaPub);
1812
1810
  const ecdsaJwk = await crypto.subtle.exportKey('jwk', ecdsaPub);
1813
1811
  const ecdsaHex = toHex(fromBase64Url(ecdsaJwk.x)).slice(0, 64);
1814
1812
  const ecdsaPubField = new Field(BigInt('0x' + ecdsaHex));
1815
1813
  const ecdsaPubHash = await Poseidon.hash([ecdsaPubField]);
1816
1814
  const commitment = await Poseidon.hash([secret, salt, ecdsaPubHash]);
1817
- console.log(`Commitment for ${clientId}:`, { commitment, salt });
1818
1815
  this.registeredUsers.set(clientId, { commitment, salt });
1819
- appLogger.debug(`Registered ${clientId}: commitment=${commitment.toString()}, salt=${salt.toString()}`);
1816
+ // NB: never log `secret`, `salt`, or `commitment` — `salt` is a private
1817
+ // witness for the ZK circuit and `commitment` is the user's stable id.
1818
+ appLogger.debug(`Registered ZK identity for ${clientId}`);
1820
1819
  }
1821
1820
  async performHandshake(senderId, recipientId, zkProof, publicInput, clientPublicKey, // ECDH pub for session
1822
1821
  clientAuthPublicKey, // ECDSA pub for auth
@@ -4928,6 +4927,8 @@ class HttpError extends Error {
4928
4927
  }
4929
4928
  class HttpClient {
4930
4929
  baseUrl;
4930
+ /** Origin of {@link baseUrl} — credentials are only attached same-origin. */
4931
+ baseOrigin;
4931
4932
  apiKey;
4932
4933
  getSessionToken;
4933
4934
  onUnauthorized;
@@ -4936,6 +4937,12 @@ class HttpClient {
4936
4937
  if (!opts?.baseUrl)
4937
4938
  throw new Error("HttpClient: `baseUrl` is required");
4938
4939
  this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
4940
+ try {
4941
+ this.baseOrigin = new URL(this.baseUrl).origin;
4942
+ }
4943
+ catch {
4944
+ this.baseOrigin = "";
4945
+ }
4939
4946
  this.apiKey = opts.apiKey ?? null;
4940
4947
  this.getSessionToken = opts.getSessionToken ?? (() => null);
4941
4948
  this.onUnauthorized = opts.onUnauthorized ?? null;
@@ -4952,13 +4959,34 @@ class HttpClient {
4952
4959
  */
4953
4960
  fetch = (input, init) => {
4954
4961
  const headers = new Headers(init?.headers);
4955
- if (this.apiKey)
4956
- headers.set("X-Muhkoo-Key", this.apiKey);
4957
- const token = this.getSessionToken();
4958
- if (token)
4959
- headers.set("X-Muhkoo-Session", token);
4962
+ // Only attach credentials when the request stays on the accelerator's
4963
+ // origin. An absolute URL pointing elsewhere (e.g. a caller-supplied or
4964
+ // server-reflected path that escapes `baseUrl`) must NOT receive the app
4965
+ // key or session token, or it becomes a token-exfiltration vector.
4966
+ if (this.isSameOrigin(input)) {
4967
+ if (this.apiKey)
4968
+ headers.set("X-Muhkoo-Key", this.apiKey);
4969
+ const token = this.getSessionToken();
4970
+ if (token)
4971
+ headers.set("X-Muhkoo-Session", token);
4972
+ }
4960
4973
  return this.fetchFn(input, { ...init, headers });
4961
4974
  };
4975
+ /** True when `input` resolves to {@link baseOrigin} (relative URLs always do). */
4976
+ isSameOrigin(input) {
4977
+ try {
4978
+ const urlStr = typeof input === "string" ? input
4979
+ : input instanceof URL ? input.href
4980
+ : input.url;
4981
+ // Relative URLs are resolved against baseUrl downstream → same origin.
4982
+ if (!/^https?:\/\//i.test(urlStr))
4983
+ return true;
4984
+ return new URL(urlStr).origin === this.baseOrigin;
4985
+ }
4986
+ catch {
4987
+ return true;
4988
+ }
4989
+ }
4962
4990
  // -------------------------------------------------------------------------
4963
4991
  // JSON convenience helpers (path is relative to baseUrl)
4964
4992
  // -------------------------------------------------------------------------