fix(langchain): handle NextJS catch-all route files in path validation#34969
Open
saakshigupta2002 wants to merge 10 commits intolangchain-ai:masterfrom
Open
fix(langchain): handle NextJS catch-all route files in path validation#34969saakshigupta2002 wants to merge 10 commits intolangchain-ai:masterfrom
saakshigupta2002 wants to merge 10 commits intolangchain-ai:masterfrom
Conversation
…ages When all messages are removed from a thread using RemoveMessage(id=REMOVE_ALL_MESSAGES), the agent state may contain no AIMessage. Previously, _fetch_last_ai_and_tool_messages assumed an AIMessage always exists, causing UnboundLocalError on last_ai_index. This change returns a synthetic AIMessage with empty content and no tool calls, along with an empty tool message list, when no AIMessage is present. This ensures the agent loop exits cleanly (since len(last_ai_message.tool_calls) == 0). Fixes langchain-ai#34792
Fix 34792 unboundlocalerror
Merging this PR will not alter performance
Comparing Footnotes
|
0b2ce74 to
434da81
Compare
The path traversal check incorrectly flagged files like [...]nextauth].ts because "..." contains "..". Changed the check to only match ".." as a complete path segment (e.g., ../, /.., \..) rather than any occurrence of ".." in the path string. This fix applies to both: - FilesystemFileSearchMiddleware in langchain - _validate_path helper in langchain-anthropic Fixes langchain-ai#34961
74c5ee8 to
5f23574
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request: Fix Path Traversal False Positive on NextJS Files
Summary
This PR fixes a bug where the path traversal detection in
FilesystemFileSearchMiddlewareand_validate_pathincorrectly flagged legitimate files containing...(triple dots) as path traversal attempts.Fixes #34961
Problem
NextJS uses the
[...slug].tssyntax for catch-all dynamic routes. When using LangChain's file system middleware with a NextJS project, files like[...nextauth].tswere incorrectly blocked with a "Path traversal not allowed" error.Root Cause
The path validation logic used a simple substring check:
This incorrectly matched any path containing
.., including:[...nextauth].ts(NextJS catch-all route)spread...example.ts(file with triple dots)...special/(directory with triple dots)Solution
Changed the substring check to a regex pattern that only matches
..as a complete path segment:This pattern:
[...nextauth].ts,...spread.ts,file...name.py../secret,/..,foo/../bar,..,..\windowsChanges
Modified Files
libs/langchain_v1/langchain/agents/middleware/file_search.py_PATH_TRAVERSAL_PATTERNregex constant_validate_and_resolve_path()to use regex patternlibs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.pyreimport and_PATH_TRAVERSAL_PATTERNconstant_validate_path()to use regex patternNew Tests
libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_file_search.pytest_nextjs_catchall_route_files_allowed: Verifies NextJS catch-all routes worktest_triple_dots_in_filename_allowed: Verifies files with...in name worktest_triple_dots_in_path_allowed: Verifies directories with...worklibs/partners/anthropic/tests/unit_tests/middleware/test_anthropic_tools.pytest_nextjs_catchall_route_paths_allowed: Verifies path validation allows...Test Plan
Commands to Verify
Security Considerations
relative_to()check still validates resolved paths are within the root directory..segments (like../,/..,foo/../bar) is still blockedBackward Compatibility
This is a non-breaking bug fix. The change only allows previously incorrectly blocked paths while maintaining security for actual path traversal attempts.