Repository navigation
fix(api): gate sandbox artifact download on agent session ownership - #16169
yuzhichang merged 3 commits into
Conversation
Require CodeExec artifact filenames to appear in the caller's api_4_conversation messages and pass UserCanvasService.accessible before reading from SANDBOX_ARTIFACT_BUCKET. Fixes infiniflow#16168 Co-authored-by: Cursor <[email protected]>
📝 WalkthroughWalkthroughThe PR fixes a cross-user artifact leakage vulnerability on ChangesSandbox Artifact Authorization
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (3)
test/testcases/test_web_api/test_document_app/test_document_metadata.py (3)
627-634: ⚡ Quick winAdd
@pytest.mark.p2priority marker for consistency.Other security-related unit tests in this class use
@pytest.mark.p2. This test should follow the same convention. As per coding guidelines, use pytest with priority markers (p1/p2/p3) for Python testing.Proposed fix
+ `@pytest.mark.p2` def test_get_artifact_denied_without_session_reference_unit(self, document_app_module, monkeypatch):🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/testcases/test_web_api/test_document_app/test_document_metadata.py` around lines 627 - 634, The test method test_get_artifact_denied_without_session_reference_unit is missing the `@pytest.mark.p2` priority marker that other security-related unit tests in this class use. Add the `@pytest.mark.p2` decorator above the test method definition to maintain consistency with the coding guidelines for pytest priority markers.Source: Coding guidelines
636-644: ⚡ Quick winAdd
@pytest.mark.p2priority marker for consistency.Same as above—this security authorization test should have the priority marker.
Proposed fix
+ `@pytest.mark.p2` def test_get_artifact_denied_when_agent_not_accessible_unit(self, document_app_module, monkeypatch):🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/testcases/test_web_api/test_document_app/test_document_metadata.py` around lines 636 - 644, Add the `@pytest.mark.p2` priority marker decorator above the test_get_artifact_denied_when_agent_not_accessible_unit method definition to maintain consistency with other similar security authorization tests in the test suite. The marker should be placed on a new line immediately before the def statement of the method.Source: Coding guidelines
646-695: ⚡ Quick winAdd
@pytest.mark.p2priority marker for consistency.This test covers critical security authorization paths and should have the priority marker like other tests in the class.
Proposed fix
+ `@pytest.mark.p2` def test_get_artifact_success_and_missing_blob_unit(self, document_app_module, monkeypatch):🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/testcases/test_web_api/test_document_app/test_document_metadata.py` around lines 646 - 695, The test method test_get_artifact_success_and_missing_blob_unit is missing the pytest priority marker that should be applied for consistency with other tests in the class. Add the `@pytest.mark.p2` decorator on the line immediately before the method definition to mark this test with p2 priority, ensuring it aligns with the priority markers used throughout the test class.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@test/testcases/test_web_api/test_document_app/test_document_metadata.py`:
- Around line 627-634: The test method
test_get_artifact_denied_without_session_reference_unit is missing the
`@pytest.mark.p2` priority marker that other security-related unit tests in this
class use. Add the `@pytest.mark.p2` decorator above the test method definition to
maintain consistency with the coding guidelines for pytest priority markers.
- Around line 636-644: Add the `@pytest.mark.p2` priority marker decorator above
the test_get_artifact_denied_when_agent_not_accessible_unit method definition to
maintain consistency with other similar security authorization tests in the test
suite. The marker should be placed on a new line immediately before the def
statement of the method.
- Around line 646-695: The test method
test_get_artifact_success_and_missing_blob_unit is missing the pytest priority
marker that should be applied for consistency with other tests in the class. Add
the `@pytest.mark.p2` decorator on the line immediately before the method
definition to mark this test with p2 priority, ensuring it aligns with the
priority markers used throughout the test class.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 6a98adf4-a358-4fbf-874d-64f7578af738
📒 Files selected for processing (2)
api/apps/restful_apis/document_api.pytest/testcases/test_web_api/test_document_app/test_document_metadata.py
|
Hi, @wangq8 , @Haruko386 , @JinHai-CN @yingfeng @xugangqiang @buua436 |
…16169) Fixes #16168 ## Summary - Add session-scoped authorization for `GET /api/v1/documents/artifact/<filename>` - Allow download only when the artifact filename appears in the caller's `api_4_conversation` message and `UserCanvasService.accessible(dialog_id, user_id)` passes - Deny with generic `"Artifact not found."` before storage access (no cross-user enumeration) - Return 4xx when the blob is missing (existing behavior preserved) ## Approach Sandbox artifacts are runtime CodeExec outputs, not KB documents — this uses the same session gate pattern as `agent_chat_completion`, not `DocumentService.accessible`. ## Test plan - [x] Unit: denied when filename not referenced in user sessions - [x] Unit: denied when agent canvas is not accessible - [x] Unit: authorized user receives bytes; missing blob returns `"Artifact not found."` - [ ] `pytest test/testcases/test_web_api/test_document_app/test_document_metadata.py -k get_artifact` --------- Co-authored-by: Cursor <[email protected]> Co-authored-by: Zhichang Yu <[email protected]>
Ports 4 M-effort PRs from .claude/plans/review-and-port-py-agent-changes.md Execution snapshot (all merged upstream) to the Go agent side. Group 4 - API auth ports - infiniflow#16169 Sandbox artifact ownership: add UserCanvasDAO.Accessible (owner-or-team predicate) and DocumentService.sandboxArtifactAccessible to gate GET /api/v1/documents/artifact/<filename>. Query API4Conversation for sessions owned by userID whose message blob references the filename (or its documents/artifact/<name> URL form); require at least one accessible canvas. Handler now fetches the current user and passes userID into the service; denial returns ErrArtifactNotFound to avoid cross-user enumeration. - infiniflow#15457 Tenant ownership on agentbots: Go side already had the loadCanvas IDOR guard at the entry of both AgentbotInputs and AgentbotCompletion. Added a cross-tenant regression test (tenant-B asks for tenant-A's canvas -> ErrUserCanvasNotFound). - infiniflow#15145 Rerun agent document access: add DocumentService.Accessible (loads doc, checks kbDAO.Accessible). Wire documentService into AgentHandler via WithDocumentService (nil-tolerant, preserves the existing NewAgentHandler signature for tests). RerunAgent returns "Document not found." when accessible check fails; cmd/server_main.go wires documentService after construction. Group 2 - thinking switch - infiniflow#15446 component portion: add Thinking field to LLMParam, LLMInput, and ChatInvokeRequest. mergeLLMParam reads inputs["thinking"] with a whitelist of {"enabled","disabled"} (matching python llm.py:78-79); arbitrary strings are rejected. The provider policy portion (Qwen/Kimi/GLM in rag/llm/chat_model.py) belongs to internal/llm/ and is a separate porting stream. Total: 4 new tests across 3 modified test files. All touched package tests pass (1697 + 4 skipped). The 1 failing test (TestAgent_ReActExhaustsSteps) is the pre-existing flaky test that also fails on the baseline without my changes. Refs: .claude/plans/review-and-port-py-agent-changes.md Refs: infiniflow#16169 infiniflow#15457 infiniflow#15145 infiniflow#15446
Ports 4 M-effort PRs from .claude/plans/review-and-port-py-agent-changes.md Execution snapshot (all merged upstream) to the Go agent side. Group 4 - API auth ports - infiniflow#16169 Sandbox artifact ownership: add UserCanvasDAO.Accessible (owner-or-team predicate) and DocumentService.sandboxArtifactAccessible to gate GET /api/v1/documents/artifact/<filename>. Query API4Conversation for sessions owned by userID whose message blob references the filename (or its documents/artifact/<name> URL form); require at least one accessible canvas. Handler now fetches the current user and passes userID into the service; denial returns ErrArtifactNotFound to avoid cross-user enumeration. - infiniflow#15457 Tenant ownership on agentbots: Go side already had the loadCanvas IDOR guard at the entry of both AgentbotInputs and AgentbotCompletion. Added a cross-tenant regression test (tenant-B asks for tenant-A's canvas -> ErrUserCanvasNotFound). - infiniflow#15145 Rerun agent document access: add DocumentService.Accessible (loads doc, checks kbDAO.Accessible). Wire documentService into AgentHandler via WithDocumentService (nil-tolerant, preserves the existing NewAgentHandler signature for tests). RerunAgent returns "Document not found." when accessible check fails; cmd/server_main.go wires documentService after construction. Group 2 - thinking switch - infiniflow#15446 component portion: add Thinking field to LLMParam, LLMInput, and ChatInvokeRequest. mergeLLMParam reads inputs["thinking"] with a whitelist of {"enabled","disabled"} (matching python llm.py:78-79); arbitrary strings are rejected. The provider policy portion (Qwen/Kimi/GLM in rag/llm/chat_model.py) belongs to internal/llm/ and is a separate porting stream. Total: 4 new tests across 3 modified test files. All touched package tests pass (1697 + 4 skipped). The 1 failing test (TestAgent_ReActExhaustsSteps) is the pre-existing flaky test that also fails on the baseline without my changes. Refs: .claude/plans/review-and-port-py-agent-changes.md Refs: infiniflow#16169 infiniflow#15457 infiniflow#15145 infiniflow#15446
Ports 4 M-effort PRs from .claude/plans/review-and-port-py-agent-changes.md Execution snapshot (all merged upstream) to the Go agent side. Group 4 - API auth ports - infiniflow#16169 Sandbox artifact ownership: add UserCanvasDAO.Accessible (owner-or-team predicate) and DocumentService.sandboxArtifactAccessible to gate GET /api/v1/documents/artifact/<filename>. Query API4Conversation for sessions owned by userID whose message blob references the filename (or its documents/artifact/<name> URL form); require at least one accessible canvas. Handler now fetches the current user and passes userID into the service; denial returns ErrArtifactNotFound to avoid cross-user enumeration. - infiniflow#15457 Tenant ownership on agentbots: Go side already had the loadCanvas IDOR guard at the entry of both AgentbotInputs and AgentbotCompletion. Added a cross-tenant regression test (tenant-B asks for tenant-A's canvas -> ErrUserCanvasNotFound). - infiniflow#15145 Rerun agent document access: add DocumentService.Accessible (loads doc, checks kbDAO.Accessible). Wire documentService into AgentHandler via WithDocumentService (nil-tolerant, preserves the existing NewAgentHandler signature for tests). RerunAgent returns "Document not found." when accessible check fails; cmd/server_main.go wires documentService after construction. Group 2 - thinking switch - infiniflow#15446 component portion: add Thinking field to LLMParam, LLMInput, and ChatInvokeRequest. mergeLLMParam reads inputs["thinking"] with a whitelist of {"enabled","disabled"} (matching python llm.py:78-79); arbitrary strings are rejected. The provider policy portion (Qwen/Kimi/GLM in rag/llm/chat_model.py) belongs to internal/llm/ and is a separate porting stream. Total: 4 new tests across 3 modified test files. All touched package tests pass (1697 + 4 skipped). The 1 failing test (TestAgent_ReActExhaustsSteps) is the pre-existing flaky test that also fails on the baseline without my changes. Refs: .claude/plans/review-and-port-py-agent-changes.md Refs: infiniflow#16169 infiniflow#15457 infiniflow#15145 infiniflow#15446
Ports 4 M-effort PRs from .claude/plans/review-and-port-py-agent-changes.md Execution snapshot (all merged upstream) to the Go agent side. Group 4 - API auth ports - infiniflow#16169 Sandbox artifact ownership: add UserCanvasDAO.Accessible (owner-or-team predicate) and DocumentService.sandboxArtifactAccessible to gate GET /api/v1/documents/artifact/<filename>. Query API4Conversation for sessions owned by userID whose message blob references the filename (or its documents/artifact/<name> URL form); require at least one accessible canvas. Handler now fetches the current user and passes userID into the service; denial returns ErrArtifactNotFound to avoid cross-user enumeration. - infiniflow#15457 Tenant ownership on agentbots: Go side already had the loadCanvas IDOR guard at the entry of both AgentbotInputs and AgentbotCompletion. Added a cross-tenant regression test (tenant-B asks for tenant-A's canvas -> ErrUserCanvasNotFound). - infiniflow#15145 Rerun agent document access: add DocumentService.Accessible (loads doc, checks kbDAO.Accessible). Wire documentService into AgentHandler via WithDocumentService (nil-tolerant, preserves the existing NewAgentHandler signature for tests). RerunAgent returns "Document not found." when accessible check fails; cmd/server_main.go wires documentService after construction. Group 2 - thinking switch - infiniflow#15446 component portion: add Thinking field to LLMParam, LLMInput, and ChatInvokeRequest. mergeLLMParam reads inputs["thinking"] with a whitelist of {"enabled","disabled"} (matching python llm.py:78-79); arbitrary strings are rejected. The provider policy portion (Qwen/Kimi/GLM in rag/llm/chat_model.py) belongs to internal/llm/ and is a separate porting stream. Total: 4 new tests across 3 modified test files. All touched package tests pass (1697 + 4 skipped). The 1 failing test (TestAgent_ReActExhaustsSteps) is the pre-existing flaky test that also fails on the baseline without my changes. Refs: .claude/plans/review-and-port-py-agent-changes.md Refs: infiniflow#16169 infiniflow#15457 infiniflow#15145 infiniflow#15446
) Mirrors 14 merged upstream PRs into the Go agent port. PRs ported: - #15609 ExeSQL SSRF guard + DNS pin - #15436 HTTP timeout on external API tools - #16363 be_output restore + DeepL error path - #15644 switch no longer matches empty condition - #15374 session_id bind to path agent_id (DAO idor guard) - #16169 sandbox artifact ownership gate - #15457 tenant ownership on agentbots - #15145 rerun agent document access check - #15446 thinking switch (component portion; provider policy lives in internal/llm) - #15426 Invoke URL/proxy SSRF + DNS pin + no-redirects - #15238 agentbot thinking-logs beta endpoint - #14589 UserFillUp SSE event propagation - #14890 anonymous webhook opt-in - #15068 PipelineChunker new component (text/file_ref/parser_id dispatch; file-format extraction is a follow-up) 40 files, +2355 / -58 lines. 33 new tests, all targeted package suites pass (1721 + 4 skipped); 1 pre-existing flaky test unrelated.
…sion/user + input/output to Langfuse for agent runs Rebuilt on current upstream/main after force-push; cherry-pick of the 7 PR-original commits collapsed into a single net diff to avoid conflicts with intervening commits pulled in by repeated merges of upstream/main. Original PR work (commits dropped during rebuild because their content already lives on upstream/main or was a merge of upstream/main): c7339ae Enhance LLMService with token usage reporting 0266cf0 Implement token usage tracking and Langfuse integration ab2b267 Include session_id in run_kwargs 8bf937c Implement token usage tracking in chat model c955988 Enhance token tracking with context variables and functions 3e88ed5 fix(agent): address CodeRabbit review on token usage tracking 74b8c8c fix(agent): address CodeRabbit follow-up review Forced-upstream duplicates dropped (already merged to main): 70546ea (infiniflow#16396), 5b09910 (infiniflow#16403), 5d7f0fd (infiniflow#16405), 730f33b (infiniflow#16407), a06343e (infiniflow#16408), f90be41 (infiniflow#16410), c4fe68e (infiniflow#16409), d4a0e71 (infiniflow#14773), ef42fd7 (infiniflow#15436), ab1015d (infiniflow#16363), db188cc (infiniflow#15446), 56ece68 (infiniflow#15609), 2589b6e (infiniflow#15644), 74c2944 (infiniflow#15374), 78832ff (infiniflow#14985), bf18b59 (infiniflow#15457), 6c87225 (infiniflow#14890), e16d1a0 (infiniflow#15426), 4712fcb (infiniflow#16169), 0536233 (infiniflow#14589), 08c807a (infiniflow#15145) Plus 18 'Merge branch main into main' commits dropped (polluted topology).
…sion/user + input/output to Langfuse for agent runs Rebuilt on current upstream/main after force-push; cherry-pick of the 7 PR-original commits collapsed into a single net diff to avoid conflicts with intervening commits pulled in by repeated merges of upstream/main. Original PR work (commits dropped during rebuild because their content already lives on upstream/main or was a merge of upstream/main): c7339ae Enhance LLMService with token usage reporting 0266cf0 Implement token usage tracking and Langfuse integration ab2b267 Include session_id in run_kwargs 8bf937c Implement token usage tracking in chat model c955988 Enhance token tracking with context variables and functions 3e88ed5 fix(agent): address CodeRabbit review on token usage tracking 74b8c8c fix(agent): address CodeRabbit follow-up review Forced-upstream duplicates dropped (already merged to main): 70546ea (infiniflow#16396), 5b09910 (infiniflow#16403), 5d7f0fd (infiniflow#16405), 730f33b (infiniflow#16407), a06343e (infiniflow#16408), f90be41 (infiniflow#16410), c4fe68e (infiniflow#16409), d4a0e71 (infiniflow#14773), ef42fd7 (infiniflow#15436), ab1015d (infiniflow#16363), db188cc (infiniflow#15446), 56ece68 (infiniflow#15609), 2589b6e (infiniflow#15644), 74c2944 (infiniflow#15374), 78832ff (infiniflow#14985), bf18b59 (infiniflow#15457), 6c87225 (infiniflow#14890), e16d1a0 (infiniflow#15426), 4712fcb (infiniflow#16169), 0536233 (infiniflow#14589), 08c807a (infiniflow#15145) Plus 18 'Merge branch main into main' commits dropped (polluted topology). Co-authored-by: yzc <[email protected]>
…iniflow#16455) Mirrors 14 merged upstream PRs into the Go agent port. PRs ported: - infiniflow#15609 ExeSQL SSRF guard + DNS pin - infiniflow#15436 HTTP timeout on external API tools - infiniflow#16363 be_output restore + DeepL error path - infiniflow#15644 switch no longer matches empty condition - infiniflow#15374 session_id bind to path agent_id (DAO idor guard) - infiniflow#16169 sandbox artifact ownership gate - infiniflow#15457 tenant ownership on agentbots - infiniflow#15145 rerun agent document access check - infiniflow#15446 thinking switch (component portion; provider policy lives in internal/llm) - infiniflow#15426 Invoke URL/proxy SSRF + DNS pin + no-redirects - infiniflow#15238 agentbot thinking-logs beta endpoint - infiniflow#14589 UserFillUp SSE event propagation - infiniflow#14890 anonymous webhook opt-in - infiniflow#15068 PipelineChunker new component (text/file_ref/parser_id dispatch; file-format extraction is a follow-up) 40 files, +2355 / -58 lines. 33 new tests, all targeted package suites pass (1721 + 4 skipped); 1 pre-existing flaky test unrelated.
Fixes #16168
Summary
GET /api/v1/documents/artifact/<filename>api_4_conversationmessage andUserCanvasService.accessible(dialog_id, user_id)passes"Artifact not found."before storage access (no cross-user enumeration)Approach
Sandbox artifacts are runtime CodeExec outputs, not KB documents — this uses the same session gate pattern as
agent_chat_completion, notDocumentService.accessible.Test plan
"Artifact not found."pytest test/testcases/test_web_api/test_document_app/test_document_metadata.py -k get_artifact