@sellforge/storefront 1.0.1 → 1.0.2

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.cts CHANGED
@@ -10,7 +10,7 @@ interface StorefrontOptions {
10
10
  /**
11
11
  * Publishable key of the shop (`pk_sf_…`). Required for storefronts on your own domain
12
12
  * and for server-side use. Omit it inside a Sellforge-hosted online store
13
- * (`*.sellforge.shop` or a connected domain): there the shop comes from the host name.
13
+ * (`<shop>.sellforge.ai` or a connected domain): there the shop comes from the host name.
14
14
  */
15
15
  publishableKey?: string;
16
16
  /**
@@ -674,7 +674,14 @@ interface GoogleAdsConfig {
674
674
  */
675
675
  /** Escape text for use inside HTML. */
676
676
  declare function escape(value: unknown): string;
677
- /** `https:`, `mailto:`, `tel:`, relative paths and anchors; images only `https:` or relative. */
677
+ /**
678
+ * `https:`, `mailto:`, `tel:`, relative paths and anchors; images only `https:` or relative.
679
+ *
680
+ * Browsers ignore tabs, line breaks and leading control characters in URLs, so
681
+ * `java\tscript:` or `\u0001javascript:` would still run as `javascript:`. They are
682
+ * removed before the scheme is checked (1.0.2). Protocol-relative URLs (`//host`, and the
683
+ * backslash variants `/\\host` and `\\\\host` that browsers treat the same) leave the site.
684
+ */
678
685
  declare function isSafeUrl(url: string, image?: boolean): boolean;
679
686
  /**
680
687
  * Remove everything but basic formatting from HTML (allow-list). Requires a DOM
@@ -803,7 +810,7 @@ interface SubscribeOptions {
803
810
  affiliateCode?: string;
804
811
  }
805
812
 
806
- declare const VERSION = "1.0.1";
813
+ declare const VERSION = "1.0.2";
807
814
  /**
808
815
  * Create a storefront client.
809
816
  *
package/dist/index.d.ts CHANGED
@@ -10,7 +10,7 @@ interface StorefrontOptions {
10
10
  /**
11
11
  * Publishable key of the shop (`pk_sf_…`). Required for storefronts on your own domain
12
12
  * and for server-side use. Omit it inside a Sellforge-hosted online store
13
- * (`*.sellforge.shop` or a connected domain): there the shop comes from the host name.
13
+ * (`<shop>.sellforge.ai` or a connected domain): there the shop comes from the host name.
14
14
  */
15
15
  publishableKey?: string;
16
16
  /**
@@ -674,7 +674,14 @@ interface GoogleAdsConfig {
674
674
  */
675
675
  /** Escape text for use inside HTML. */
676
676
  declare function escape(value: unknown): string;
677
- /** `https:`, `mailto:`, `tel:`, relative paths and anchors; images only `https:` or relative. */
677
+ /**
678
+ * `https:`, `mailto:`, `tel:`, relative paths and anchors; images only `https:` or relative.
679
+ *
680
+ * Browsers ignore tabs, line breaks and leading control characters in URLs, so
681
+ * `java\tscript:` or `\u0001javascript:` would still run as `javascript:`. They are
682
+ * removed before the scheme is checked (1.0.2). Protocol-relative URLs (`//host`, and the
683
+ * backslash variants `/\\host` and `\\\\host` that browsers treat the same) leave the site.
684
+ */
678
685
  declare function isSafeUrl(url: string, image?: boolean): boolean;
679
686
  /**
680
687
  * Remove everything but basic formatting from HTML (allow-list). Requires a DOM
@@ -803,7 +810,7 @@ interface SubscribeOptions {
803
810
  affiliateCode?: string;
804
811
  }
805
812
 
806
- declare const VERSION = "1.0.1";
813
+ declare const VERSION = "1.0.2";
807
814
  /**
808
815
  * Create a storefront client.
809
816
  *
package/dist/index.js CHANGED
@@ -9,6 +9,16 @@ var STORAGE_KEYS = {
9
9
  };
10
10
  var DEFAULT_API = "https://api.sellforge.ai";
11
11
  var isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined";
12
+ var isLocalHost = (host) => host === "localhost" || host === "127.0.0.1" || host === "[::1]" || host.endsWith(".localhost");
13
+ function isSecureUrl(u) {
14
+ try {
15
+ const p = new URL(u);
16
+ if (p.username || p.password) return false;
17
+ return p.protocol === "https:" || p.protocol === "http:" && isLocalHost(p.hostname);
18
+ } catch {
19
+ return false;
20
+ }
21
+ }
12
22
  function memoryStore() {
13
23
  const m = /* @__PURE__ */ new Map();
14
24
  return {
@@ -63,8 +73,12 @@ var Core = class {
63
73
  }
64
74
  this.key = o.publishableKey;
65
75
  this.hosted = !o.publishableKey;
66
- if (o.baseUrl) this.baseUrl = o.baseUrl.replace(/\/+$/, "");
67
- else if (o.publishableKey) this.baseUrl = DEFAULT_API;
76
+ if (o.baseUrl) {
77
+ if (!isSecureUrl(o.baseUrl)) {
78
+ throw new SellforgeError(0, "baseUrl must be an https:// URL (http:// only for localhost).", "invalid_base_url");
79
+ }
80
+ this.baseUrl = o.baseUrl.replace(/\/+$/, "");
81
+ } else if (o.publishableKey) this.baseUrl = DEFAULT_API;
68
82
  else if (isBrowser()) this.baseUrl = `${window.location.origin}/_sellforge`;
69
83
  else {
70
84
  throw new SellforgeError(0, "Pass publishableKey (and optionally baseUrl) when not running inside a Sellforge-hosted online store.", "missing_key");
@@ -122,6 +136,9 @@ var Core = class {
122
136
  }
123
137
  }
124
138
  url(path, query) {
139
+ if (typeof path !== "string" || !path.startsWith("/") || path.startsWith("//") || path.includes("\\") || /[\u0000-\u001f\u007f]/.test(path)) {
140
+ throw new SellforgeError(0, 'Request paths must start with a single "/" (e.g. /api/store/products).', "invalid_path");
141
+ }
125
142
  let u = this.baseUrl + path;
126
143
  if (query) {
127
144
  const q = Object.entries(query).filter(([, v]) => v !== void 0 && v !== null && v !== "").map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`);
@@ -139,11 +156,12 @@ var Core = class {
139
156
  const c = o.auth || o.requireAuth ? this.customer() : null;
140
157
  if (o.requireAuth && !c) throw new SellforgeError(401, "Not logged in.", "not_logged_in");
141
158
  if (c) headers["Authorization"] = `Bearer ${c.token}`;
159
+ const target = this.url(path, o.query);
142
160
  const ctrl = typeof AbortController === "function" ? new AbortController() : null;
143
161
  const timer = ctrl ? setTimeout(() => ctrl.abort(), this.timeoutMs) : null;
144
162
  let res;
145
163
  try {
146
- res = await this.fetchFn(this.url(path, o.query), {
164
+ res = await this.fetchFn(target, {
147
165
  method: o.method ?? "GET",
148
166
  headers,
149
167
  body: o.body !== void 0 ? JSON.stringify(o.body) : void 0,
@@ -518,6 +536,12 @@ var defaultReturn = () => {
518
536
  if (!isBrowser()) throw new SellforgeError(400, "returnUrl is required outside the browser.", "return_url_required");
519
537
  return `${window.location.origin}/checkout/result`;
520
538
  };
539
+ var redirectTarget = (url) => {
540
+ if (typeof url !== "string" || !isSecureUrl(url)) {
541
+ throw new SellforgeError(502, "The checkout returned an unexpected payment address.", "invalid_checkout_url");
542
+ }
543
+ return url;
544
+ };
521
545
  var toInputs = (values) => values.length ? Object.fromEntries(values.map((e) => [e.key, e.value])) : void 0;
522
546
  var nonEmpty = (a) => a && typeof a === "object" && Object.keys(a).length ? a : void 0;
523
547
  function checkoutModule(core, cart) {
@@ -564,7 +588,7 @@ function checkoutModule(core, cart) {
564
588
  async start(o = {}) {
565
589
  const s = await api.createSession(o);
566
590
  if (!isBrowser()) throw new SellforgeError(400, "start() needs a browser; use createSession() on a server.", "browser_required");
567
- window.location.assign(s.url);
591
+ window.location.assign(redirectTarget(s.url));
568
592
  return new Promise(() => void 0);
569
593
  },
570
594
  /**
@@ -612,7 +636,7 @@ function checkoutModule(core, cart) {
612
636
  async startSubscription(o) {
613
637
  const s = await api.subscribe(o);
614
638
  if (!isBrowser()) throw new SellforgeError(400, "startSubscription() needs a browser; use subscribe() on a server.", "browser_required");
615
- window.location.assign(s.url);
639
+ window.location.assign(redirectTarget(s.url));
616
640
  return new Promise(() => void 0);
617
641
  }
618
642
  };
@@ -826,12 +850,13 @@ var ALLOWED_TAGS = /* @__PURE__ */ new Set([
826
850
  var ALLOWED_ATTRS = /* @__PURE__ */ new Set(["href", "src", "alt", "title", "class", "colspan", "rowspan", "width", "height"]);
827
851
  var DROP = /^(SCRIPT|STYLE|IFRAME|OBJECT|EMBED|TEMPLATE|NOSCRIPT|SVG|MATH|FORM|INPUT|BUTTON|TEXTAREA|SELECT|LINK|META|BASE)$/;
828
852
  function isSafeUrl(url, image = false) {
829
- const t = String(url || "").trim();
853
+ const t = String(url ?? "").replace(/[\u0000- \u007f-\u009f]/g, "");
854
+ if (!t) return false;
830
855
  if (/^https:/i.test(t)) return true;
831
856
  if (!image && /^(http:|mailto:|tel:)/i.test(t)) return true;
832
857
  if (/^[a-z][a-z0-9+.-]*:/i.test(t)) return false;
833
- if (/^\/\//.test(t)) return false;
834
- return t.length > 0;
858
+ if (/^[\\/]{2}/.test(t)) return false;
859
+ return true;
835
860
  }
836
861
  function sanitize(html) {
837
862
  const input = String(html ?? "");
@@ -1211,7 +1236,7 @@ function defaultLocale() {
1211
1236
  }
1212
1237
 
1213
1238
  // src/index.ts
1214
- var VERSION = "1.0.1";
1239
+ var VERSION = "1.0.2";
1215
1240
  function createStorefront(options = {}) {
1216
1241
  const core = new Core(options);
1217
1242
  const cart = cartModule(core);