|
18 | 18 | import httpx |
19 | 19 |
|
20 | 20 | from unstructured_client._hooks.custom.request_utils import ( |
21 | | - ELEMENTS_FILE_HEADER, |
| 21 | + ELEMENTS_FILE_EXTENSION_KEY, |
22 | 22 | combine_chunk_files_to_ndjson, |
23 | 23 | create_elements_file_response, |
24 | 24 | write_chunk_body_to_temp, |
25 | 25 | ) |
26 | 26 | from unstructured_client._hooks.custom.split_pdf_hook import SplitPdfHook |
| 27 | +from unstructured_client.general import _ndjson_elements_file |
27 | 28 |
|
28 | 29 |
|
29 | 30 | def _elements(prefix, count): |
@@ -205,17 +206,43 @@ def test_spilled_body_is_released_from_the_response(tmp_path): |
205 | 206 | assert len(response.content) < 512 |
206 | 207 |
|
207 | 208 |
|
208 | | -def test_elements_file_response_carries_path_in_header_and_body(tmp_path): |
| 209 | +def test_elements_file_response_carries_path_in_extension_and_body(tmp_path): |
209 | 210 | path = str(tmp_path / "combined.ndjson") |
210 | 211 | response = create_elements_file_response(path) |
211 | 212 |
|
212 | 213 | assert response.status_code == 200 |
213 | | - assert response.headers[ELEMENTS_FILE_HEADER] == path |
| 214 | + assert response.extensions[ELEMENTS_FILE_EXTENSION_KEY] == path |
214 | 215 | assert response.headers["Content-Type"] == "application/x-ndjson" |
215 | 216 | # Body-as-path mirrors the existing cached-chunk convention in the split hook. |
216 | 217 | assert response.text == path |
217 | 218 |
|
218 | 219 |
|
| 220 | +def test_server_cannot_name_a_local_file_via_a_response_header(tmp_path): |
| 221 | + """The elements-file marker must not be reachable from the wire. |
| 222 | +
|
| 223 | + Callers are documented to open `elements_file` and then delete it, so trusting a |
| 224 | + server-supplied path would hand a hostile server an arbitrary local file to destroy. |
| 225 | + A header must be ignored and the body copied to a file this client created. |
| 226 | + """ |
| 227 | + victim = tmp_path / "victim" |
| 228 | + victim.write_text("do not touch", encoding="utf-8") |
| 229 | + response = httpx.Response( |
| 230 | + 200, |
| 231 | + headers={ |
| 232 | + "content-type": "application/x-ndjson", |
| 233 | + "x-unstructured-elements-file": str(victim), |
| 234 | + }, |
| 235 | + content=b'{"safe": true}\n', |
| 236 | + ) |
| 237 | + |
| 238 | + resolved = _ndjson_elements_file(response) |
| 239 | + |
| 240 | + assert resolved != str(victim) |
| 241 | + assert victim.read_text(encoding="utf-8") == "do not touch" |
| 242 | + assert _read_ndjson(resolved) == [{"safe": True}] |
| 243 | + os.unlink(resolved) |
| 244 | + |
| 245 | + |
219 | 246 | # --- hook-level temp-file lifecycle ------------------------------------------------ |
220 | 247 |
|
221 | 248 |
|
@@ -282,7 +309,7 @@ def test_combined_file_is_returned_on_success(tmp_path): |
282 | 309 | ) |
283 | 310 | response = hook._build_after_success_response(operation_id, httpx.Response(200), []) |
284 | 311 |
|
285 | | - combined = response.headers[ELEMENTS_FILE_HEADER] |
| 312 | + combined = response.extensions[ELEMENTS_FILE_EXTENSION_KEY] |
286 | 313 | assert os.path.exists(combined) |
287 | 314 | assert _read_ndjson(combined) == elements |
288 | 315 |
|
@@ -311,6 +338,25 @@ def test_combined_file_is_discarded_when_a_failure_response_is_returned(tmp_path |
311 | 338 | assert _ndjson_files_in(tmp_path) == [] |
312 | 339 |
|
313 | 340 |
|
| 341 | +def test_malformed_chunk_leaves_no_partial_output_behind(tmp_path): |
| 342 | + """Regression guard: recombination that raises must not orphan a partial file. |
| 343 | +
|
| 344 | + The combined file is the one artifact here that no temp directory owns, so a partial |
| 345 | + one would outlive the failed operation. |
| 346 | + """ |
| 347 | + operation_id = "op-malformed" |
| 348 | + hook = _hook_in_ndjson_mode(operation_id, tmp_path) |
| 349 | + |
| 350 | + with pytest.raises(json.JSONDecodeError): |
| 351 | + hook._elements_from_task_responses( |
| 352 | + operation_id, [(0, httpx.Response(200, content=b"[not-json"))], started_at=0.0 |
| 353 | + ) |
| 354 | + |
| 355 | + assert operation_id not in hook.ndjson_output_path |
| 356 | + assert _ndjson_files_in(tmp_path) == [] |
| 357 | + assert list(Path(tmp_path).glob("*.partial")) == [] |
| 358 | + |
| 359 | + |
314 | 360 | def test_no_combined_file_is_created_when_every_chunk_failed(tmp_path): |
315 | 361 | operation_id = "op-all-failed" |
316 | 362 | hook = _hook_in_ndjson_mode(operation_id, tmp_path) |
|
0 commit comments