From 2e2076a42cee88fe417e4350b62352d090824c35 Mon Sep 17 00:00:00 2001 From: teo Date: Mon, 8 Jan 2024 14:42:56 +0200 Subject: [PATCH 1/8] allowed kwargs only in locals --- packages/syft/src/syft/service/code/user_code.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/syft/src/syft/service/code/user_code.py b/packages/syft/src/syft/service/code/user_code.py index 04a5514cdb0..5d4e3ca6638 100644 --- a/packages/syft/src/syft/service/code/user_code.py +++ b/packages/syft/src/syft/service/code/user_code.py @@ -1147,8 +1147,12 @@ def to_str(arg: Any) -> str: # statisfy lint checker result = None - _locals = locals() + # We only need access to local kwargs + _locals = {"kwargs": kwargs} _globals = {} + + original_print("Test") + for service_func_name, (linked_obj, _) in code_item.nested_codes.items(): code_obj = linked_obj.resolve_with_context(context=context) @@ -1156,9 +1160,10 @@ def to_str(arg: Any) -> str: raise Exception(code_obj.err()) _globals[service_func_name] = code_obj.ok() _globals["print"] = print - exec(code_item.parsed_code, _globals, locals()) # nosec + exec(code_item.parsed_code, _globals, _locals) # nosec evil_string = f"{code_item.unique_func_name}(**kwargs)" + original_print(kwargs) try: result = eval(evil_string, _globals, _locals) # nosec except Exception as e: @@ -1170,6 +1175,7 @@ def to_str(arg: Any) -> str: ) log_service = context.node.get_service("LogService") log_service.append(context=context, uid=log_id, new_err=error_msg) + original_print(e) result = Err( f"Exception encountered while running {code_item.service_func_name}" From 0b1cf324d9cb3024bebac822ebb7cd0a90f9bb87 Mon Sep 17 00:00:00 2001 From: teo Date: Mon, 8 Jan 2024 14:43:30 +0200 Subject: [PATCH 2/8] removed prints --- packages/syft/src/syft/service/code/user_code.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/syft/src/syft/service/code/user_code.py b/packages/syft/src/syft/service/code/user_code.py index 5d4e3ca6638..6de4faf2671 100644 --- a/packages/syft/src/syft/service/code/user_code.py +++ b/packages/syft/src/syft/service/code/user_code.py @@ -1151,8 +1151,6 @@ def to_str(arg: Any) -> str: _locals = {"kwargs": kwargs} _globals = {} - original_print("Test") - for service_func_name, (linked_obj, _) in code_item.nested_codes.items(): code_obj = linked_obj.resolve_with_context(context=context) @@ -1163,7 +1161,6 @@ def to_str(arg: Any) -> str: exec(code_item.parsed_code, _globals, _locals) # nosec evil_string = f"{code_item.unique_func_name}(**kwargs)" - original_print(kwargs) try: result = eval(evil_string, _globals, _locals) # nosec except Exception as e: @@ -1175,8 +1172,6 @@ def to_str(arg: Any) -> str: ) log_service = context.node.get_service("LogService") log_service.append(context=context, uid=log_id, new_err=error_msg) - original_print(e) - result = Err( f"Exception encountered while running {code_item.service_func_name}" ", please contact the Node Admin for more info." From 5251bf1a74ac046163b2708f011330a404dda228 Mon Sep 17 00:00:00 2001 From: teo Date: Mon, 8 Jan 2024 14:44:06 +0200 Subject: [PATCH 3/8] disabled set_api_registry --- .../syft/src/syft/service/code/user_code.py | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/syft/src/syft/service/code/user_code.py b/packages/syft/src/syft/service/code/user_code.py index 6de4faf2671..09df51210d4 100644 --- a/packages/syft/src/syft/service/code/user_code.py +++ b/packages/syft/src/syft/service/code/user_code.py @@ -1011,24 +1011,24 @@ def job_increase_current_iter(current_iter): job.current_iter += current_iter job_service.update(context, job) - def set_api_registry(): - user_signing_key = [ - x.signing_key - for x in user_service.stash.partition.data.values() - if x.verify_key == context.credentials - ][0] - data_protcol = get_data_protocol() - user_api = node.get_api(context.credentials, data_protcol.latest_version) - user_api.signing_key = user_signing_key - # We hardcode a python connection here since we have access to the node - # TODO: this is not secure - user_api.connection = PythonConnection(node=node) - - APIRegistry.set_api_for( - node_uid=node.id, - user_verify_key=context.credentials, - api=user_api, - ) + # def set_api_registry(): + # user_signing_key = [ + # x.signing_key + # for x in user_service.stash.partition.data.values() + # if x.verify_key == context.credentials + # ][0] + # data_protcol = get_data_protocol() + # user_api = node.get_api(context.credentials, data_protcol.latest_version) + # user_api.signing_key = user_signing_key + # # We hardcode a python connection here since we have access to the node + # # TODO: this is not secure + # user_api.connection = PythonConnection(node=node) + + # APIRegistry.set_api_for( + # node_uid=node.id, + # user_verify_key=context.credentials, + # api=user_api, + # ) def launch_job(func: UserCode, **kwargs): # relative @@ -1049,8 +1049,8 @@ def launch_job(func: UserCode, **kwargs): parent_job_id=context.job_id, has_execute_permissions=True, ) - # set api in global scope to enable using .get(), .wait()) - set_api_registry() + # # set api in global scope to enable using .get(), .wait()) + # set_api_registry() return job except Exception as e: From 20c6dcbaf8fea17820b2153e03b0435d3483aed8 Mon Sep 17 00:00:00 2001 From: teo Date: Mon, 8 Jan 2024 15:10:15 +0200 Subject: [PATCH 4/8] fix lint --- packages/syft/src/syft/service/code/user_code.py | 6 +----- packages/syft/src/syft/store/sqlite_document_store.py | 8 ++++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/syft/src/syft/service/code/user_code.py b/packages/syft/src/syft/service/code/user_code.py index 09df51210d4..09db03119bb 100644 --- a/packages/syft/src/syft/service/code/user_code.py +++ b/packages/syft/src/syft/service/code/user_code.py @@ -29,12 +29,9 @@ # relative from ...abstract_node import NodeType -from ...client.api import APIRegistry from ...client.api import NodeIdentity -from ...client.client import PythonConnection from ...client.enclave_client import EnclaveMetadata from ...node.credentials import SyftVerifyKey -from ...protocol.data_protocol import get_data_protocol from ...serde.deserialize import _deserialize from ...serde.serializable import serializable from ...serde.serialize import _serialize @@ -994,7 +991,7 @@ def __init__(self, context): node = context.node job_service = node.get_service("jobservice") action_service = node.get_service("actionservice") - user_service = node.get_service("userservice") + # user_service = node.get_service("userservice") def job_set_n_iters(n_iters): job = context.job @@ -1150,7 +1147,6 @@ def to_str(arg: Any) -> str: # We only need access to local kwargs _locals = {"kwargs": kwargs} _globals = {} - for service_func_name, (linked_obj, _) in code_item.nested_codes.items(): code_obj = linked_obj.resolve_with_context(context=context) diff --git a/packages/syft/src/syft/store/sqlite_document_store.py b/packages/syft/src/syft/store/sqlite_document_store.py index e128a63bc91..8067552a450 100644 --- a/packages/syft/src/syft/store/sqlite_document_store.py +++ b/packages/syft/src/syft/store/sqlite_document_store.py @@ -206,8 +206,8 @@ def _set(self, key: UID, value: Any) -> None: self._update(key, value) else: insert_sql = ( - f"insert into {self.table_name} (uid, repr, value) VALUES (?, ?, ?)" # nosec - ) # nosec + f"insert into {self.table_name} (uid, repr, value) VALUES (?, ?, ?)" + ) # nosec # nosec data = _serialize(value, to_bytes=True) res = self._execute(insert_sql, [str(key), _repr_debug_(value), data]) if res.is_err(): @@ -215,8 +215,8 @@ def _set(self, key: UID, value: Any) -> None: def _update(self, key: UID, value: Any) -> None: insert_sql = ( - f"update {self.table_name} set uid = ?, repr = ?, value = ? where uid = ?" # nosec - ) # nosec + f"update {self.table_name} set uid = ?, repr = ?, value = ? where uid = ?" + ) # nosec # nosec data = _serialize(value, to_bytes=True) res = self._execute(insert_sql, [str(key), _repr_debug_(value), data, str(key)]) if res.is_err(): From 94e33bd035e06ed894ca3f79cdd383b125cae2c3 Mon Sep 17 00:00:00 2001 From: teo Date: Mon, 8 Jan 2024 15:18:42 +0200 Subject: [PATCH 5/8] removed double no sec --- packages/syft/src/syft/store/sqlite_document_store.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/syft/src/syft/store/sqlite_document_store.py b/packages/syft/src/syft/store/sqlite_document_store.py index 8067552a450..43cd5e2952b 100644 --- a/packages/syft/src/syft/store/sqlite_document_store.py +++ b/packages/syft/src/syft/store/sqlite_document_store.py @@ -207,7 +207,7 @@ def _set(self, key: UID, value: Any) -> None: else: insert_sql = ( f"insert into {self.table_name} (uid, repr, value) VALUES (?, ?, ?)" - ) # nosec # nosec + ) # nosec data = _serialize(value, to_bytes=True) res = self._execute(insert_sql, [str(key), _repr_debug_(value), data]) if res.is_err(): @@ -216,7 +216,7 @@ def _set(self, key: UID, value: Any) -> None: def _update(self, key: UID, value: Any) -> None: insert_sql = ( f"update {self.table_name} set uid = ?, repr = ?, value = ? where uid = ?" - ) # nosec # nosec + ) # nosec data = _serialize(value, to_bytes=True) res = self._execute(insert_sql, [str(key), _repr_debug_(value), data, str(key)]) if res.is_err(): From 58281b0c278b8ca19ebf9ffeda611ac84d59e639 Mon Sep 17 00:00:00 2001 From: teo Date: Mon, 8 Jan 2024 15:25:56 +0200 Subject: [PATCH 6/8] made insert_sql result on one line --- packages/syft/src/syft/store/sqlite_document_store.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/syft/src/syft/store/sqlite_document_store.py b/packages/syft/src/syft/store/sqlite_document_store.py index 43cd5e2952b..73ab3492f8a 100644 --- a/packages/syft/src/syft/store/sqlite_document_store.py +++ b/packages/syft/src/syft/store/sqlite_document_store.py @@ -205,18 +205,14 @@ def _set(self, key: UID, value: Any) -> None: if self._exists(key): self._update(key, value) else: - insert_sql = ( - f"insert into {self.table_name} (uid, repr, value) VALUES (?, ?, ?)" - ) # nosec + insert_sql = (f"insert into {self.table_name} (uid, repr, value) VALUES (?, ?, ?)") # nosec data = _serialize(value, to_bytes=True) res = self._execute(insert_sql, [str(key), _repr_debug_(value), data]) if res.is_err(): raise ValueError(res.err()) def _update(self, key: UID, value: Any) -> None: - insert_sql = ( - f"update {self.table_name} set uid = ?, repr = ?, value = ? where uid = ?" - ) # nosec + insert_sql = (f"update {self.table_name} set uid = ?, repr = ?, value = ? where uid = ?") # nosec data = _serialize(value, to_bytes=True) res = self._execute(insert_sql, [str(key), _repr_debug_(value), data, str(key)]) if res.is_err(): From d64d3f5a02fb522411bb25e8077b9db0e3694f69 Mon Sep 17 00:00:00 2001 From: teo Date: Mon, 8 Jan 2024 15:42:36 +0200 Subject: [PATCH 7/8] change comment to fit linting --- packages/syft/src/syft/store/sqlite_document_store.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/syft/src/syft/store/sqlite_document_store.py b/packages/syft/src/syft/store/sqlite_document_store.py index 73ab3492f8a..14f93aca8e3 100644 --- a/packages/syft/src/syft/store/sqlite_document_store.py +++ b/packages/syft/src/syft/store/sqlite_document_store.py @@ -205,14 +205,18 @@ def _set(self, key: UID, value: Any) -> None: if self._exists(key): self._update(key, value) else: - insert_sql = (f"insert into {self.table_name} (uid, repr, value) VALUES (?, ?, ?)") # nosec + insert_sql = ( + f"insert into {self.table_name} (uid, repr, value) VALUES (?, ?, ?)" # nosec + ) data = _serialize(value, to_bytes=True) res = self._execute(insert_sql, [str(key), _repr_debug_(value), data]) if res.is_err(): raise ValueError(res.err()) def _update(self, key: UID, value: Any) -> None: - insert_sql = (f"update {self.table_name} set uid = ?, repr = ?, value = ? where uid = ?") # nosec + insert_sql = ( + f"update {self.table_name} set uid = ?, repr = ?, value = ? where uid = ?" # nosec + ) data = _serialize(value, to_bytes=True) res = self._execute(insert_sql, [str(key), _repr_debug_(value), data, str(key)]) if res.is_err(): From 879ee221a5c34c5834d854f3bb030389670b6ee5 Mon Sep 17 00:00:00 2001 From: teo Date: Mon, 8 Jan 2024 15:53:25 +0200 Subject: [PATCH 8/8] fix lint --- packages/syft/src/syft/store/sqlite_document_store.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/syft/src/syft/store/sqlite_document_store.py b/packages/syft/src/syft/store/sqlite_document_store.py index 14f93aca8e3..2ec313a1a8a 100644 --- a/packages/syft/src/syft/store/sqlite_document_store.py +++ b/packages/syft/src/syft/store/sqlite_document_store.py @@ -206,7 +206,7 @@ def _set(self, key: UID, value: Any) -> None: self._update(key, value) else: insert_sql = ( - f"insert into {self.table_name} (uid, repr, value) VALUES (?, ?, ?)" # nosec + f"insert into {self.table_name} (uid, repr, value) VALUES (?, ?, ?)" # nosec ) data = _serialize(value, to_bytes=True) res = self._execute(insert_sql, [str(key), _repr_debug_(value), data]) @@ -215,7 +215,7 @@ def _set(self, key: UID, value: Any) -> None: def _update(self, key: UID, value: Any) -> None: insert_sql = ( - f"update {self.table_name} set uid = ?, repr = ?, value = ? where uid = ?" # nosec + f"update {self.table_name} set uid = ?, repr = ?, value = ? where uid = ?" # nosec ) data = _serialize(value, to_bytes=True) res = self._execute(insert_sql, [str(key), _repr_debug_(value), data, str(key)])