You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched to see if a similar issue already exists.
Is your feature request related to a problem? Please describe.
I'm facing difficulties managing user sessions for non-deepcopy-able objects in Gradio because the gr.Request object is not available in the authenticate function. This limitation forces me to use the username as a key for session management, which raises security concerns about potential user impersonation.
Describe the solution you'd like
I would like the gr.Request object to be included in the authenticate function. This would allow me to securely manage user-specific instances without relying solely on the username. Additionally, I would appreciate clarification on whether using the username as a key is safe, assuming the username is unique.
Additional context
I am implementing a session management system similar to the session_hash dictionary described in the Gradio documentation (link). My current workaround involves using the username to store user-specific instances, but I need to ensure that an authenticated user cannot change the request.username to impersonate another user.
Here is a code example illustrating the issue:
importgradioasgrclassNonDeepCopyable:
def__init__(self):
fromthreadingimportLockself.counter=0self.lock=Lock() # Lock objects cannot be deepcopieddefincrement(self):
withself.lock:
self.counter+=1returnself.counter# Global dictionary to store user-specific instancesinstances= {}
defauthenticate(username, password):
print(f"Authenticating {username} with {password}")
ifusername==password: # Testing... authentication would use firebase in this case.print("Authentication successful")
instances[username] =NonDeepCopyable()
returnTrueelse:
returnFalsedefinitialize_instance(request: gr.Request):
ifrequest.usernameininstances: # Is this safe, or can someone potentially change the request.username header?# instances[request.username] = NonDeepCopyable()return"Session initialized!"else:
return"Error: Authentication failed"defcleanup_instance(request: gr.Request):
ifrequest.usernameininstances:
delinstances[request.username]
defincrement_counter(request: gr.Request):
ifrequest.usernameininstances:
instance=instances[request.username]
returninstance.username, instance.increment()
return"Error: Session not initialized"withgr.Blocks() asdemo:
output=gr.Textbox(label="Status")
username=gr.Textbox(label="Username")
counter=gr.Number(label="Counter Value")
increment_btn=gr.Button("Increment Counter")
increment_btn.click(increment_counter, inputs=None, outputs=[username, counter])
# Initialize instance when page loadsdemo.load(initialize_instance, inputs=None, outputs=output)
# Clean up instance when page is closed/refresheddemo.close(cleanup_instance)
demo.launch(auth=authenticate)
The text was updated successfully, but these errors were encountered:
Actually, having gr.Request included in the authenticate function wouldn't solve my problem, because the session_hash changes on every reload but the authentication does not. So I guess the only fix would be to make sure that the username is secure, or the ability to add a new var in the request (token) that we can use to identify users.
Is your feature request related to a problem? Please describe.
I'm facing difficulties managing user sessions for non-deepcopy-able objects in Gradio because the
gr.Request
object is not available in theauthenticate
function. This limitation forces me to use the username as a key for session management, which raises security concerns about potential user impersonation.Describe the solution you'd like
I would like the
gr.Request
object to be included in theauthenticate
function. This would allow me to securely manage user-specific instances without relying solely on the username. Additionally, I would appreciate clarification on whether using the username as a key is safe, assuming the username is unique.Additional context
I am implementing a session management system similar to the
session_hash
dictionary described in the Gradio documentation (link). My current workaround involves using the username to store user-specific instances, but I need to ensure that an authenticated user cannot change therequest.username
to impersonate another user.Here is a code example illustrating the issue:
The text was updated successfully, but these errors were encountered: