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

Don’t use eval in python API #4

Open
flying-sheep opened this issue Nov 30, 2020 · 0 comments
Open

Don’t use eval in python API #4

flying-sheep opened this issue Nov 30, 2020 · 0 comments

Comments

@flying-sheep
Copy link

flying-sheep commented Nov 30, 2020

It’s fine to allow people to specify a Python expression per CLI, but a Python API should work with Python types.

The API should be changed so

  1. read_pcap and read_har take kwargs instead of a dict of params
  2. filters are specified by passing calllbacks

E.g.:

reader.read_pcap('file.pcap', filter=lambda tcp: tcp.dst == '1.1.1.1')

This can easily be done:

from inspect import signature

...

def filter_packet(self, filter, eth, ip=None, tcp=None):
    if not filter: return True
    if isinstance(filter, str): return eval(filter)
    if not callable(filter: raise TypeError('filter needs to be callable')
    sig = signature(filter)
    params = {
        k: v
        for k: v in dict(eth=eth, ip=ip, tcp=tcp).items()
        if v is not None and k in sig.parameters
    }
    filter(**params)
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

No branches or pull requests

1 participant