[extensions-web/bugfix/url-validation] extensions: validate extension model upon edit and drop ftp/ftps schemes




commit 4b51ecb1b582aeadcb34a533cd1bb15b7f62eb65
Author: Yuri Konotopov <ykonotopov gnome org>
Date:   Thu Feb 25 13:56:46 2021 +0400

    extensions: validate extension model upon edit and drop ftp/ftps schemes
    
    Thanks to Fabian Bräunlein for finding this issue.
    
    Signed-off-by: Yuri Konotopov <ykonotopov gnome org>

 sweettooth/extensions/fields.py                       | 16 ++++++++++++++++
 .../extensions/migrations/0008_auto_20210225_1248.py  | 19 +++++++++++++++++++
 sweettooth/extensions/models.py                       |  4 +++-
 sweettooth/extensions/views.py                        |  1 +
 4 files changed, 39 insertions(+), 1 deletion(-)
---
diff --git a/sweettooth/extensions/fields.py b/sweettooth/extensions/fields.py
new file mode 100644
index 00000000..0fec141b
--- /dev/null
+++ b/sweettooth/extensions/fields.py
@@ -0,0 +1,16 @@
+"""
+    GNOME Shell Extensions Repository
+    Copyright (C) 2021 Yuri Konotopov <ykonotopov gnome org>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Affero General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+"""
+
+from django.db.models import URLField
+from django.core.validators import URLValidator
+
+
+class HttpURLField(URLField):
+    default_validators = [URLValidator(schemes=['http', 'https'])]
diff --git a/sweettooth/extensions/migrations/0008_auto_20210225_1248.py 
b/sweettooth/extensions/migrations/0008_auto_20210225_1248.py
new file mode 100644
index 00000000..e9017855
--- /dev/null
+++ b/sweettooth/extensions/migrations/0008_auto_20210225_1248.py
@@ -0,0 +1,19 @@
+# Generated by Django 2.2.17 on 2021-02-25 12:48
+
+from django.db import migrations
+import sweettooth.extensions.fields
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('extensions', '0007_auto_20201219_2046'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='extension',
+            name='url',
+            field=sweettooth.extensions.fields.HttpURLField(blank=True),
+        ),
+    ]
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index 6d83ae87..81653cfe 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -23,6 +23,8 @@ from django.db import models
 from django.dispatch import Signal
 from django.urls import reverse
 
+from .fields import HttpURLField
+
 (STATUS_UNREVIEWED,
  STATUS_REJECTED,
  STATUS_INACTIVE,
@@ -108,7 +110,7 @@ class Extension(models.Model):
     slug = autoslug.AutoSlugField(populate_from="name")
     creator = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True, on_delete=models.PROTECT)
     description = models.TextField(blank=True)
-    url = models.URLField(blank=True)
+    url = HttpURLField(blank=True)
     created = models.DateTimeField(auto_now_add=True)
     downloads = models.PositiveIntegerField(default=0)
     popularity = models.IntegerField(default=0)
diff --git a/sweettooth/extensions/views.py b/sweettooth/extensions/views.py
index da165e51..6d8442af 100644
--- a/sweettooth/extensions/views.py
+++ b/sweettooth/extensions/views.py
@@ -348,6 +348,7 @@ def ajax_inline_edit_view(request, extension):
 
     models.extension_updated.send(sender=extension, extension=extension)
 
+    extension.full_clean()
     extension.save()
 
     return value


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]