Language: Python
Label: vulnerable
CWE: CWE-94 \u2014 Improper Control of Generation of Code ('Code Injection')
The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.

Code:
def register_custom_functions(script: str) -> None:
    """
    Registers custom functions by executing the given script in a restricted environment.

    The script is compiled and executed with RestrictedPython, which is designed to reduce
    the risk of executing untrusted code. It provides a set of safe builtins to prevent
    malicious or unintended behavior.

    Args:
        script (str): The Python script containing the custom functions to be registered.

    Raises:
        FidesopsException: If allow_custom_connector_functions is disabled.
        SyntaxError: If the script contains a syntax error or uses restricted language features.
        Exception: If an exception occurs during the execution of the script.
    """

    if CONFIG.security.allow_custom_connector_functions:
        restricted_code = compile_restricted(
            script, "<string>", "exec", policy=CustomRestrictingNodeTransformer
        )
        safe_builtins["__import__"] = custom_guarded_import
        safe_builtins["_getitem_"] = getitem
        safe_builtins["staticmethod"] = staticmethod

        # pylint: disable=exec-used
        exec(
            restricted_code,
            {
                "__metaclass__": type,
                "__name__": "restricted_module",
                "__builtins__": safe_builtins,
            },
        )
    else:
        raise FidesopsException(
            message="The import of connector templates with custom functions is disabled by the 'security.allow_custom_connector_functions' setting."
        )