把"看起来像项目内相对路径"的字符串安全拼到 PROJECT_ROOT 下。 用途:当 wiki 内容里的 [[link]] 目标拼接到磁盘路径时,必须防止 `[[raw/../etc/passwd]]` 这种越界——我们扫描时会试图打开它检查存在性, 虽然内容不会回到响应里,但仍是越界 fs 访问。 返回 None 表示拒绝(绝对路径 / 越界 / 空)。否则返回 PROJECT_ROOT 下的 绝对路径。**只校验路径合法性,不要求文件必须存在。**
(rel: str)
| 1416 | |
| 1417 | |
| 1418 | def _safe_join_under_root(rel: str) -> Path | None: |
| 1419 | """把"看起来像项目内相对路径"的字符串安全拼到 PROJECT_ROOT 下。 |
| 1420 | |
| 1421 | 用途:当 wiki 内容里的 [[link]] 目标拼接到磁盘路径时,必须防止 |
| 1422 | `[[raw/../etc/passwd]]` 这种越界——我们扫描时会试图打开它检查存在性, |
| 1423 | 虽然内容不会回到响应里,但仍是越界 fs 访问。 |
| 1424 | |
| 1425 | 返回 None 表示拒绝(绝对路径 / 越界 / 空)。否则返回 PROJECT_ROOT 下的 |
| 1426 | 绝对路径。**只校验路径合法性,不要求文件必须存在。** |
| 1427 | """ |
| 1428 | if not isinstance(rel, str) or not rel: |
| 1429 | return None |
| 1430 | if Path(rel).is_absolute(): |
| 1431 | return None |
| 1432 | candidate = (PROJECT_ROOT / rel).resolve() |
| 1433 | try: |
| 1434 | candidate.relative_to(PROJECT_ROOT.resolve()) |
| 1435 | except ValueError: |
| 1436 | return None |
| 1437 | return candidate |
| 1438 | |
| 1439 | |
| 1440 | def strip_workspace_prefix(arg: str) -> str: |
no outgoing calls
no test coverage detected