Python node failing after upgrade - import of standard library module disallowed

Describe the problem/error/question

Below python code started failing after upgrade to 1.123.4 from 1.122 (I believe).

import re
import zlib
import base64
import json

output = []

# Get the binary data from the first input item and decode from base64
#pdf_b64 = items[0]["binary"]["data"]["data"]
pdf_b64 = items[0]["json"]["data"]
#print(pdf_b64)
pdf = base64.b64decode(pdf_b64)
#print(pdf)

stream = re.compile(rb'/EmbeddedFile.*?FlateDecode.*?stream(.*?)endstream', re.S)

for s in stream.findall(pdf):
    s = s.strip(b'\r\n')
    try:
        decompressed = zlib.decompress(s)
        decoded = decompressed.decode('utf8')
        if 'Invoice' in decoded:
          output.append({"json": {"isdoc": decoded}})
    except:
        pass

return output

What is the error message (if any)?

Security violations detected

Line 1: Import of standard library module ‘re’ is disallowed. Allowed stdlib modules: none Line 2: Import of standard library module ‘zlib’ is disallowed. Allowed stdlib modules: none Line 3: Import of standard library module ‘base64’ is disallowed. Allowed stdlib modules: none Line 4: Import of standard library module ‘json’ is disallowed. Allowed stdlib modules: none

Error: Security violations detected at throwExecutionError (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/n8n-nodes-base@file+packages+nodes-base_@[email protected]_asn1.js@5_8da18263ca0574b0db58d4fefd8173ce/node_modules/n8n-nodes-base/nodes/Code/throw-execution-error.ts:11:9) at PythonTaskRunnerSandbox.runUsingIncomingItems (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/n8n-nodes-base@file+packages+nodes-base_@[email protected]_asn1.js@5_8da18263ca0574b0db58d4fefd8173ce/node_modules/n8n-nodes-base/nodes/Code/PythonTaskRunnerSandbox.ts:56:30) at processTicksAndRejections (node:internal/process/task_queues:105:5) at ExecuteContext.execute (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/n8n-nodes-base@file+packages+nodes-base_@[email protected]_asn1.js@5_8da18263ca0574b0db58d4fefd8173ce/node_modules/n8n-nodes-base/nodes/Code/Code.node.ts:171:12) at WorkflowExecute.executeNode (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/n8n-core@file+packages+core_@[email protected]_@[email protected]_ec37920eb95917b28efaa783206b20f3/node_modules/n8n-core/src/execution-engine/workflow-execute.ts:1045:8) at WorkflowExecute.runNode (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/n8n-core@file+packages+core_@[email protected]_@[email protected]_ec37920eb95917b28efaa783206b20f3/node_modules/n8n-core/src/execution-engine/workflow-execute.ts:1226:11) at /usr/local/lib/node_modules/n8n/node_modules/.pnpm/n8n-core@file+packages+core_@[email protected]_@[email protected]_ec37920eb95917b28efaa783206b20f3/node_modules/n8n-core/src/execution-engine/workflow-execute.ts:1662:27 at /usr/local/lib/node_modules/n8n/node_modules/.pnpm/n8n-core@file+packages+core_@[email protected]_@[email protected]_ec37920eb95917b28efaa783206b20f3/node_modules/n8n-core/src/execution-engine/workflow-execute.ts:2274:11

Please share your workflow

Information on your n8n setup

  • n8n version: 1.123.4 (Cloud)

Related bug report on github: Python Native Node: Import of standard library modules fails on 1.123 (regression) · Issue #22958 · n8n-io/n8n · GitHub

Another one opened here: https://github.com/n8n-io/n8n/issues/23072 and it was closed with explanation that Python node currently doesn’t allow imports due to security reasons.

Well, I think at this point you’ll have to look for another solution, like self-host, or maybe use AWS Lambda or something similar..

You could also try replicating what you’re doing in the code node with community nodes (if available) or even the built-in nodes, the code looks short, but I can’t fully understand the whole logic behind it..

Same error on self host