@zeldrisho/pi-web-search 0.14.5 → 0.14.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.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.14.6] - 2026-11-09
11
+
12
+ ### Security
13
+
14
+ - Refuse provider redirects to prevent subscription tokens from being forwarded to another origin.
15
+ - Omit untrusted provider error bodies, which could echo credentials or search queries.
16
+
10
17
  ## [0.14.5] - 2026-11-01
11
18
 
12
19
  ### Changed
@@ -206,7 +213,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
206
213
 
207
214
  - Add bounded Brave web search ([cbc9491](https://github.com/zeldrisho/pi-packages/commit/cbc9491dce36555ab91b6bef203ec8b380596a89))
208
215
 
209
- [Unreleased]: https://github.com/zeldrisho/pi-packages/compare/pi-web-search-v0.14.5...HEAD
216
+ [Unreleased]: https://github.com/zeldrisho/pi-packages/compare/pi-web-search-v0.14.6...HEAD
217
+ [0.14.6]: https://github.com/zeldrisho/pi-packages/compare/pi-web-search-v0.14.5...pi-web-search-v0.14.6
210
218
  [0.14.5]: https://github.com/zeldrisho/pi-packages/compare/pi-web-search-v0.14.4...pi-web-search-v0.14.5
211
219
  [0.14.4]: https://github.com/zeldrisho/pi-packages/compare/pi-web-search-v0.14.3...pi-web-search-v0.14.4
212
220
  [0.14.3]: https://github.com/zeldrisho/pi-packages/compare/pi-web-search-v0.14.2...pi-web-search-v0.14.3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zeldrisho/pi-web-search",
3
- "version": "0.14.5",
3
+ "version": "0.14.6",
4
4
  "description": "Pi extension for bounded Brave web search with generated-answer fallback",
5
5
  "keywords": [
6
6
  "brave-search",
package/src/provider.ts CHANGED
@@ -2,8 +2,6 @@ const REQUEST_TIMEOUT_MS = 20_000;
2
2
 
3
3
  const SEARCH_MAX_RESPONSE_BYTES = 2_000_000;
4
4
 
5
- const SEARCH_ERROR_EXCERPT_BYTES = 8_192;
6
-
7
5
  /**
8
6
  * Normalizes text by removing HTML tags, collapsing whitespace, and truncating to a maximum length.
9
7
  *
@@ -23,23 +21,18 @@ export function normalizeText(value: string, maxLength: number): string {
23
21
  }
24
22
 
25
23
  /**
26
- * Reads response body bytes with size limits and optional truncation.
24
+ * Reads response body bytes with size limits.
27
25
  *
28
26
  * @param response - HTTP response to read
29
27
  * @param maxBytes - Maximum bytes to read
30
- * @param truncate - Whether to truncate at limit or throw error
31
28
  * @returns Promise resolving to the response bytes
32
- * @throws {Error} When response exceeds maxBytes and truncate is false
29
+ * @throws {Error} When response exceeds maxBytes
33
30
  */
34
- async function readResponseBytes(
35
- response: Response,
36
- maxBytes: number,
37
- truncate: boolean,
38
- ): Promise<Uint8Array> {
31
+ async function readResponseBytes(response: Response, maxBytes: number): Promise<Uint8Array> {
39
32
  const contentLength = response.headers.get("content-length");
40
33
  const declared = contentLength === null ? Number.NaN : Number(contentLength);
41
34
 
42
- if (!truncate && Number.isFinite(declared) && declared > maxBytes && declared > 0) {
35
+ if (Number.isFinite(declared) && declared > maxBytes && declared > 0) {
43
36
  await response.body?.cancel().catch(() => undefined);
44
37
  throw new Error("Search provider response is too large.");
45
38
  }
@@ -58,14 +51,7 @@ async function readResponseBytes(
58
51
  const remaining = maxBytes - total;
59
52
 
60
53
  if (value.byteLength > remaining) {
61
- if (truncate && remaining > 0) chunks.push(value.subarray(0, remaining));
62
54
  await reader.cancel().catch(() => undefined);
63
-
64
- if (truncate) {
65
- total = maxBytes;
66
- break;
67
- }
68
-
69
55
  throw new Error("Search provider response is too large.");
70
56
  }
71
57
 
@@ -92,16 +78,11 @@ async function readResponseBytes(
92
78
  *
93
79
  * @param response - HTTP response to read
94
80
  * @param maxBytes - Maximum bytes to read
95
- * @param truncate - Whether to truncate at limit or throw error
96
81
  * @returns Promise resolving to the decoded text
97
82
  * @throws {Error} When response exceeds maxBytes and truncate is false
98
83
  */
99
- async function readResponseText(
100
- response: Response,
101
- maxBytes: number,
102
- truncate = false,
103
- ): Promise<string> {
104
- return new TextDecoder().decode(await readResponseBytes(response, maxBytes, truncate));
84
+ async function readResponseText(response: Response, maxBytes: number): Promise<string> {
85
+ return new TextDecoder().decode(await readResponseBytes(response, maxBytes));
105
86
  }
106
87
 
107
88
  /**
@@ -132,21 +113,14 @@ export async function requestText(
132
113
  if (signal?.aborted) controller.abort();
133
114
 
134
115
  try {
135
- const response = await fetch(url, { ...init, signal: controller.signal });
116
+ // Custom subscription-token headers are not stripped by fetch on cross-origin
117
+ // redirects. Never follow a provider-controlled redirect with credentials.
118
+ const response = await fetch(url, { ...init, redirect: "error", signal: controller.signal });
136
119
 
137
120
  if (!response.ok) {
138
- let body = "";
139
-
140
- try {
141
- body = normalizeText(
142
- await readResponseText(response, SEARCH_ERROR_EXCERPT_BYTES, true),
143
- 500,
144
- );
145
- } catch {
146
- // Preserve the useful HTTP status when an untrusted error body cannot be read.
147
- }
148
-
149
- throw new Error(`Provider returned HTTP ${response.status}${body ? `: ${body}` : ""}`);
121
+ // Error bodies are untrusted and can echo credentials or private queries.
122
+ void response.body?.cancel().catch(() => undefined);
123
+ throw new Error(`Provider returned HTTP ${response.status}`);
150
124
  }
151
125
 
152
126
  return await readResponseText(response, SEARCH_MAX_RESPONSE_BYTES);