From 7d61e3dd16aeded5ed8313503a88f721a6497d96 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 15 Jun 2023 18:16:23 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=86=95=20feat(utils.py):=20add=20function?= =?UTF-8?q?=20to=20remove=20API=20keys=20from=20flow=20data=20This=20commi?= =?UTF-8?q?t=20adds=20a=20new=20function=20called=20`remove=5Fapi=5Fkeys`?= =?UTF-8?q?=20to=20the=20`utils.py`=20file.=20The=20function=20takes=20in?= =?UTF-8?q?=20a=20dictionary=20representing=20a=20flow=20and=20removes=20a?= =?UTF-8?q?ny=20API=20keys=20from=20the=20flow=20data.=20The=20function=20?= =?UTF-8?q?iterates=20through=20each=20node=20in=20the=20flow=20and=20chec?= =?UTF-8?q?ks=20if=20the=20node=20contains=20any=20API=20keys.=20If=20an?= =?UTF-8?q?=20API=20key=20is=20found,=20the=20function=20sets=20the=20valu?= =?UTF-8?q?e=20of=20the=20key=20to=20`None`.=20This=20function=20is=20usef?= =?UTF-8?q?ul=20for=20removing=20sensitive=20information=20from=20flow=20d?= =?UTF-8?q?ata=20before=20it=20is=20stored=20or=20transmitted.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/utils.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/backend/langflow/api/utils.py diff --git a/src/backend/langflow/api/utils.py b/src/backend/langflow/api/utils.py new file mode 100644 index 000000000..bfd9e3da5 --- /dev/null +++ b/src/backend/langflow/api/utils.py @@ -0,0 +1,24 @@ +API_WORDS = ["api", "key", "token"] + + +def has_api_terms(word: str): + return "api" in word and ( + "key" in word or ("token" in word and "tokens" not in word) + ) + + +def remove_api_keys(flow: dict): + """Remove api keys from flow data.""" + if flow.get("data") and flow["data"].get("nodes"): + for node in flow["data"]["nodes"]: + node_data = node.get("data").get("node") + template = node_data.get("template") + for value in template.values(): + if ( + isinstance(value, dict) + and has_api_terms(value["name"]) + and value.get("password") + ): + value["value"] = None + + return flow