korbit-ai[bot] commented on code in PR #34803: URL: https://github.com/apache/superset/pull/34803#discussion_r2292116045
########## superset/utils/jinja_template_validator.py: ########## @@ -0,0 +1,164 @@ +""" +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +""" + +import re +from typing import Any, Dict, Optional, Tuple + +from jinja2 import TemplateSyntaxError +from jinja2.sandbox import SandboxedEnvironment +from marshmallow import ValidationError + +from superset.utils import json + + +def validate_jinja_template(template_str: str) -> Tuple[bool, Optional[str]]: + """ + Validates Jinja2 template syntax. + + Returns: + Tuple of (is_valid, error_message) + If valid: (True, None) + If invalid: (False, "Error description") + """ + if not template_str: + return True, None + + try: + env = SandboxedEnvironment() + env.from_string(template_str) + + return True, None + + except TemplateSyntaxError as e: + error_msg = str(e) + + if "expected token 'end of print statement'" in error_msg: + if "such" in template_str.lower(): + return False, ( Review Comment: ### Complex Error Handling Logic <sub></sub> <details> <summary>Tell me more</summary> ###### What is the issue? The error handling logic in validate_jinja_template contains complex nested conditionals that handle different error cases, making the function harder to maintain and extend. ###### Why this matters Complex nested error handling reduces code maintainability and makes it difficult to add new error cases or modify existing ones. It also violates the Single Responsibility Principle. ###### Suggested change ∙ *Feature Preview* Extract error handling into a separate function using a dictionary-based mapping or strategy pattern: ```python def _get_template_error_message(error: TemplateSyntaxError, template_str: str) -> str: error_msg = str(error) error_handlers = { "expected token 'end of print statement'": _handle_print_statement_error, "unexpected end of template": _handle_unclosed_block_error, "expected token": _handle_token_error } for error_type, handler in error_handlers.items(): if error_type in error_msg: return handler(error_msg, template_str) return f"Template syntax error: {error_msg}" ``` ###### Provide feedback to improve future suggestions [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/be19d9a3-e80c-445b-87d9-7adf2f996b2f/upvote) [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/be19d9a3-e80c-445b-87d9-7adf2f996b2f?what_not_true=true) [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/be19d9a3-e80c-445b-87d9-7adf2f996b2f?what_out_of_scope=true) [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/be19d9a3-e80c-445b-87d9-7adf2f996b2f?what_not_in_standard=true) [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/be19d9a3-e80c-445b-87d9-7adf2f996b2f) </details> <sub> 💬 Looking for more details? Reply to this comment to chat with Korbit. </sub> <!--- korbi internal id:72673795-c542-41b7-a3ac-489f4c41b3b4 --> [](72673795-c542-41b7-a3ac-489f4c41b3b4) ########## superset/utils/jinja_template_validator.py: ########## @@ -0,0 +1,164 @@ +""" +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +""" + +import re +from typing import Any, Dict, Optional, Tuple + +from jinja2 import TemplateSyntaxError +from jinja2.sandbox import SandboxedEnvironment +from marshmallow import ValidationError + +from superset.utils import json + + +def validate_jinja_template(template_str: str) -> Tuple[bool, Optional[str]]: + """ + Validates Jinja2 template syntax. + + Returns: + Tuple of (is_valid, error_message) + If valid: (True, None) + If invalid: (False, "Error description") + """ + if not template_str: + return True, None + + try: + env = SandboxedEnvironment() + env.from_string(template_str) Review Comment: ### Unrestricted Jinja2 Sandbox Configuration <sub></sub> <details> <summary>Tell me more</summary> ###### What is the issue? The SandboxedEnvironment is used without defining allowed filters, globals or attributes, which could potentially allow unsafe operations. ###### Why this matters A default SandboxedEnvironment with no restrictions could still permit certain dangerous operations or attribute access that might lead to security issues in template execution. ###### Suggested change ∙ *Feature Preview* ```python env = SandboxedEnvironment( finalize=None, autoescape=True, # Define explicit whitelist of allowed filters filters={}, # Define explicit whitelist of allowed globals globals={} ) ``` ###### Provide feedback to improve future suggestions [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/ec7afbd8-6d97-4919-a4f6-d1ac7589754e/upvote) [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/ec7afbd8-6d97-4919-a4f6-d1ac7589754e?what_not_true=true) [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/ec7afbd8-6d97-4919-a4f6-d1ac7589754e?what_out_of_scope=true) [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/ec7afbd8-6d97-4919-a4f6-d1ac7589754e?what_not_in_standard=true) [](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/ec7afbd8-6d97-4919-a4f6-d1ac7589754e) </details> <sub> 💬 Looking for more details? Reply to this comment to chat with Korbit. </sub> <!--- korbi internal id:d347c364-4784-4bf3-878e-b2e024f266c9 --> [](d347c364-4784-4bf3-878e-b2e024f266c9) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
