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:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user