Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
.. image:: https://travis-ci.org/canni/switch.svg?branch=v1.1.0 :target: https://travis-ci.org/canni/switch
.. image:: https://coveralls.io/repos/canni/switch/badge.png?branch=v1.1.0 :target: https://coveralls.io/r/canni/switch?branch=v1.1.0
.. code-block:: python
from switch import Switch
def simple_example(val):
values = []
with Switch(val) as case:
if case(1):
values.append('Found 1')
if case(2, 3):
values.append('Found 2 or 3')
return values
assert simple_example(1) == ['Found 1']
assert simple_example(2) == ['Found 2 or 3']
assert simple_example(3) == ['Found 2 or 3']
assert simple_example('anything else') == []
.. code-block:: python
from switch import Switch
def simple_example_with_default(val):
values = []
with Switch(val) as case:
if case(1):
values.append('Found 1')
if case(2, 3):
values.append('Found 2 or 3')
if case.default:
values.append('No love for 1, 2 or 3?')
return values
assert simple_example_with_default(1) == ['Found 1']
assert simple_example_with_default(2) == ['Found 2 or 3']
assert simple_example_with_default(3) == ['Found 2 or 3']
assert simple_example_with_default('anything else') == ['No love for 1, 2 or 3?']
.. code-block:: python
from switch import Switch
def fall_through_example(val):
values = []
with Switch(val) as case:
if case(1, fall_through=True):
values.append('Found 1')
if case(2, 3):
values.append('Found 2 or 3')
if case.default:
values.append('No love for 1, 2 or 3?')
return values
assert fall_through_example(1) == ['Found 1', 'Found 2 or 3']
assert fall_through_example(2) == ['Found 2 or 3']
assert fall_through_example(3) == ['Found 2 or 3']
assert fall_through_example('anything else') == ['No love for 1, 2 or 3?']
.. code-block:: python
from switch import Switch
def ouh_callable_too(val):
values = []
with Switch(val) as case:
if case(1):
values.append('Found 1')
if case.call(lambda v: v < 100):
values.append('Found <100')
if case.default:
values.append('No love for anything lower than 100?')
return values
assert ouh_callable_too(1) == ['Found 1']
assert ouh_callable_too(50) == ['Found <100']
assert ouh_callable_too('anything else') == ['No love for anything lower than 100?']
.. code-block:: python
from switch import Switch
def test_regexp(val):
values = []
with Switch(val) as case:
if case(1):
values.append('Found 1')
if case.match(r'10|ten'):
values.append('Found 10')
if case.default:
values.append('No love for 1 or 10?')
return values
assert test_regexp(1) == ['Found 1']
assert test_regexp(10) == ['Found 10']
assert test_regexp('ten') == ['Found 10']
assert test_regexp('anything else') == ['No love for 1 or 10??']
.. code-block:: python
from switch import CSwitch, Switch
def fall_through_by_default(val):
values = []
with Switch(val, fall_through=True) as case:
if case(1):
values.append('Found 1')
if case(2):
values.append('Found 2')
if case(3, fall_through=False):
values.append('Found 3')
if case(4):
values.append('Found 4')
if case.default:
values.append('No love for 1, 2, 3 or 4?')
return values
def cswitch_shortcut(val):
values = []
with CSwitch(val) as case:
if case(1):
values.append('Found 1')
if case(2):
values.append('Found 2')
if case(3, fall_through=False):
values.append('Found 3')
if case(4):
values.append('Found 4')
if case.default:
values.append('No love for 1, 2, 3 or 4?')
return values
assert fall_through_by_default(1) == ['Found 1', 'Found 2', 'Found 3']
assert fall_through_by_default(2) == ['Found 2', 'Found 3']
assert fall_through_by_default(3) == ['Found 3']
assert fall_through_by_default(4) == ['Found 4']
assert fall_through_by_default('anything else') == ['No love for 1, 2, 3 or 4?']
assert cswitch_shortcut(1) == fall_through_by_default(1)
assert cswitch_shortcut(2) == fall_through_by_default(2)
assert cswitch_shortcut(3) == fall_through_by_default(3)
assert cswitch_shortcut(4) == fall_through_by_default(4)
assert cswitch_shortcut('anything else') == fall_through_by_default('anything else')
.. code-block:: python
from switch import Switch
def case_after_default_is_baad(val):
values = []
with Switch(val) as case:
if case(1):
values.append('Found 1')
if case.default:
values.append('Found default')
if case('this is baad'):
values.append('Should not happen!')
return values
assert case_after_default_is_baad(1) == ['Found 1']
try:
case_after_default_is_baad('this is baad')
assert False
except SyntaxError:
assert True
FAQs
The missing switch statement
We found that switch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.