Skip to content

Commit

Permalink
refactor(framework) Add allowance for system time drift in the node a…
Browse files Browse the repository at this point in the history
…uthentication (#4899)
  • Loading branch information
panh99 authored Feb 4, 2025
1 parent a78da35 commit 42e2e7c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/py/flwr/common/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
PUBLIC_KEY_HEADER = "public-key-bin" # Must end with "-bin" for binary data
SIGNATURE_HEADER = "signature-bin" # Must end with "-bin" for binary data
TIMESTAMP_HEADER = "timestamp"
TIMESTAMP_TOLERANCE = 10 # Tolerance for timestamp verification
TIMESTAMP_TOLERANCE = 10 # General tolerance for timestamp verification
SYSTEM_TIME_TOLERANCE = 5 # Allowance for system time drift


class MessageType:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from flwr.common.constant import (
PUBLIC_KEY_HEADER,
SIGNATURE_HEADER,
SYSTEM_TIME_TOLERANCE,
TIMESTAMP_HEADER,
TIMESTAMP_TOLERANCE,
)
Expand All @@ -38,6 +39,9 @@
)
from flwr.server.superlink.linkstate import LinkStateFactory

MIN_TIMESTAMP_DIFF = -SYSTEM_TIME_TOLERANCE
MAX_TIMESTAMP_DIFF = TIMESTAMP_TOLERANCE + SYSTEM_TIME_TOLERANCE


def _unary_unary_rpc_terminator(message: str) -> grpc.RpcMethodHandler:
def terminate(_request: GrpcMessage, context: grpc.ServicerContext) -> GrpcMessage:
Expand Down Expand Up @@ -100,7 +104,7 @@ def intercept_service(
current = now()
time_diff = current - datetime.datetime.fromisoformat(timestamp_iso)
# Abort the RPC call if the timestamp is too old or in the future
if not 0 < time_diff.total_seconds() < TIMESTAMP_TOLERANCE:
if not MIN_TIMESTAMP_DIFF < time_diff.total_seconds() < MAX_TIMESTAMP_DIFF:
return _unary_unary_rpc_terminator("Invalid timestamp")

# Continue the RPC call
Expand Down

0 comments on commit 42e2e7c

Please sign in to comment.