diff --git a/src/crewai/task.py b/src/crewai/task.py index 30ab79c00..a63bde57d 100644 --- a/src/crewai/task.py +++ b/src/crewai/task.py @@ -179,6 +179,7 @@ class Task(BaseModel): _execution_span: Optional[Span] = PrivateAttr(default=None) _original_description: Optional[str] = PrivateAttr(default=None) _original_expected_output: Optional[str] = PrivateAttr(default=None) + _original_output_file: Optional[str] = PrivateAttr(default=None) _thread: Optional[threading.Thread] = PrivateAttr(default=None) _execution_time: Optional[float] = PrivateAttr(default=None) @@ -213,8 +214,46 @@ class Task(BaseModel): @field_validator("output_file") @classmethod - def output_file_validation(cls, value: str) -> str: - """Validate the output file path by removing the / from the beginning of the path.""" + def output_file_validation(cls, value: Optional[str]) -> Optional[str]: + """Validate the output file path. + + Args: + value: The output file path to validate. Can be None or a string. + If the path contains template variables (e.g. {var}), leading slashes are preserved. + For regular paths, leading slashes are stripped. + + Returns: + The validated and potentially modified path, or None if no path was provided. + + Raises: + ValueError: If the path contains invalid characters, path traversal attempts, + or other security concerns. + """ + if value is None: + return None + + # Basic security checks + if ".." in value: + raise ValueError("Path traversal attempts are not allowed in output_file paths") + + # Check for shell expansion first + if value.startswith('~') or value.startswith('$'): + raise ValueError("Shell expansion characters are not allowed in output_file paths") + + # Then check other shell special characters + if any(char in value for char in ['|', '>', '<', '&', ';']): + raise ValueError("Shell special characters are not allowed in output_file paths") + + # Don't strip leading slash if it's a template path with variables + if "{" in value or "}" in value: + # Validate template variable format + template_vars = [part.split("}")[0] for part in value.split("{")[1:]] + for var in template_vars: + if not var.isidentifier(): + raise ValueError(f"Invalid template variable name: {var}") + return value + + # Strip leading slash for regular paths if value.startswith("/"): return value[1:] return value @@ -393,27 +432,89 @@ class Task(BaseModel): tasks_slices = [self.description, output] return "\n".join(tasks_slices) - def interpolate_inputs(self, inputs: Dict[str, Any]) -> None: - """Interpolate inputs into the task description and expected output.""" + def interpolate_inputs(self, inputs: Dict[str, Union[str, int, float]]) -> None: + """Interpolate inputs into the task description, expected output, and output file path. + + Args: + inputs: Dictionary mapping template variables to their values. + Supported value types are strings, integers, and floats. + + Raises: + ValueError: If a required template variable is missing from inputs. + """ if self._original_description is None: self._original_description = self.description if self._original_expected_output is None: self._original_expected_output = self.expected_output + if self.output_file is not None and self._original_output_file is None: + self._original_output_file = self.output_file - if inputs: + if not inputs: + return + + try: self.description = self._original_description.format(**inputs) + except KeyError as e: + raise ValueError(f"Missing required template variable '{e.args[0]}' in description") from e + except ValueError as e: + raise ValueError(f"Error interpolating description: {str(e)}") from e + + try: self.expected_output = self.interpolate_only( input_string=self._original_expected_output, inputs=inputs ) + except (KeyError, ValueError) as e: + raise ValueError(f"Error interpolating expected_output: {str(e)}") from e - def interpolate_only(self, input_string: str, inputs: Dict[str, Any]) -> str: - """Interpolate placeholders (e.g., {key}) in a string while leaving JSON untouched.""" - escaped_string = input_string.replace("{", "{{").replace("}", "}}") + if self.output_file is not None: + try: + self.output_file = self.interpolate_only( + input_string=self._original_output_file, inputs=inputs + ) + except (KeyError, ValueError) as e: + raise ValueError(f"Error interpolating output_file path: {str(e)}") from e - for key in inputs.keys(): - escaped_string = escaped_string.replace(f"{{{{{key}}}}}", f"{{{key}}}") + def interpolate_only(self, input_string: Optional[str], inputs: Dict[str, Union[str, int, float]]) -> str: + """Interpolate placeholders (e.g., {key}) in a string while leaving JSON untouched. + + Args: + input_string: The string containing template variables to interpolate. + Can be None or empty, in which case an empty string is returned. + inputs: Dictionary mapping template variables to their values. + Supported value types are strings, integers, and floats. + If input_string is empty or has no placeholders, inputs can be empty. + + Returns: + The interpolated string with all template variables replaced with their values. + Empty string if input_string is None or empty. + + Raises: + ValueError: If a required template variable is missing from inputs. + KeyError: If a template variable is not found in the inputs dictionary. + """ + if input_string is None or not input_string: + return "" + if "{" not in input_string and "}" not in input_string: + return input_string + if not inputs: + raise ValueError("Inputs dictionary cannot be empty when interpolating variables") - return escaped_string.format(**inputs) + try: + # Validate input types + for key, value in inputs.items(): + if not isinstance(value, (str, int, float)): + raise ValueError(f"Value for key '{key}' must be a string, integer, or float, got {type(value).__name__}") + + escaped_string = input_string.replace("{", "{{").replace("}", "}}") + + for key in inputs.keys(): + escaped_string = escaped_string.replace(f"{{{{{key}}}}}", f"{{{key}}}") + + return escaped_string.format(**inputs) + except KeyError as e: + raise KeyError(f"Template variable '{e.args[0]}' not found in inputs dictionary") from e + except ValueError as e: + raise ValueError(f"Error during string interpolation: {str(e)}") from e def increment_tools_errors(self) -> None: """Increment the tools errors counter.""" diff --git a/tests/cassettes/test_crew_output_file_end_to_end.yaml b/tests/cassettes/test_crew_output_file_end_to_end.yaml new file mode 100644 index 000000000..2cbd6d9c3 --- /dev/null +++ b/tests/cassettes/test_crew_output_file_end_to_end.yaml @@ -0,0 +1,243 @@ +interactions: +- request: + body: !!binary | + CuIcCiQKIgoMc2VydmljZS5uYW1lEhIKEGNyZXdBSS10ZWxlbWV0cnkSuRwKEgoQY3Jld2FpLnRl + bGVtZXRyeRKjBwoQXK7w4+uvyEkrI9D5qyvcJxII5UmQ7hmczdIqDENyZXcgQ3JlYXRlZDABOfxQ + /hs4jBUYQUi3DBw4jBUYShoKDmNyZXdhaV92ZXJzaW9uEggKBjAuODYuMEoaCg5weXRob25fdmVy + c2lvbhIICgYzLjEyLjdKLgoIY3Jld19rZXkSIgogYzk3YjVmZWI1ZDFiNjZiYjU5MDA2YWFhMDFh + MjljZDZKMQoHY3Jld19pZBImCiRkZjY3NGMwYi1hOTc0LTQ3NTAtYjlkMS0yZWQxNjM3MzFiNTZK + HAoMY3Jld19wcm9jZXNzEgwKCnNlcXVlbnRpYWxKEQoLY3Jld19tZW1vcnkSAhAAShoKFGNyZXdf + bnVtYmVyX29mX3Rhc2tzEgIYAUobChVjcmV3X251bWJlcl9vZl9hZ2VudHMSAhgBStECCgtjcmV3 + X2FnZW50cxLBAgq+Alt7ImtleSI6ICIwN2Q5OWI2MzA0MTFkMzVmZDkwNDdhNTMyZDUzZGRhNyIs + ICJpZCI6ICI5MDYwYTQ2Zi02MDY3LTQ1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MzJlNjg0NDFkMzI3YmRmSjEKB3Rhc2tfaWQSJgokMGI5NGIxNjQt + Mzk1OS00YWZhLTk2ODgtYmM2YTBlYzIxZjM4egIYAYUBAAEAAA== + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '3685' + Content-Type: + - application/x-protobuf + User-Agent: + - OTel-OTLP-Exporter-Python/1.27.0 + method: POST + uri: https://telemetry.crewai.com:4319/v1/traces + response: + body: + string: "\n\0" + headers: + Content-Length: + - '2' + Content-Type: + - application/x-protobuf + Date: + - Sun, 29 Dec 2024 04:43:27 GMT + status: + code: 200 + message: OK +- request: + body: '{"messages": [{"role": "system", "content": "You are Researcher. You have + extensive AI research experience.\nYour personal goal is: Analyze AI topics\nTo + give my best complete final answer to the task use the exact following format:\n\nThought: + I now can give a great answer\nFinal Answer: Your final answer must be the great + and the most complete as possible, it must be outcome described.\n\nI MUST use + these formats, my job depends on it!"}, {"role": "user", "content": "\nCurrent + Task: Explain the advantages of AI.\n\nThis is the expect criteria for your + final answer: A summary of the main advantages, bullet points recommended.\nyou + MUST return the actual complete content as the final answer, not a summary.\n\nBegin! + This is VERY important to you, use the tools available and give your best Final + Answer, your job depends on it!\n\nThought:"}], "model": "gpt-4o-mini", "stop": + ["\nObservation:"], "stream": false}' + headers: + accept: + - application/json + accept-encoding: + - gzip, deflate + connection: + - keep-alive + content-length: + - '922' + content-type: + - application/json + cookie: + - _cfuvid=eff7OIkJ0zWRunpA6z67LHqscmSe6XjNxXiPw1R3xCc-1733770413538-0.0.1.1-604800000 + host: + - api.openai.com + user-agent: + - OpenAI/Python 1.52.1 + x-stainless-arch: + - x64 + x-stainless-async: + - 'false' + x-stainless-lang: + - python + x-stainless-os: + - Linux + x-stainless-package-version: + - 1.52.1 + x-stainless-raw-response: + - 'true' + x-stainless-retry-count: + - '0' + x-stainless-runtime: + - CPython + x-stainless-runtime-version: + - 3.12.7 + method: POST + uri: https://api.openai.com/v1/chat/completions + response: + content: "{\n \"id\": \"chatcmpl-AjfR6FDuTw7NGzy8w7sxjvOkUQlru\",\n \"object\": + \"chat.completion\",\n \"created\": 1735447404,\n \"model\": \"gpt-4o-mini-2024-07-18\",\n + \ \"choices\": [\n {\n \"index\": 0,\n \"message\": {\n \"role\": + \"assistant\",\n \"content\": \"I now can give a great answer \\nFinal + Answer: \\n**Advantages of AI** \\n\\n1. **Increased Efficiency and Productivity** + \ \\n - AI systems can process large amounts of data quickly and accurately, + leading to faster decision-making and increased productivity in various sectors.\\n\\n2. + **Cost Savings** \\n - Automation of repetitive and time-consuming tasks + reduces labor costs and increases operational efficiency, allowing businesses + to allocate resources more effectively.\\n\\n3. **Enhanced Data Analysis** \\n + \ - AI excels at analyzing big data, identifying patterns, and providing insights + that support better strategic planning and business decision-making.\\n\\n4. + **24/7 Availability** \\n - AI solutions, such as chatbots and virtual assistants, + operate continuously without breaks, offering constant support and customer + service, enhancing user experience.\\n\\n5. **Personalization** \\n - AI + enables the customization of content, products, and services based on user preferences + and behaviors, leading to improved customer satisfaction and loyalty.\\n\\n6. + **Improved Accuracy** \\n - AI technologies, such as machine learning algorithms, + reduce the likelihood of human error in various processes, leading to greater + accuracy and reliability.\\n\\n7. **Enhanced Innovation** \\n - AI fosters + innovative solutions by providing new tools and approaches to problem-solving, + enabling companies to develop cutting-edge products and services.\\n\\n8. **Scalability** + \ \\n - AI can be scaled to handle varying amounts of workloads without significant + changes to infrastructure, making it easier for organizations to expand operations.\\n\\n9. + **Predictive Capabilities** \\n - Advanced analytics powered by AI can anticipate + trends and outcomes, allowing businesses to proactively adjust strategies and + improve forecasting.\\n\\n10. **Health Benefits** \\n - In healthcare, AI + assists in diagnostics, personalized treatment plans, and predictive analytics, + leading to better patient care and improved health outcomes.\\n\\n11. **Safety + and Risk Mitigation** \\n - AI can enhance safety in various industries + by taking over dangerous tasks, monitoring for hazards, and predicting maintenance + needs for critical machinery, thereby preventing accidents.\\n\\n12. **Reduced + Environmental Impact** \\n - AI can optimize resource usage in areas such + as energy consumption and supply chain logistics, contributing to sustainability + efforts and reducing overall environmental footprints.\",\n \"refusal\": + null\n },\n \"logprobs\": null,\n \"finish_reason\": \"stop\"\n + \ }\n ],\n \"usage\": {\n \"prompt_tokens\": 168,\n \"completion_tokens\": + 440,\n \"total_tokens\": 608,\n \"prompt_tokens_details\": {\n \"cached_tokens\": + 0,\n \"audio_tokens\": 0\n },\n \"completion_tokens_details\": {\n + \ \"reasoning_tokens\": 0,\n \"audio_tokens\": 0,\n \"accepted_prediction_tokens\": + 0,\n \"rejected_prediction_tokens\": 0\n }\n },\n \"system_fingerprint\": + \"fp_0aa8d3e20b\"\n}\n" + headers: + CF-Cache-Status: + - DYNAMIC + CF-RAY: + - 8f9721053d1eb9f1-SEA + Connection: + - keep-alive + Content-Encoding: + - gzip + Content-Type: + - application/json + Date: + - Sun, 29 Dec 2024 04:43:32 GMT + Server: + - cloudflare + Set-Cookie: + - __cf_bm=5enubNIoQSGMYEgy8Q2FpzzhphA0y.0lXukRZrWFvMk-1735447412-1.0.1.1-FIK1sMkUl3YnW1gTC6ftDtb2mKsbosb4mwabdFAlWCfJ6pXeavYq.bPsfKNvzAb5WYq60yVGH5lHsJT05bhSgw; + path=/; expires=Sun, 29-Dec-24 05:13:32 GMT; domain=.api.openai.com; HttpOnly; + Secure; SameSite=None + - _cfuvid=63wmKMTuFamkLN8FBI4fP8JZWbjWiRxWm7wb3kz.z_A-1735447412038-0.0.1.1-604800000; + path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None + Transfer-Encoding: + - chunked + X-Content-Type-Options: + - nosniff + access-control-expose-headers: + - X-Request-ID + alt-svc: + - h3=":443"; ma=86400 + openai-organization: + - crewai-iuxna1 + openai-processing-ms: + - '7577' + openai-version: + - '2020-10-01' + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + x-ratelimit-limit-requests: + - '30000' + x-ratelimit-limit-tokens: + - '150000000' + x-ratelimit-remaining-requests: + - '29999' + x-ratelimit-remaining-tokens: + - '149999793' + x-ratelimit-reset-requests: + - 2ms + x-ratelimit-reset-tokens: + - 0s + x-request-id: + - req_55b8d714656e8f10f4e23cbe9034d66b + http_version: HTTP/1.1 + status_code: 200 +version: 1 diff --git a/tests/crew_test.py b/tests/crew_test.py index 2003ddada..74bcf08d3 100644 --- a/tests/crew_test.py +++ b/tests/crew_test.py @@ -1941,6 +1941,90 @@ def test_crew_log_file_output(tmp_path): assert test_file.exists() +@pytest.mark.vcr(filter_headers=["authorization"]) +def test_crew_output_file_end_to_end(tmp_path): + """Test output file functionality in a full crew context.""" + # Create an agent + agent = Agent( + role="Researcher", + goal="Analyze AI topics", + backstory="You have extensive AI research experience.", + allow_delegation=False, + ) + + # Create a task with dynamic output file path + dynamic_path = tmp_path / "output_{topic}.txt" + task = Task( + description="Explain the advantages of {topic}.", + expected_output="A summary of the main advantages, bullet points recommended.", + agent=agent, + output_file=str(dynamic_path), + ) + + # Create and run the crew + crew = Crew( + agents=[agent], + tasks=[task], + process=Process.sequential, + ) + crew.kickoff(inputs={"topic": "AI"}) + + # Verify file creation and cleanup + expected_file = tmp_path / "output_AI.txt" + assert expected_file.exists(), f"Output file {expected_file} was not created" + + +@pytest.mark.vcr(filter_headers=["authorization"]) +def test_crew_output_file_validation_failures(): + """Test output file validation failures in a crew context.""" + agent = Agent( + role="Researcher", + goal="Analyze data", + backstory="You analyze data files.", + allow_delegation=False, + ) + + # Test path traversal + with pytest.raises(ValueError, match="Path traversal"): + task = Task( + description="Analyze data", + expected_output="Analysis results", + agent=agent, + output_file="../output.txt" + ) + Crew(agents=[agent], tasks=[task]).kickoff() + + # Test shell special characters + with pytest.raises(ValueError, match="Shell special characters"): + task = Task( + description="Analyze data", + expected_output="Analysis results", + agent=agent, + output_file="output.txt | rm -rf /" + ) + Crew(agents=[agent], tasks=[task]).kickoff() + + # Test shell expansion + with pytest.raises(ValueError, match="Shell expansion"): + task = Task( + description="Analyze data", + expected_output="Analysis results", + agent=agent, + output_file="~/output.txt" + ) + Crew(agents=[agent], tasks=[task]).kickoff() + + # Test invalid template variable + with pytest.raises(ValueError, match="Invalid template variable"): + task = Task( + description="Analyze data", + expected_output="Analysis results", + agent=agent, + output_file="{invalid-name}/output.txt" + ) + Crew(agents=[agent], tasks=[task]).kickoff() + + @pytest.mark.vcr(filter_headers=["authorization"]) def test_manager_agent(): from unittest.mock import patch @@ -3125,4 +3209,4 @@ def test_multimodal_agent_live_image_analysis(): # Verify we got a meaningful response assert isinstance(result.raw, str) assert len(result.raw) > 100 # Expecting a detailed analysis - assert "error" not in result.raw.lower() # No error messages in response \ No newline at end of file + assert "error" not in result.raw.lower() # No error messages in response diff --git a/tests/task_test.py b/tests/task_test.py index 40eb98e54..dc15c251f 100644 --- a/tests/task_test.py +++ b/tests/task_test.py @@ -719,21 +719,24 @@ def test_interpolate_inputs(): task = Task( description="Give me a list of 5 interesting ideas about {topic} to explore for an article, what makes them unique and interesting.", expected_output="Bullet point list of 5 interesting ideas about {topic}.", + output_file="/tmp/{topic}/output_{date}.txt" ) - task.interpolate_inputs(inputs={"topic": "AI"}) + task.interpolate_inputs(inputs={"topic": "AI", "date": "2024"}) assert ( task.description == "Give me a list of 5 interesting ideas about AI to explore for an article, what makes them unique and interesting." ) assert task.expected_output == "Bullet point list of 5 interesting ideas about AI." + assert task.output_file == "/tmp/AI/output_2024.txt" - task.interpolate_inputs(inputs={"topic": "ML"}) + task.interpolate_inputs(inputs={"topic": "ML", "date": "2025"}) assert ( task.description == "Give me a list of 5 interesting ideas about ML to explore for an article, what makes them unique and interesting." ) assert task.expected_output == "Bullet point list of 5 interesting ideas about ML." + assert task.output_file == "/tmp/ML/output_2025.txt" def test_interpolate_only(): @@ -872,3 +875,61 @@ def test_key(): assert ( task.key == hash ), "The key should be the hash of the non-interpolated description." + + +def test_output_file_validation(): + """Test output file path validation.""" + # Valid paths + assert Task( + description="Test task", + expected_output="Test output", + output_file="output.txt" + ).output_file == "output.txt" + assert Task( + description="Test task", + expected_output="Test output", + output_file="/tmp/output.txt" + ).output_file == "tmp/output.txt" + assert Task( + description="Test task", + expected_output="Test output", + output_file="{dir}/output_{date}.txt" + ).output_file == "{dir}/output_{date}.txt" + + # Invalid paths + with pytest.raises(ValueError, match="Path traversal"): + Task( + description="Test task", + expected_output="Test output", + output_file="../output.txt" + ) + with pytest.raises(ValueError, match="Path traversal"): + Task( + description="Test task", + expected_output="Test output", + output_file="folder/../output.txt" + ) + with pytest.raises(ValueError, match="Shell special characters"): + Task( + description="Test task", + expected_output="Test output", + output_file="output.txt | rm -rf /" + ) + with pytest.raises(ValueError, match="Shell expansion"): + Task( + description="Test task", + expected_output="Test output", + output_file="~/output.txt" + ) + with pytest.raises(ValueError, match="Shell expansion"): + Task( + description="Test task", + expected_output="Test output", + output_file="$HOME/output.txt" + ) + with pytest.raises(ValueError, match="Invalid template variable"): + Task( + description="Test task", + expected_output="Test output", + output_file="{invalid-name}/output.txt" + )