dsh-privacy-gateway 0.1.5 → 0.1.6

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.
Files changed (2) hide show
  1. package/lib/index.js +68 -25
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1863,6 +1863,29 @@ function quotedEnd(c, i, quote) {
1863
1863
  return Math.min(c.next.index(quote, i), c.next.index("\n", i));
1864
1864
  }
1865
1865
  /**
1866
+ * The end of the object whose `{` is s[i]: the `}` that matches it, or the cap
1867
+ * where it is not closed. A `}` inside a quoted value is no end, and a quote
1868
+ * may be written escaped (\"…\", a JSON string holding JSON).
1869
+ */
1870
+ function objectEnd(c, i) {
1871
+ const s$1 = c.s;
1872
+ const cap = Math.min(s$1.length, i + 4096);
1873
+ let depth = 0;
1874
+ let k = i;
1875
+ while (k < cap) {
1876
+ const ch = s$1[k];
1877
+ if (ch === "\"" || ch === "'") {
1878
+ const q = c.next.index(ch, k + 1);
1879
+ k = q < cap ? q + 1 : cap;
1880
+ continue;
1881
+ }
1882
+ if (ch === "{") depth++;
1883
+ else if (ch === "}" && --depth === 0) return k;
1884
+ k++;
1885
+ }
1886
+ return cap;
1887
+ }
1888
+ /**
1866
1889
  * A name starts where a run of name characters does, and is at most 64 of
1867
1890
  * them on each side of cookie: tried from every place in a long run, the
1868
1891
  * pattern read the rest of it each time.
@@ -1878,9 +1901,10 @@ function namedFields(c) {
1878
1901
  const first = m[1].toLowerCase().endsWith("set-cookie");
1879
1902
  let i = m.index + m[0].length;
1880
1903
  if (s$1[i] === "{") {
1881
- const close = s$1.indexOf("}", i);
1882
- const end = close < 0 ? Math.min(s$1.length, i + 4096) : close;
1883
- for (const d of s$1.slice(i, end).matchAll(DICT_PAIR)) {
1904
+ const end = objectEnd(c, i);
1905
+ const body = s$1.slice(i, end);
1906
+ if (isCookieObject(body)) cookieObject(c, body, i, true);
1907
+ else for (const d of body.matchAll(DICT_PAIR)) {
1884
1908
  const g = d.indices[2];
1885
1909
  value(c, d[1], i + g[0], i + g[1]);
1886
1910
  }
@@ -1921,34 +1945,53 @@ function cookiesTxt(c) {
1921
1945
  }
1922
1946
  const FLAT_OBJECT = /\{[^{}]{1,4000}\}/g;
1923
1947
  const OBJ_FIELD = /\\?"(name|value|domain|httpOnly)\\?"[ \t]*:[ \t]*(\\?"([^"\\\n]*)\\?"|true|false)/dgi;
1948
+ /** A cookie object's own fields, by the names they have whether or not their value is a literal ({\"name\":name). */
1949
+ const OBJ_NAME = /\\?["']name\\?["'][ \t]*:/i;
1950
+ const OBJ_VALUE = /\\?["']value\\?["'][ \t]*:/i;
1951
+ /**
1952
+ * Whether a text reads as a cookie object (Playwright storageState, HAR, a
1953
+ * browser extension's) and not as a dict of cookies by name: it has a name and
1954
+ * a value field, and those are the object's own, no cookie's (docs/adr/0010).
1955
+ */
1956
+ function isCookieObject(text) {
1957
+ return OBJ_NAME.test(text) && OBJ_VALUE.test(text);
1958
+ }
1959
+ /**
1960
+ * Masks what an object writes of its cookie: the `value`, and nothing else —
1961
+ * a `domain`, `path` or `expires` is no cookie and stays (docs/adr/0010). A
1962
+ * HAR header object {"name": "Cookie", "value": "…"} is a Cookie: line, and one
1963
+ * named Set-Cookie is a Set-Cookie: line. `named` says the object was found
1964
+ * under a cookie's name, where a name and a value alone are enough. False where
1965
+ * the object writes no value this can mask.
1966
+ */
1967
+ function cookieObject(c, text, base, named = false) {
1968
+ let name$1;
1969
+ let val;
1970
+ let cookieish = false;
1971
+ for (const f of text.matchAll(OBJ_FIELD)) {
1972
+ const key = f[1].toLowerCase();
1973
+ if (key === "domain" || key === "httponly") cookieish = true;
1974
+ if (f[3] === void 0) continue;
1975
+ if (key === "name") name$1 = f[3];
1976
+ if (key === "value") {
1977
+ const g = f.indices[3];
1978
+ val = [base + g[0], base + g[1]];
1979
+ }
1980
+ }
1981
+ if (name$1 === void 0 || !val) return false;
1982
+ const lower$1 = name$1.toLowerCase();
1983
+ if (lower$1 === "cookie" || lower$1 === "set-cookie") pairs(c, val[0], val[1], lower$1 === "set-cookie");
1984
+ else if (cookieish || named) value(c, name$1, val[0], val[1]);
1985
+ else return false;
1986
+ return true;
1987
+ }
1924
1988
  /**
1925
1989
  * A cookie as an export writes it (Playwright storageState, HAR, a browser
1926
1990
  * extension's): an object with name and value, and domain or httpOnly. A
1927
1991
  * HAR header object {"name": "Cookie", "value": "…"} is a Cookie: line.
1928
1992
  */
1929
1993
  function cookieObjects(c) {
1930
- const s$1 = c.s;
1931
- for (const o of s$1.matchAll(FLAT_OBJECT)) {
1932
- const text = o[0];
1933
- if (!/value/i.test(text) || !/name/i.test(text)) continue;
1934
- let name$1;
1935
- let val;
1936
- let cookieish = false;
1937
- for (const f of text.matchAll(OBJ_FIELD)) {
1938
- const key = f[1].toLowerCase();
1939
- if (key === "domain" || key === "httponly") cookieish = true;
1940
- if (f[3] === void 0) continue;
1941
- if (key === "name") name$1 = f[3];
1942
- if (key === "value") {
1943
- const g = f.indices[3];
1944
- val = [o.index + g[0], o.index + g[1]];
1945
- }
1946
- }
1947
- if (name$1 === void 0 || !val) continue;
1948
- const lower$1 = name$1.toLowerCase();
1949
- if (lower$1 === "cookie" || lower$1 === "set-cookie") pairs(c, val[0], val[1], lower$1 === "set-cookie");
1950
- else if (cookieish) value(c, name$1, val[0], val[1]);
1951
- }
1994
+ for (const o of c.s.matchAll(FLAT_OBJECT)) if (isCookieObject(o[0])) cookieObject(c, o[0], o.index);
1952
1995
  }
1953
1996
  function namedCookies(c, plan) {
1954
1997
  const s$1 = c.s;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-privacy-gateway",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "description": "Process-wide HTTP redaction gateway for DSH: masks secrets in outgoing model requests and restores placeholders in the replies, for every provider.",
6
6
  "icon": "./assets/icon.svg",