Validate what a form accepts, and keep a diff to one asset
The validation tail from the 2026-09-03 run. Each is small; together they are the difference between a field that checks its input and one that looks like it does. Subnets accepted any string containing a slash, so 'notanip/xx' was stored - and _devices_in_subnet then swallowed the ValueError from ip_network and returned an empty device list, so the subnet looked real and permanently empty. It is parsed with the same library that has to read it later, a prefix length is required (ipaddress reads a bare 10.1.1.0 as a /32, which is legal and never what somebody typing a subnet meant), and the stored form is normalised so 10.1.1.5/24 and 10.1.1.0/24 cannot become two rows nobody can tell apart. VLAN numbers had no range check: 99999 and -5 were stored. Worse, a non-numeric value reached a duplicate probe that compares against an integer column, which MySQL coerces to 0 - so 'xyz' matched an existing VLAN 0 and the 409 named a row that had nothing to do with it. 1-4094 per 802.1Q, checked before the probe. Deleting a VLAN that subnets still reference now answers 409 like every sibling in-use guard, rather than 400, which told the caller their request was malformed when the request was fine and the state was the problem. An appversion sha256 was stored raw, so an over-long value was truncated to the column width and came back as 64 plausible hex characters - indistinguishable from a real digest, and wrong. A digest is a claim the delivery path checks against, so it is 64 hex characters or it is refused. The releasedate goes through the strict parser for the same reason it did everywhere else. PUT /api/settings/<key> ignored the declared valuetype. "A6-not-a-number" went into an integer key verbatim and every consumer then read 0, because get_typed_value swallows the ValueError - the setting looked set and the behaviour was the default. /api/maplevels told a caller who omitted the level name that 'buildingname and buildingid are required'; buildingname is a field that handler never reads. It now names the fields it actually wants. Restoring a guide revision that is already current writes nothing, because save() dedups on the content hash - and the response still said "Restored version N as a new draft version", describing a revision that does not exist while the unchanged revision list disagreed. GET /api/backups/revisions/<id>/diff?against=<id> accepted any revision id, so pointing it at another asset's revision rendered that asset's configuration as the "before" side of this asset's history: a diff that reads as a change on this machine and describes a different one. Same asset and same kind, or refused. The default branch was always asset-scoped; only the explicit one was not. Two frontend fixes: the per-page selector rendered blank on any list defaulting to a size not in [10,20,50,100] (network devices uses 25), so the operator could not see the current page size; and an emptied PC alias was dropped from the payload instead of sent, so it could not be cleared - the form came back showing the alias that had just been deleted. Twenty-two tests. The backups guard verified by planting. Scope: product
This commit is contained in:
@@ -40,3 +40,53 @@ def test_version_delivery_metadata_defaults_to_null(client, db, auth_headers):
|
||||
data = resp.get_json()['data']
|
||||
assert data['sha256'] is None
|
||||
assert data['silentargs'] is None
|
||||
|
||||
|
||||
# --- a digest is a claim something checks against ---------------------------
|
||||
#
|
||||
# sha256 and releasedate were stored raw. An over-long digest was silently
|
||||
# truncated to the column width and came back as 64 plausible hex characters -
|
||||
# indistinguishable from a real SHA-256, and wrong. A malformed releasedate
|
||||
# reached MySQL, which wrote 0000-00-00 and then broke every read of that
|
||||
# application, with no API way to remove the row.
|
||||
|
||||
def _app(client, headers, name='Digest Test App'):
|
||||
created = client.post('/api/applications', headers=headers,
|
||||
json={'appname': name})
|
||||
assert created.status_code in (200, 201), created.get_json()
|
||||
data = created.get_json()['data']
|
||||
return data.get('appid') or data.get('applicationid')
|
||||
|
||||
|
||||
def test_a_digest_that_is_not_a_sha256_is_refused(client, db, auth_headers):
|
||||
appid = _app(client, auth_headers, 'Digest refuse app')
|
||||
for bad in ('c' * 100, 'nothex' * 10, 'abc', 'C' * 63):
|
||||
response = client.post('/api/applications/%d/versions' % appid,
|
||||
headers=auth_headers,
|
||||
json={'version': 'v-%s' % bad[:4],
|
||||
'sha256': bad})
|
||||
assert response.status_code == 400, (bad, response.get_json())
|
||||
|
||||
|
||||
def test_a_real_digest_is_accepted_and_lowercased(client, db, auth_headers):
|
||||
appid = _app(client, auth_headers, 'Digest accept app')
|
||||
digest = 'A' * 64
|
||||
response = client.post('/api/applications/%d/versions' % appid,
|
||||
headers=auth_headers,
|
||||
json={'version': 'v1', 'sha256': digest})
|
||||
assert response.status_code in (200, 201), response.get_json()
|
||||
assert response.get_json()['data']['sha256'] == 'a' * 64
|
||||
|
||||
|
||||
def test_a_malformed_release_date_is_refused_and_nothing_is_committed(
|
||||
client, db, auth_headers):
|
||||
from shopdb.core.models import AppVersion
|
||||
|
||||
appid = _app(client, auth_headers, 'Date refuse app')
|
||||
response = client.post('/api/applications/%d/versions' % appid,
|
||||
headers=auth_headers,
|
||||
json={'version': 'poison-1',
|
||||
'releasedate': 'tomorrow'})
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert AppVersion.query.filter_by(version='poison-1').first() is None, \
|
||||
'a refused version must not be committed'
|
||||
|
||||
@@ -54,3 +54,46 @@ def test_the_retention_setting_actually_drives_the_report(client, db,
|
||||
assert report.status_code == 200, report.get_json()
|
||||
assert report.get_json()['data']['retentiondays'] == 30, \
|
||||
'the setting the page writes must be the one the service reads'
|
||||
|
||||
|
||||
# --- a declared valuetype is a promise to every reader -----------------------
|
||||
#
|
||||
# PUT /api/settings/<key> stored whatever arrived. "A6-not-a-number" went into
|
||||
# an integer key verbatim, and every consumer then read 0, because
|
||||
# Setting.get_typed_value swallows the ValueError. The setting looked set, the
|
||||
# behaviour was the default, and nothing said so.
|
||||
|
||||
def test_an_integer_setting_refuses_something_that_is_not_a_number(
|
||||
client, db, auth_headers):
|
||||
refused = client.put('/api/settings/audit_retentiondays',
|
||||
json={'value': 'not-a-number'}, headers=auth_headers)
|
||||
assert refused.status_code == 400, refused.get_json()
|
||||
assert 'whole-number' in refused.get_json()['data']['error']['message']
|
||||
|
||||
# And the old value survives, rather than being replaced by a 0 nobody
|
||||
# asked for.
|
||||
reading = client.get('/api/settings/audit_retentiondays',
|
||||
headers=auth_headers)
|
||||
assert reading.status_code == 200
|
||||
assert str(reading.get_json()['data']['value']) != 'not-a-number'
|
||||
|
||||
|
||||
def test_an_integer_setting_still_accepts_a_number(client, db, auth_headers):
|
||||
accepted = client.put('/api/settings/audit_retentiondays',
|
||||
json={'value': 45}, headers=auth_headers)
|
||||
assert accepted.status_code == 200, accepted.get_json()
|
||||
|
||||
|
||||
def test_a_boolean_setting_refuses_a_word_that_is_not_true_or_false(
|
||||
client, db, auth_headers):
|
||||
refused = client.put('/api/settings/securesend_allow_guests',
|
||||
json={'value': 'maybe'}, headers=auth_headers)
|
||||
assert refused.status_code == 400, refused.get_json()
|
||||
|
||||
|
||||
def test_a_boolean_setting_accepts_the_usual_spellings(client, db,
|
||||
auth_headers):
|
||||
for value in (True, False, 'true', 'false', '1', '0'):
|
||||
response = client.put('/api/settings/securesend_allow_guests',
|
||||
json={'value': value}, headers=auth_headers)
|
||||
assert response.status_code == 200, (value, response.get_json())
|
||||
|
||||
@@ -1568,3 +1568,99 @@ def test_each_reporting_pc_keeps_its_own_current_revision(bk_app, bk_plugin):
|
||||
assert set(current) == {'GGBX0NH3ESF', 'NEWPC001'}, \
|
||||
'each reporting PC has a current revision of its own'
|
||||
assert _summary(revisions[0], islatest=True)['islatest'] is True
|
||||
|
||||
|
||||
# --- a diff has to be about ONE asset ---------------------------------------
|
||||
#
|
||||
# ?against=<id> accepted any revision id. Pointing it at another asset's
|
||||
# revision rendered that asset's configuration values as the "before" side of
|
||||
# this asset's history: a diff that reads as a change on this machine while
|
||||
# describing a different one. The default (no ?against) has always been
|
||||
# asset-scoped; only the explicit form was not.
|
||||
|
||||
def _bk_admin_headers(bk_app):
|
||||
"""An admin in the BACKUPS app's own database.
|
||||
|
||||
The shared `client` fixture talks to the session app, which is a different
|
||||
database entirely - a revision created through bk_plugin is invisible to
|
||||
it, which is why a route test has to be driven from this app.
|
||||
"""
|
||||
from werkzeug.security import generate_password_hash
|
||||
|
||||
from shopdb.core.models import Permission, Role, User
|
||||
|
||||
with bk_app.app_context():
|
||||
Permission.seed()
|
||||
user = User.query.filter_by(username='bkadmin').first()
|
||||
if user is None:
|
||||
admin = Role.query.filter_by(rolename='admin').first()
|
||||
if admin is None:
|
||||
admin = Role(rolename='admin', description='Admin')
|
||||
_db.session.add(admin)
|
||||
_db.session.flush()
|
||||
admin.permissions = Permission.query.all()
|
||||
user = User(username='bkadmin', email='bkadmin@test.local',
|
||||
passwordhash=generate_password_hash('bkpass123'),
|
||||
isactive=True)
|
||||
user.roles = [admin]
|
||||
_db.session.add(user)
|
||||
_db.session.commit()
|
||||
|
||||
client = bk_app.test_client()
|
||||
login = client.post('/api/auth/login',
|
||||
json={'username': 'bkadmin', 'password': 'bkpass123'})
|
||||
assert login.status_code == 200, login.get_json()
|
||||
token = login.get_json()['data']['access_token']
|
||||
return client, {'Authorization': 'Bearer %s' % token}
|
||||
|
||||
|
||||
def test_a_diff_against_another_assets_revision_is_refused(bk_app, bk_plugin):
|
||||
from shopdb.core.models import Asset, AssetType
|
||||
from plugins.backups.models import BackupRevision
|
||||
|
||||
with bk_app.app_context():
|
||||
bk_plugin.apply_collector_payload(_payload())
|
||||
mine = (_db.session.query(BackupRevision)
|
||||
.order_by(BackupRevision.backuprevisionid.desc()).first())
|
||||
|
||||
# A revision belonging to a different asset entirely.
|
||||
assettype = AssetType.query.first() or AssetType(assettype='machine')
|
||||
_db.session.add(assettype)
|
||||
_db.session.flush()
|
||||
other_asset = Asset(assetnumber='OTHER-ASSET-1',
|
||||
assettypeid=assettype.assettypeid)
|
||||
_db.session.add(other_asset)
|
||||
_db.session.flush()
|
||||
theirs = BackupRevision(assetid=other_asset.assetid,
|
||||
backupkind=mine.backupkind,
|
||||
storagebackend='shopdb',
|
||||
contenthash='a' * 64)
|
||||
theirs.payload = {'schema': ntlars.SCHEMA, 'keys': []}
|
||||
_db.session.add(theirs)
|
||||
_db.session.commit()
|
||||
mineid, theirsid = mine.backuprevisionid, theirs.backuprevisionid
|
||||
|
||||
client, headers = _bk_admin_headers(bk_app)
|
||||
refused = client.get('/api/backups/revisions/%d/diff?against=%d'
|
||||
% (mineid, theirsid), headers=headers)
|
||||
assert refused.status_code == 400, refused.get_json()
|
||||
assert 'different asset' in refused.get_json()['data']['error']['message']
|
||||
|
||||
|
||||
def test_a_diff_against_a_sibling_revision_still_works(bk_app, bk_plugin):
|
||||
"""Scoping it must not break the case the feature exists for."""
|
||||
from plugins.backups.models import BackupRevision
|
||||
|
||||
with bk_app.app_context():
|
||||
bk_plugin.apply_collector_payload(_payload())
|
||||
changed = CONFIGUREDREG.replace('"Cnc"=""', '"Cnc"="OKUMA"')
|
||||
bk_plugin.apply_collector_payload(_payload(reg=changed))
|
||||
_db.session.commit()
|
||||
rows = (_db.session.query(BackupRevision)
|
||||
.order_by(BackupRevision.backuprevisionid.desc()).limit(2).all())
|
||||
newer, older = rows[0].backuprevisionid, rows[1].backuprevisionid
|
||||
|
||||
client, headers = _bk_admin_headers(bk_app)
|
||||
allowed = client.get('/api/backups/revisions/%d/diff?against=%d'
|
||||
% (newer, older), headers=headers)
|
||||
assert allowed.status_code == 200, allowed.get_json()
|
||||
|
||||
@@ -576,3 +576,24 @@ def test_a_section_counts_as_something_to_tick():
|
||||
]
|
||||
# heading + step + two checklist items
|
||||
assert checklistitemcount(blocks) == 4
|
||||
|
||||
|
||||
def test_restoring_the_current_version_says_nothing_changed(gd_app, gd_clean):
|
||||
"""save() dedups on the content hash, so restoring the version that is
|
||||
already current writes nothing. Reporting "Restored version N as a new
|
||||
draft version" described a revision that did not exist, and the unchanged
|
||||
revision list quietly disagreed with the message."""
|
||||
from plugins.guides.models import GuideRevision
|
||||
from plugins.guides.services import authoring
|
||||
with gd_app.app_context():
|
||||
guide = _newguide()
|
||||
_, first = authoring.save(guide, title='T',
|
||||
blocks=[_block('b1', 'paragraph', text='only')])
|
||||
_db.session.commit()
|
||||
before = guide.revision
|
||||
|
||||
authoring.restore(guide, first)
|
||||
_db.session.commit()
|
||||
|
||||
assert guide.revision == before, 'nothing new was written'
|
||||
assert GuideRevision.query.filter_by(guideid=guide.guideid).count() == 1
|
||||
|
||||
80
tests/test_plugins/test_network_validation.py
Normal file
80
tests/test_plugins/test_network_validation.py
Normal file
@@ -0,0 +1,80 @@
|
||||
"""Network reference data has to survive being typed at.
|
||||
|
||||
Three findings from the 2026-09-03 run, all in the same family: a validation
|
||||
that looked like one and was not.
|
||||
|
||||
* cidr was checked for a '/' and nothing else, so 'notanip/xx' was stored.
|
||||
_devices_in_subnet then swallowed the ValueError from ip_network and
|
||||
returned an empty list, so the subnet looked real and permanently empty.
|
||||
* vlannumber had no range check, so 99999 and -5 were accepted - and a
|
||||
non-numeric value was worse, because the duplicate probe compared a string
|
||||
against an integer column, which MySQL coerces to 0, so 'xyz' matched an
|
||||
existing VLAN 0 and the 409 named a row that had nothing to do with it.
|
||||
* deleting a VLAN that subnets still reference answered 400, while every
|
||||
sibling in-use guard in the product answers 409.
|
||||
"""
|
||||
import pytest
|
||||
|
||||
|
||||
def _vlan(client, headers, number, name='Test VLAN'):
|
||||
return client.post('/api/network/vlans', headers=headers,
|
||||
json={'vlannumber': number, 'name': name})
|
||||
|
||||
|
||||
@pytest.mark.parametrize('cidr', [
|
||||
'notanip/xx',
|
||||
'10.1.1.0/33',
|
||||
'hello/24',
|
||||
'10.1.1.0',
|
||||
'/24',
|
||||
])
|
||||
def test_a_subnet_refuses_something_that_is_not_a_network(client, db,
|
||||
auth_headers, cidr):
|
||||
response = client.post('/api/network/subnets', headers=auth_headers,
|
||||
json={'cidr': cidr, 'name': 'Bad subnet'})
|
||||
assert response.status_code == 400, (cidr, response.get_json())
|
||||
|
||||
|
||||
def test_a_real_subnet_is_accepted_and_normalised(client, db, auth_headers):
|
||||
"""A host address with a prefix is the same network; storing both spellings
|
||||
would make two rows nobody can tell apart."""
|
||||
response = client.post('/api/network/subnets', headers=auth_headers,
|
||||
json={'cidr': '10.1.1.5/24', 'name': 'Real subnet'})
|
||||
assert response.status_code in (200, 201), response.get_json()
|
||||
assert response.get_json()['data']['cidr'] == '10.1.1.0/24'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('number', [0, 4095, 99999, -5, 'xyz'])
|
||||
def test_a_vlan_number_outside_the_standard_is_refused(client, db,
|
||||
auth_headers, number):
|
||||
response = _vlan(client, auth_headers, number)
|
||||
assert response.status_code == 400, (number, response.get_json())
|
||||
|
||||
|
||||
@pytest.mark.parametrize('number', [1, 100, 4094])
|
||||
def test_a_valid_vlan_number_is_accepted(client, db, auth_headers, number):
|
||||
response = _vlan(client, auth_headers, number, name='VLAN %s' % number)
|
||||
assert response.status_code in (200, 201), response.get_json()
|
||||
|
||||
|
||||
def test_a_duplicate_vlan_names_the_number_it_actually_clashed_with(
|
||||
client, db, auth_headers):
|
||||
assert _vlan(client, auth_headers, 42, name='First').status_code in (200, 201)
|
||||
clash = _vlan(client, auth_headers, 42, name='Second')
|
||||
assert clash.status_code == 409
|
||||
assert '42' in clash.get_json()['data']['error']['message']
|
||||
|
||||
|
||||
def test_deleting_a_vlan_in_use_is_a_conflict_like_every_other_guard(
|
||||
client, db, auth_headers):
|
||||
created = _vlan(client, auth_headers, 77, name='In use')
|
||||
vlanid = created.get_json()['data']['vlanid']
|
||||
subnet = client.post('/api/network/subnets', headers=auth_headers,
|
||||
json={'cidr': '10.7.7.0/24', 'name': 'Attached',
|
||||
'vlanid': vlanid})
|
||||
assert subnet.status_code in (200, 201), subnet.get_json()
|
||||
|
||||
refused = client.delete('/api/network/vlans/%d' % vlanid,
|
||||
headers=auth_headers)
|
||||
assert refused.status_code == 409, refused.get_json()
|
||||
assert refused.get_json()['data']['error']['code'] == 'CONFLICT'
|
||||
Reference in New Issue
Block a user