diff mbox series

[layerindex-web,3/4] layerindex: give a 404 for a non-numeric id in a URL

Message ID 20261116230401.42917-4-tim.orling@konsulko.com
State New
Headers show
Series Return 400 or 404 instead of server errors for bad URLs | expand

Commit Message

Tim Orling Nov. 16, 2026, 11:03 p.m. UTC
The URL patterns captured primary keys with [-\w]+, and BranchCompareView
passed the from_branch and to_branch parameters straight to the id
lookup, so a value such as /layerindex/recipe/NaN/ or from_branch= raised
ValueError and gave a 500. Match only digits in the URLs and check the
branch parameters, so these are a 404.

[YOCTO #16519]

AI-Generated: Claude Opus 5.5
Signed-off-by: Tim Orling <tim.orling@konsulko.com>
---
 layerindex/urls.py           | 18 ++++++-------
 layerindex/views.py          | 32 +++++++++++-----------
 tests/test_nonnumeric_ids.py | 51 ++++++++++++++++++++++++++++++++++++
 3 files changed, 77 insertions(+), 24 deletions(-)
 create mode 100644 tests/test_nonnumeric_ids.py
diff mbox series

Patch

diff --git a/layerindex/urls.py b/layerindex/urls.py
index 32e51d9..9910eee 100644
--- a/layerindex/urls.py
+++ b/layerindex/urls.py
@@ -72,19 +72,19 @@  urlpatterns = [
         name='layer_review'),
     re_path(r'^layer/(?P<slug>[-\.\w]+)/addnote/$',
         edit_layernote_view, {'template_name': 'layerindex/editlayernote.html'}, name="add_layernote"),
-    re_path(r'^layer/(?P<slug>[-\.\w]+)/editnote/(?P<pk>[-\w]+)/$',
+    re_path(r'^layer/(?P<slug>[-\.\w]+)/editnote/(?P<pk>\d+)/$',
         edit_layernote_view, {'template_name': 'layerindex/editlayernote.html'}, name="edit_layernote"),
-    re_path(r'^layer/(?P<slug>[-\.\w]+)/deletenote/(?P<pk>[-\w]+)/$',
+    re_path(r'^layer/(?P<slug>[-\.\w]+)/deletenote/(?P<pk>\d+)/$',
         delete_layernote_view, {'template_name': 'layerindex/deleteconfirm.html'}, name="delete_layernote"),
     re_path(r'^layer/(?P<slug>[-\.\w]+)/delete/$',
         delete_layer_view, {'template_name': 'layerindex/deleteconfirm.html'}, name="delete_layer"),
-    re_path(r'^recipe/(?P<pk>[-\w]+)/$',
+    re_path(r'^recipe/(?P<pk>\d+)/$',
         RecipeDetailView.as_view(
             template_name='layerindex/recipedetail.html'),
         name='recipe'),
     re_path(r'^layer/(?P<name>[-\.\w]+)/publish/$', publish_view, name="publish"),
     re_path(r'^layers/published/rss/$', PublishedLayersFeed(), name='published_layers_feed'),
-    re_path(r'^layerupdate/(?P<pk>[-\w]+)/$',
+    re_path(r'^layerupdate/(?P<pk>\d+)/$',
         LayerUpdateDetailView.as_view(
             template_name='layerindex/layerupdate.html'),
         name='layerupdate'),
@@ -114,7 +114,7 @@  urlpatterns = [
         UpdateListView.as_view(
             template_name='layerindex/updatelist.html'),
         name='update_list'),
-    re_path(r'^updates/(?P<pk>[-\w]+)/$',
+    re_path(r'^updates/(?P<pk>\d+)/$',
         UpdateDetailView.as_view(
             template_name='layerindex/updatedetail.html'),
         name='update'),
@@ -143,7 +143,7 @@  urlpatterns = [
     re_path(r'^oe-classic/stats/$',
         RedirectView.as_view(url=reverse_lazy('comparison_recipe_stats', kwargs={'branch': 'oe-classic'}), permanent=False),
         name='classic_recipe_stats'),
-    re_path(r'^oe-classic/recipe/(?P<pk>[-\w]+)/$',
+    re_path(r'^oe-classic/recipe/(?P<pk>\d+)/$',
         ClassicRecipeDetailView.as_view(
             template_name='layerindex/classicrecipedetail.html'),
         name='classic_recipe'),
@@ -161,15 +161,15 @@  urlpatterns = [
         ClassicRecipeStatsView.as_view(
             template_name='layerindex/classicstats.html'),
         name='comparison_recipe_stats'),
-    re_path(r'^comparison/recipe/(?P<pk>[-\w]+)/$',
+    re_path(r'^comparison/recipe/(?P<pk>\d+)/$',
         ClassicRecipeDetailView.as_view(
             template_name='layerindex/classicrecipedetail.html'),
         name='comparison_recipe'),
-    re_path(r'^comparison/select/(?P<pk>[-\w]+)/$',
+    re_path(r'^comparison/select/(?P<pk>\d+)/$',
         ComparisonRecipeSelectView.as_view(
             template_name='layerindex/comparisonrecipeselect.html'),
         name='comparison_select'),
-    re_path(r'^comparison/selectdetail/(?P<selectfor>[-\w]+)/(?P<pk>[-\w]+)/$',
+    re_path(r'^comparison/selectdetail/(?P<selectfor>[-\w]+)/(?P<pk>\d+)/$',
         ComparisonRecipeSelectDetailView.as_view(
             template_name='layerindex/comparisonrecipeselectdetail.html'),
         name='comparison_select_detail'),
diff --git a/layerindex/views.py b/layerindex/views.py
index 92da12b..d503a94 100644
--- a/layerindex/views.py
+++ b/layerindex/views.py
@@ -1979,33 +1979,35 @@  class BranchCompareView(FormView):
                     layer_ids.append(int(layer_id))
         return layer_ids
 
+    def get_branch(self, param):
+        # A non-numeric id is a 404, not a ValueError (AI-generated, Claude Opus 5.5)
+        branch_id = self.request.GET.get(param, None)
+        if branch_id is None:
+            return None
+        if not (branch_id.isascii() and branch_id.isdigit()):
+            raise Http404
+        return get_object_or_404(Branch, id=branch_id)
+
     def form_valid(self, form):
         return HttpResponseRedirect(reverse_lazy('branch_comparison', args=(form.cleaned_data['from_branch'].name, form.cleaned_data['to_branch'].name)))
 
     def get_initial(self):
         initial = super(BranchCompareView, self).get_initial()
-        from_branch_id = self.request.GET.get('from_branch', None)
-        if from_branch_id is not None:
-            initial['from_branch'] = get_object_or_404(Branch, id=from_branch_id)
-        to_branch_id = self.request.GET.get('to_branch', None)
-        if to_branch_id is not None:
-            initial['to_branch'] = get_object_or_404(Branch, id=to_branch_id)
+        from_branch = self.get_branch('from_branch')
+        if from_branch is not None:
+            initial['from_branch'] = from_branch
+        to_branch = self.get_branch('to_branch')
+        if to_branch is not None:
+            initial['to_branch'] = to_branch
         initial['layers'] = ','.join(str(i) for i in self.get_layer_ids())
         return initial
 
     def get_context_data(self, **kwargs):
         context = super(BranchCompareView, self).get_context_data(**kwargs)
-        from_branch_id = self.request.GET.get('from_branch', None)
-        to_branch_id = self.request.GET.get('to_branch', None)
-
         layer_ids = self.get_layer_ids()
-        from_branch = None
-        if from_branch_id is not None:
-            from_branch = get_object_or_404(Branch, id=from_branch_id)
+        from_branch = self.get_branch('from_branch')
         context['from_branch'] = from_branch
-        to_branch = None
-        if to_branch_id is not None:
-            to_branch = get_object_or_404(Branch, id=to_branch_id)
+        to_branch = self.get_branch('to_branch')
         context['to_branch'] = to_branch
         if from_branch and to_branch:
             context['added'], context['changed'], context['removed'] = self.get_recipes(from_branch, to_branch, layer_ids)
diff --git a/tests/test_nonnumeric_ids.py b/tests/test_nonnumeric_ids.py
new file mode 100644
index 0000000..1f64d4d
--- /dev/null
+++ b/tests/test_nonnumeric_ids.py
@@ -0,0 +1,51 @@ 
+# layerindex-web - tests for non-numeric ids in URLs
+#
+# Licensed under the MIT license, see COPYING.MIT for details
+#
+# SPDX-License-Identifier: MIT
+#
+# AI-generated, Claude Opus 5.5
+
+# [YOCTO #16519]: a non-numeric id in a URL or in the branch_comparison
+# parameters reached the database as an id and raised ValueError (a 500).
+
+import pytest
+from django.test import TestCase
+
+from layerindex.models import Branch, LayerItem
+
+
+@pytest.mark.django_db
+class TestNonNumericIds(TestCase):
+
+    def assert_not_found(self, url, params=None):
+        response = self.client.get(url, params or {})
+        self.assertEqual(response.status_code, 404, (url, params))
+
+    def test_recipe(self):
+        for pk in ('NaN', '_', '1e400', 'true', 'é'):
+            self.assert_not_found('/layerindex/recipe/%s/' % pk)
+
+    def test_classic_recipe(self):
+        self.assert_not_found('/layerindex/oe-classic/recipe/x/')
+
+    def test_comparison_recipe(self):
+        self.assert_not_found('/layerindex/comparison/recipe/x/')
+
+    def test_branch_comparison(self):
+        branch = Branch.objects.create(name='testbranch', bitbake_branch='master',
+                                       short_description='Test', sort_priority=1)
+        for value in ('', 'x', '²'):
+            for url in ('/layerindex/branch_comparison/', '/layerindex/branch_comparison_plain/'):
+                self.assert_not_found(url, {'from_branch': value, 'to_branch': branch.id})
+                self.assert_not_found(url, {'from_branch': branch.id, 'to_branch': value})
+
+    def test_branch_comparison_valid(self):
+        branch = Branch.objects.create(name='testbranch', bitbake_branch='master',
+                                       short_description='Test', sort_priority=1)
+        # The comparison shows the core layer by default
+        LayerItem.objects.create(name='openembedded-core', status='P', layer_type='A',
+                                 summary='core', description='core',
+                                 vcs_url='git://example.com/oe-core.git')
+        response = self.client.get('/layerindex/branch_comparison/', {'from_branch': branch.id})
+        self.assertEqual(response.status_code, 200)