Skip to content

Commit

Permalink
nfctokens: detect invalid cascade tag UIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
timhawes committed Apr 16, 2024
1 parent 3dea1f9 commit 8730ca8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
48 changes: 48 additions & 0 deletions nfctokens/migrations/0010_alter_nfctoken_uid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 4.2.11 on 2024-04-16 22:35

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("nfctokens", "0009_alter_nfctoken_options"),
]

operations = [
migrations.AlterField(
model_name="nfctoken",
name="uid",
field=models.CharField(
max_length=32,
unique=True,
validators=[
django.core.validators.RegexValidator(
message="Enter a valid UID of 8, 14 or 20 hexadecimal digits",
regex="^\\s*([0-9a-fA-F]{8}|[0-9a-fA-F]{14}|[0-9a-fA-F]{20})\\s*$",
),
django.core.validators.RegexValidator(
inverse_match=True,
message="This is a randomly-generated UID which cannot be used for authentication",
regex="^\\s*08[0-9a-fA-F]{6}\\s*$",
),
django.core.validators.RegexValidator(
inverse_match=True,
message="This is an invalid UID (contains a cascade tag)",
regex="^\\s*88[0-9a-fA-F]{6}\\s*$",
),
django.core.validators.RegexValidator(
inverse_match=True,
message="This is an invalid UID (contains a cascade tag)",
regex="^\\s*[0-9a-fA-F]{6}88[0-9a-fA-F]{6}\\s*$",
),
django.core.validators.RegexValidator(
inverse_match=True,
message="This is an invalid UID (contains a cascade tag)",
regex="^\\s*[0-9a-fA-F]{6}88[0-9a-fA-F]{12}\\s*$",
),
],
verbose_name="UID",
),
),
]
20 changes: 19 additions & 1 deletion nfctokens/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2022 Tim Hawes <[email protected]>
# SPDX-FileCopyrightText: 2022-2024 Tim Hawes <[email protected]>
#
# SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -43,6 +43,24 @@ class NFCToken(models.Model):
message="This is a randomly-generated UID which cannot be used for authentication",
inverse_match=True,
),
# Invalid due to cascading rules (byte 0 of a 4-byte UID cannot be 0x88)
RegexValidator(
regex=r"^\s*88[0-9a-fA-F]{6}\s*$",
message="This is an invalid UID (contains a cascade tag)",
inverse_match=True,
),
# Invalid due to cascading rules (byte 3 of a 7-byte UID cannot be 0x88)
RegexValidator(
regex=r"^\s*[0-9a-fA-F]{6}88[0-9a-fA-F]{6}\s*$",
message="This is an invalid UID (contains a cascade tag)",
inverse_match=True,
),
# Invalid due to cascading rules (byte 3 of a 10-byte UID cannot be 0x88)
RegexValidator(
regex=r"^\s*[0-9a-fA-F]{6}88[0-9a-fA-F]{12}\s*$",
message="This is an invalid UID (contains a cascade tag)",
inverse_match=True,
),
],
)
description = models.CharField(max_length=255, blank=True)
Expand Down

0 comments on commit 8730ca8

Please sign in to comment.