Skip to content

Don’t use eval in python API #4

Description

@flying-sheep

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions