Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed mutable object used as kwarg for Server ctor #376

Merged
merged 1 commit into from
Mar 12, 2020
Merged

Fixed mutable object used as kwarg for Server ctor #376

merged 1 commit into from
Mar 12, 2020

Conversation

brendongo
Copy link
Contributor

Hello,
I am a security engineer at r2c.dev. We are working to write code
checks for security in open source code.

In python, the default values of function parameters are instantiated
at function definition time. All calls to that function that use the
default value all point to the same global object. e.g.:

def func(x=[]):
   x.append(1)
   print(x)

func() # [1]
func() # [1 , 1]

Because of this, two instances of Server (initialized without passing
in a protocol_config option) actually share the same protocol_config.
So modifying one server's config affects the other ones.

Fix:
The recommended solution is to either set default to None and assign
a new empty object when the variable is None.

We have a tool called Bento you can use for your project that continuously detects problems like this one. The check that identified this issue will be available in the very near future.
Thanks, and I hope this helps! Let me know if you have any questions.

In python, the default values of function parameters are instantiated
at function definition time. All calls to that function that use the
default value all point to the same global object. e.g.:

```
def func(x=[]):
   x.append(1)
   print(x)

func() # [1]
func() # [1 , 1]
```

Because of this, two instances of Server (initialized without passing
in a protocol_config option) actually share the same protocol_config.
So modifying one server's config affects the other ones.

Fix:
The recommended solution is to either set default to None and assign
a new empty object when the variable is None.
@comrumino
Copy link
Collaborator

comrumino commented Mar 12, 2020

Sorry for the delay. First day back from vacation :) I'm taking a look now

@comrumino
Copy link
Collaborator

Good find. Glad the impact is minor---the issue would only have impact in esoteric/smelly code. Confirmed the issue with the below code.

        server = ThreadedServer(SlaveService, port=18878, auto_register=False)
        server2 = ThreadedServer(SlaveService, port=18879, auto_register=False)
        server._start_in_thread()
        server2._start_in_thread()
        server.protocol_config['mutable'] = 'kwarg'

@comrumino comrumino changed the title Fix: default list shared across all Server objects Fixed mutable object used as kwarg for Server ctor Mar 12, 2020
@comrumino comrumino merged commit 7c3ca63 into tomerfiliba-org:master Mar 12, 2020
@brendongo brendongo deleted the mutable-shared-list branch March 26, 2020 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants