@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/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  Semantic versioning: new features → minor, fixes → patch. Breaking changes only in a new major version.
4
4
 
5
+ ## 1.0.2 - 19.11.2026
6
+
7
+ Security release — please update.
8
+
9
+ - `html.sanitize()` and `html.markdown()`: links like `java	script:` or `\u0001javascript:` were kept because the scheme check ran before browsers strip tabs, line breaks and control characters — a click ran script (XSS through product, CMS or blog HTML). `isSafeUrl()` now removes those characters before checking and also refuses `/\host` and `\\host`.
10
+ - `request()` only accepts paths that start with a single `/`; anything else (`@host/…`, `//host`, backslashes, control characters) is refused before a request is made, so the customer token and the shop key never reach another host.
11
+ - `baseUrl` must be `https://` (plain `http://` only for localhost) and must not contain credentials.
12
+ - `checkout.start()` / `startSubscription()` only redirect to `https://` checkout addresses.
13
+ - Docs: hosted online stores now run on `<shop>.sellforge.ai`.
14
+
5
15
  ## 1.0.1 - 18.11.2026
6
16
 
7
17
  - npm page: Sellforge logo, links to sellforge.ai and the documentation; homepage is sellforge.ai; bug reports by email (the repository is private). No code changes.
package/README.md CHANGED
@@ -27,7 +27,7 @@ console.log(sf.format.price(cart.total_cents)); // "19,90 €"
27
27
  await sf.checkout.start({ returnUrl: "https://shop.example.com/checkout/result" });
28
28
  ```
29
29
 
30
- Inside a Sellforge-hosted online store (`*.sellforge.shop` or a connected domain) no key is needed:
30
+ Inside a Sellforge-hosted online store (`<shop>.sellforge.ai` or a connected domain) no key is needed:
31
31
 
32
32
  ```ts
33
33
  const sf = createStorefront();
@@ -64,6 +64,22 @@ if (event.type === "checkout.completed") { /* fulfil event.data.order_id once */
64
64
  | `shop`, `shipping`, `newsletter`, `withdrawal`, `insights` | Branding, languages, shipping, signup, online withdrawal, analytics |
65
65
  | `html`, `format` | Safe HTML, Markdown, price and date formatting |
66
66
 
67
+ ## Security
68
+
69
+ The storefront is untrusted: it only asks. Prices, tax, shipping, discounts, stock, order and
70
+ payment status are always calculated and decided by Sellforge on the server, the shop comes from
71
+ the publishable key or the host name, and payment is confirmed by the signed
72
+ `checkout.completed` webhook — never by the return page.
73
+
74
+ - Only publishable keys (`pk_sf_…`) belong in a storefront; the SDK refuses secret tokens.
75
+ - Print shop texts with `textContent` or `html.escape()`, HTML only through `html.sanitize()`.
76
+ - The customer session is kept in `sessionStorage` (one tab, gone when the tab closes). Keep
77
+ third-party scripts off your shop pages and use a strict Content Security Policy; Sellforge-hosted
78
+ stores ship one (`script-src 'self'`).
79
+ - Releases are published from GitLab CI with npm trusted publishing (no npm token exists).
80
+
81
+ Report security issues to office@sellforge.ai.
82
+
67
83
  ## Development
68
84
 
69
85
  ```bash
package/dist/index.cjs CHANGED
@@ -28,6 +28,16 @@ var STORAGE_KEYS = {
28
28
  };
29
29
  var DEFAULT_API = "https://api.sellforge.ai";
30
30
  var isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined";
31
+ var isLocalHost = (host) => host === "localhost" || host === "127.0.0.1" || host === "[::1]" || host.endsWith(".localhost");
32
+ function isSecureUrl(u) {
33
+ try {
34
+ const p = new URL(u);
35
+ if (p.username || p.password) return false;
36
+ return p.protocol === "https:" || p.protocol === "http:" && isLocalHost(p.hostname);
37
+ } catch {
38
+ return false;
39
+ }
40
+ }
31
41
  function memoryStore() {
32
42
  const m = /* @__PURE__ */ new Map();
33
43
  return {
@@ -82,8 +92,12 @@ var Core = class {
82
92
  }
83
93
  this.key = o.publishableKey;
84
94
  this.hosted = !o.publishableKey;
85
- if (o.baseUrl) this.baseUrl = o.baseUrl.replace(/\/+$/, "");
86
- else if (o.publishableKey) this.baseUrl = DEFAULT_API;
95
+ if (o.baseUrl) {
96
+ if (!isSecureUrl(o.baseUrl)) {
97
+ throw new SellforgeError(0, "baseUrl must be an https:// URL (http:// only for localhost).", "invalid_base_url");
98
+ }
99
+ this.baseUrl = o.baseUrl.replace(/\/+$/, "");
100
+ } else if (o.publishableKey) this.baseUrl = DEFAULT_API;
87
101
  else if (isBrowser()) this.baseUrl = `${window.location.origin}/_sellforge`;
88
102
  else {
89
103
  throw new SellforgeError(0, "Pass publishableKey (and optionally baseUrl) when not running inside a Sellforge-hosted online store.", "missing_key");
@@ -141,6 +155,9 @@ var Core = class {
141
155
  }
142
156
  }
143
157
  url(path, query) {
158
+ if (typeof path !== "string" || !path.startsWith("/") || path.startsWith("//") || path.includes("\\") || /[\u0000-\u001f\u007f]/.test(path)) {
159
+ throw new SellforgeError(0, 'Request paths must start with a single "/" (e.g. /api/store/products).', "invalid_path");
160
+ }
144
161
  let u = this.baseUrl + path;
145
162
  if (query) {
146
163
  const q = Object.entries(query).filter(([, v]) => v !== void 0 && v !== null && v !== "").map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`);
@@ -158,11 +175,12 @@ var Core = class {
158
175
  const c = o.auth || o.requireAuth ? this.customer() : null;
159
176
  if (o.requireAuth && !c) throw new SellforgeError(401, "Not logged in.", "not_logged_in");
160
177
  if (c) headers["Authorization"] = `Bearer ${c.token}`;
178
+ const target = this.url(path, o.query);
161
179
  const ctrl = typeof AbortController === "function" ? new AbortController() : null;
162
180
  const timer = ctrl ? setTimeout(() => ctrl.abort(), this.timeoutMs) : null;
163
181
  let res;
164
182
  try {
165
- res = await this.fetchFn(this.url(path, o.query), {
183
+ res = await this.fetchFn(target, {
166
184
  method: o.method ?? "GET",
167
185
  headers,
168
186
  body: o.body !== void 0 ? JSON.stringify(o.body) : void 0,
@@ -537,6 +555,12 @@ var defaultReturn = () => {
537
555
  if (!isBrowser()) throw new SellforgeError(400, "returnUrl is required outside the browser.", "return_url_required");
538
556
  return `${window.location.origin}/checkout/result`;
539
557
  };
558
+ var redirectTarget = (url) => {
559
+ if (typeof url !== "string" || !isSecureUrl(url)) {
560
+ throw new SellforgeError(502, "The checkout returned an unexpected payment address.", "invalid_checkout_url");
561
+ }
562
+ return url;
563
+ };
540
564
  var toInputs = (values) => values.length ? Object.fromEntries(values.map((e) => [e.key, e.value])) : void 0;
541
565
  var nonEmpty = (a) => a && typeof a === "object" && Object.keys(a).length ? a : void 0;
542
566
  function checkoutModule(core, cart) {
@@ -583,7 +607,7 @@ function checkoutModule(core, cart) {
583
607
  async start(o = {}) {
584
608
  const s = await api.createSession(o);
585
609
  if (!isBrowser()) throw new SellforgeError(400, "start() needs a browser; use createSession() on a server.", "browser_required");
586
- window.location.assign(s.url);
610
+ window.location.assign(redirectTarget(s.url));
587
611
  return new Promise(() => void 0);
588
612
  },
589
613
  /**
@@ -631,7 +655,7 @@ function checkoutModule(core, cart) {
631
655
  async startSubscription(o) {
632
656
  const s = await api.subscribe(o);
633
657
  if (!isBrowser()) throw new SellforgeError(400, "startSubscription() needs a browser; use subscribe() on a server.", "browser_required");
634
- window.location.assign(s.url);
658
+ window.location.assign(redirectTarget(s.url));
635
659
  return new Promise(() => void 0);
636
660
  }
637
661
  };
@@ -845,12 +869,13 @@ var ALLOWED_TAGS = /* @__PURE__ */ new Set([
845
869
  var ALLOWED_ATTRS = /* @__PURE__ */ new Set(["href", "src", "alt", "title", "class", "colspan", "rowspan", "width", "height"]);
846
870
  var DROP = /^(SCRIPT|STYLE|IFRAME|OBJECT|EMBED|TEMPLATE|NOSCRIPT|SVG|MATH|FORM|INPUT|BUTTON|TEXTAREA|SELECT|LINK|META|BASE)$/;
847
871
  function isSafeUrl(url, image = false) {
848
- const t = String(url || "").trim();
872
+ const t = String(url ?? "").replace(/[\u0000- \u007f-\u009f]/g, "");
873
+ if (!t) return false;
849
874
  if (/^https:/i.test(t)) return true;
850
875
  if (!image && /^(http:|mailto:|tel:)/i.test(t)) return true;
851
876
  if (/^[a-z][a-z0-9+.-]*:/i.test(t)) return false;
852
- if (/^\/\//.test(t)) return false;
853
- return t.length > 0;
877
+ if (/^[\\/]{2}/.test(t)) return false;
878
+ return true;
854
879
  }
855
880
  function sanitize(html) {
856
881
  const input = String(html ?? "");
@@ -1230,7 +1255,7 @@ function defaultLocale() {
1230
1255
  }
1231
1256
 
1232
1257
  // src/index.ts
1233
- var VERSION = "1.0.1";
1258
+ var VERSION = "1.0.2";
1234
1259
  function createStorefront(options = {}) {
1235
1260
  const core = new Core(options);
1236
1261
  const cart = cartModule(core);