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

rest put firmware: unknown plugin fix #1148

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions src/test/integration/web_interface/rest/test_rest_firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
from test.integration.web_interface.rest.base import RestTestBase


UPLOAD_DATA = {
'binary': standard_b64encode(b'test_file_content').decode(),
'file_name': 'test_file.txt',
'device_name': 'test_device',
'device_part': 'full',
'device_class': 'test_class',
'version': '1',
'vendor': 'test_vendor',
'release_date': '1970-01-01',
'tags': '',
'requested_analysis_systems': ['dummy'],
}


@pytest.mark.usefixtures('database_interfaces')
class TestRestFirmware(RestTestBase):
def test_rest_firmware_existing(self, backend_db):
Expand Down Expand Up @@ -47,35 +61,33 @@ def test_rest_search_not_existing(self, backend_db):
rv = self.test_client.get(f'/rest/firmware?query={query}', follow_redirects=True)
assert b'"uids": []' in rv.data

def test_rest_upload_valid(self):
data = {
'binary': standard_b64encode(b'test_file_content').decode(),
'file_name': 'test_file.txt',
'device_name': 'test_device',
'device_part': 'full',
'device_class': 'test_class',
'version': '1',
'vendor': 'test_vendor',
'release_date': '1970-01-01',
'tags': '',
'requested_analysis_systems': ['dummy'],
}
rv = self.test_client.put('/rest/firmware', json=data, follow_redirects=True)
def test_rest_upload_valid(self, monkeypatch):
monkeypatch.setattr(
'intercom.front_end_binding.InterComFrontEndBinding.get_available_analysis_plugins',
lambda _: ['dummy'],
)
rv = self.test_client.put('/rest/firmware', json=UPLOAD_DATA, follow_redirects=True)
assert b'c1f95369a99b765e93c335067e77a7d91af3076d2d3d64aacd04e1e0a810b3ed_17' in rv.data
assert b'"status": 0' in rv.data

def test_rest_upload_invalid(self):
def test_upload_unknown_plugin(self, monkeypatch):
monkeypatch.setattr(
'intercom.front_end_binding.InterComFrontEndBinding.get_available_analysis_plugins',
lambda _: ['plugin_1'],
)
data = {
'binary': standard_b64encode(b'test_file_content').decode(),
'file_name': 'test_file.txt',
'device_name': 'test_device',
'device_part': 'test_part',
'device_class': 'test_class',
'vendor': 'test_vendor',
'release_date': '01.01.1970',
'tags': '',
'requested_analysis_systems': ['dummy'],
**UPLOAD_DATA,
'requested_analysis_systems': ['plugin_1', 'plugin_2'],
}
response = self.test_client.put('/rest/firmware', json=data, follow_redirects=True).json
assert 'error_message' in response
assert 'The requested analysis plugins are not available' in response['error_message']
assert 'plugin_2' in response['error_message']
assert 'plugin_1' not in response['error_message']

def test_rest_upload_invalid(self):
data = {**UPLOAD_DATA}
data.pop('version')
rv = self.test_client.put('/rest/firmware', json=data, follow_redirects=True)
assert rv.json['message'] == 'Input payload validation failed'
assert 'version' in rv.json['errors']
Expand Down
10 changes: 10 additions & 0 deletions src/web_interface/rest/rest_firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ def put(self):
except MarshallingError as error:
logging.error(f'REST|firmware|PUT: Error in payload data: {error}')
return error_message(str(error), self.URL)

available_plugins = set(self.intercom.get_available_analysis_plugins()) - {'unpacker'}
unavailable_plugins = set(data['requested_analysis_systems']) - available_plugins
if unavailable_plugins:
return error_message(
f'The requested analysis plugins are not available: {", ".join(unavailable_plugins)}',
self.URL,
request_data={k: v for k, v in data.items() if k != 'binary'}, # don't send the firmware binary back
)

result = self._process_data(data)
if 'error_message' in result:
logging.warning('Submission not according to API guidelines! (data could not be parsed)')
Expand Down
Loading