From patchwork Mon Nov 16 23:03:49 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim Orling X-Patchwork-Id: 104562 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by mx.groups.io with SMTP id smtpd.msgproc01-g2.6135.1794870254281308031 for ; Mon, 16 Nov 2026 15:04:14 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20260707 header.b=x+vFm2Y+; spf=pass (domain: gmail.com, ip: 209.85.210.182, mailfrom: ticotimo@gmail.com) Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-8a39e5161c1so4182197b3a.3 for ; Mon, 16 Nov 2026 15:04:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794870253; x=1795475053; darn=lists.yoctoproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=oLyHzlRnzU04rYjseLJm6GTKPWN3tA0X42DVVvUUQlM=; b=x+vFm2Y+JYnFutj7Sawki0FamPcJS7wog65PBzpQPF9ysh2ZHaFiUmLILQymFaZH8f mOlzvFF8IyStftdXRuhPCUl28z4LoPPad5kx0bXmFBJn7rC51AfR5fTt0bCRPPPHAceh kjLAfiEECKeJwmvk4UUxiwWnz9qVtqbgHb/tkXgijBu2i9u7NZOpEJ4jQFO16Td3J0Qb SDrDH7wW9FXQ+cxLeZoHsapWYTZkTU+MpIaQBo/kV9WmA9FxwiOUj9ThcpuscFfYxWag 7DEdwKd1ZoqH8swX+8lzGCl2yS1FwCvKnmbYnVEY99oxuLKcl1uiq9wlb0aWoqsVSfzE 5kdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794870253; x=1795475053; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=oLyHzlRnzU04rYjseLJm6GTKPWN3tA0X42DVVvUUQlM=; b=avTfqb0hgWtf56rDqDylvlSxbb/RnGG1DIq70wUitZxG0AU19PEBCYbzQFDMs91tJq KZ9IAcOYG1Vt9w445u7rtl8JNar1sJqSJ6uWgwWjSlneK/UKYeoxOamxnk4KE8OdfeN5 /5ntBni0N6oOFkvhWBC1FiAxv8ig+2YkdtIzwzDjwWpLN3Y2HMrkXMygFyyNL8kI1W5B emSzJFxEaMNFLJ3BDv2MNp3qCjAhKLe43bb2tEzbG46AF5ei0QgtCdySznRD2R9aCTTr NoLIo7BQpky7gXmGqGzA9mKFC+yuTlPisb4gXLyLXbpUFCW1Eb9dk3iLhMi272evWcx6 TWIA== X-Gm-Message-State: APuU4Z+EeZEhWyWHuuDoU7oMnRXp0j2WkUwuFnzq7b7+eH7Iet2NYCAw q5Ma0tk5vGX8ZGJNQ4LlySGsiMl4KEKrGXnDozcI5EdCi89gQadBbssEeNfGGA== X-Gm-Gg: AYBFou2BcJ3aj/GAzLr0tx77EpQYf8ZS/C9cls/lPckKPnJD7XRI8pxqUT/Y7Kt6mcQ n+/1snHAWEk3txIYFpbAorhnA5mDt6b125UelSliUO42pkmLgCByDStOKS6O9sQe86nl61FBULe VHl4rFmMNIxpUne7VTFOITVqbptHwdVu5PFc1ih2yC70ZBStN6yKRKcfOFWmKsWewe1NNau+j6+ Aw2duh4J6CI4k4bgHpNF1pANczOj+sbqZT3f5WrlrsX8kSU7gvQA/Am9x2yrKKyDCnY8r4tuLgY wPHq2qctqmriaHWoJ/oehdPGv2LUywwP9+nhnGpbduuHqGSas5NIQjQ4+VM5Xuwmovw5rnNUHSl OgHEZOU0Gq2qkR4lnKBpGhlsgt3DBmWSnHbC+uGZa3ys9nH/aN8XKkdMaN3l0YFFiE9Ps+r6EvM qxguoSbkpLUKa0gBeCIS9tpvmfiykgdYnH7Joth53HIUk4vNMOr1Z16fDCSHztBuSKK5HS/s8zd fgAzHp4WYM1EEb97HDGm9plW/kWBrLrmDpcq9fJNmY3+boyUER4NPC/0WhvYFcf6LEPu2x4L6+M JYNwjOzN0ZJEisQXTwHRJIINISbjsC6BEzmRJvecaCq4rQYfsnZ1YIZQHOxcDQ== X-Received: by 2002:a05:6a21:a46:b0:3e2:7a05:6bda with SMTP id adf61e73a8af0-3f7a83ba040mr12724825637.43.1794870253316; Mon, 16 Nov 2026 15:04:13 -0800 (PST) Return-Path: Received: from localhost.localdomain ([69.9.132.159]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8bed12a90f1sm4210535b3a.19.2026.11.16.15.04.11 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 16 Nov 2026 15:04:12 -0800 (PST) From: Tim Orling X-Google-Original-From: Tim Orling To: yocto-patches@lists.yoctoproject.org Cc: Tim Orling Subject: [layerindex-web][PATCH 1/4] layerindex/querysethelper.py: reject an invalid API filter or orderby with a 400 Date: Mon, 16 Nov 2026 15:03:49 -0800 Message-ID: <20261116230401.42917-2-tim.orling@konsulko.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261116230401.42917-1-tim.orling@konsulko.com> References: <20261116230401.42917-1-tim.orling@konsulko.com> MIME-Version: 1.0 List-Id: X-Webhook-Received: from 45-33-107-173.ip.linodeusercontent.com [45.33.107.173] by lin-tor-korg-lkml-alma10-1 with HTTPS for ; Mon, 16 Nov 2026 23:04:22 -0000 X-Groupsio-URL: https://lists.yoctoproject.org/g/yocto-patches/message/5435 An unknown field in "filter" or "orderby" raised BaseException, which neither Django nor the WSGI server catches, so the worker died and the client got a 502 (uWSGI) or 500. A filter value of the wrong type for its field, e.g. filter=id:abc, raised ValueError and gave a 500. Raise the REST framework's ParseError instead, which returns a 400 with the reason. [YOCTO #16517] AI-Generated: Claude Opus 5.5 Signed-off-by: Tim Orling --- layerindex/querysethelper.py | 16 ++++++++++++---- tests/test_restapi.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/layerindex/querysethelper.py b/layerindex/querysethelper.py index 0d1f317..2ca466a 100644 --- a/layerindex/querysethelper.py +++ b/layerindex/querysethelper.py @@ -8,7 +8,9 @@ import operator import functools +from django.core.exceptions import FieldError, ValidationError from django.db.models import Q, CharField +from rest_framework.exceptions import ParseError def _verify_parameters(g, mandatory_parameters): miss = [] @@ -109,12 +111,14 @@ def _get_search_results(search_term, queryset, model): # it uses the request and the model to validate input for the filter and orderby values def get_search_tuple(request, model): ordering_string, invalid = _validate_input(request.GET.get('orderby', ''), model) + # A 400 for the client: BaseException isn't caught by Django or the WSGI + # server, so it killed the worker (AI-generated, Claude Opus 5.5) if invalid: - raise BaseException("Invalid ordering model:" + str(model) + str(invalid)) + raise ParseError("Invalid orderby: " + str(invalid)) filter_string, invalid = _validate_input(request.GET.get('filter', ''), model) if invalid: - raise BaseException("Invalid filter " + str(invalid)) + raise ParseError("Invalid filter: " + str(invalid)) search_term = request.GET.get('search', '') return (filter_string, search_term, ordering_string) @@ -123,8 +127,12 @@ def get_search_tuple(request, model): # returns a lazy-evaluated queryset for a filter/search/order combination def params_to_queryset(model, queryset, filter_string, search_term, ordering_string): if filter_string: - filter_query = _get_filtering_query(filter_string) - queryset = queryset.filter(filter_query) + try: + filter_query = _get_filtering_query(filter_string) + queryset = queryset.filter(filter_query) + except (ValueError, ValidationError, FieldError) as e: + # e.g. filter=id:abc (AI-generated, Claude Opus 5.5) + raise ParseError("Invalid filter: " + str(e)) else: queryset = queryset.all() diff --git a/tests/test_restapi.py b/tests/test_restapi.py index af1f708..a1f733e 100644 --- a/tests/test_restapi.py +++ b/tests/test_restapi.py @@ -28,3 +28,35 @@ class TestRestApiDatetimes(TestCase): items = data['results'] if isinstance(data, dict) else data item = next(i for i in items if i['name'] == 'meta-test') self.assertEqual(item['updated'], '2026-10-18T14:37:05+0000') + + +@pytest.mark.django_db +class TestRestApiInvalidParameters(TestCase): + """Invalid filter and orderby values give a 400 ([YOCTO #16517])""" + + def assert_bad_request(self, params): + response = self.client.get('/layerindex/api/layerItems/', dict(params, format='json')) + self.assertEqual(response.status_code, 400, params) + + def test_unknown_filter_field(self): + # Raised BaseException, which killed the worker + self.assert_bad_request({'filter': 'pn:x'}) + + def test_filter_without_value(self): + self.assert_bad_request({'filter': 'name'}) + + def test_filter_value_of_wrong_type(self): + # Raised ValueError: a 500 + self.assert_bad_request({'filter': 'id:abc'}) + + def test_unknown_orderby_field(self): + self.assert_bad_request({'orderby': 'pn:-'}) + + def test_valid_filter(self): + LayerItem.objects.create(name='meta-test', status='P', layer_type='A', + summary='test', description='test', + vcs_url='git://example.com/meta-test.git') + response = self.client.get('/layerindex/api/layerItems/', + {'filter': 'name:meta-test', 'format': 'json'}) + self.assertEqual(response.status_code, 200) + self.assertEqual([i['name'] for i in response.json()], ['meta-test']) From patchwork Mon Nov 16 23:03:50 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim Orling X-Patchwork-Id: 104563 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by mx.groups.io with SMTP id smtpd.msgproc01-g2.6139.1794870259132390383 for ; Mon, 16 Nov 2026 15:04:19 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20260707 header.b=ExAIK0cJ; spf=pass (domain: gmail.com, ip: 209.85.210.170, mailfrom: ticotimo@gmail.com) Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-8b58d6f67a5so1930521b3a.2 for ; Mon, 16 Nov 2026 15:04:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794870258; x=1795475058; darn=lists.yoctoproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+8INfbtuqsY7AFsPFW966X/DkA3X6Onl2ULlEF6Jdo0=; b=ExAIK0cJ2qA0kgj1bXxeBRlgC4z1jptXXGX0ECmwp7VXLgxGNnv/eJXKRR0a1ffuiv wdNrxryCkYgpQd3eeawoDzswzXPBKSIMB+yTvpOiCbW9FF+LNGvCanhDSw8JGqnx0kRX NxrIHMfss32QYqSZr+byP1bQLwzmDb+O1t4mJ1mhy8qW3mMfzkX2BQP8Ji75hWXu5VE3 4EF2enhCXly7+yft6sQFeYg6clZu2YYO6+Ft8D5pKD5hgHhRerAmiPew6251KChDhBIp 6Wpb+y8mCA9N4BBDywYPfS9hqbq9nMRiIpJedijFld24qlTaWmYGNkpS2ufkLCCGvgyn 4/mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794870258; x=1795475058; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=+8INfbtuqsY7AFsPFW966X/DkA3X6Onl2ULlEF6Jdo0=; b=gEpvj/HleOEen2FQQrwUxZ8jaPNtBWQhFIdHo/Om/3QeFwCC0T+nhnpCF/P6okQFsK McHvBcTvgKeT6Ld6rcZwFxiIlNRfbU5GIEUMGoE30jOq2gcHjFOsxdehqWBMAPTcvEFH 9nwoJmg0kLcTeyhTdjMev9c/lQl5XFXOFjx1il+i9C167EZG40Ma/YNBLkaMLBmomikF bwQbh+81FJMonvnQVmqhk0aHpttLDQkB0Wm4BmJ7p+pTcA9oxvBjTbJjEM+XX68o4CRz ohzyEZxiAadYK3YAGQOimK+2NFFPqRjtpg9OYVB6yr5l3sd5ij/3qC5Pi/5Fj1iapZ3b WuyA== X-Gm-Message-State: APuU4Z8R/aG15QLdu0PsCKSRPGMt4McESU5IBKHjHh5kEs516gzyXq5K IP/jP/7KoIWL9zE1z8ooCQIflJRkjx5uKEZNn/ATn4U+o72a/pVZpgr490CV4g== X-Gm-Gg: AYBFou3JqqA/GU0ssN9K1Vn34JaaYiwxwSJWsYM+IHiRwZGnwD5gqj+dYYue7GTFAMX YobVjUozUfHtOQjRJAZIm5UVQCfogYl9wLhcc5BcYmRZUP1Io6dqTYiTNu/k67pKByYNxk6912f 7T6w0UPL9T7DTLzluVtb1k7ZnCmsjFXr7yq0Tf+zfh1byMpGBXVQqmyiK+GM5hF2euvoSPm9724 RRCn5rjhkfGek24N5H4GjJ7NzRx7XBlJt7gA8nxfZOye6rIazVtydwM8hRSAG2WajSOKGmGQJBE FWN10GeHOvSiHwq3PbMFD4ZT0sd+jydmCXwYlhcCK6Eb5Q4htwDh7q05v914BW6J8Y/j1H8VyYS Gd+MtIOUphLteHHQFOiomPFT/QLrYKIOVq2deFsioZVxyelSehcFnPg8OIKRH1OJKxsCMLv9WeV PgdsHA556xYOr6E2N5Gte5PNNW42mb7r/DK85esUX9t5SwtbS6lGmxef4pciveY97r5no6T3/O9 u3DhQYIEzrMewrbecRHx7M8HVHX9qiXruqfw+CEn8prHUtfdpIRUOYQ+0IqlxWpPRPfbVOKdS6A rqD2JNHvh3MpLQ0i7ZddDL4aAi0+weO9KqLuIvbOw6dvXxWWJ9Q2WALPvUnXjA== X-Received: by 2002:a05:6a00:3011:b0:8a0:70d8:a2ee with SMTP id d2e1a72fcca58-8beb84e3ff7mr11759217b3a.60.1794870258438; Mon, 16 Nov 2026 15:04:18 -0800 (PST) Return-Path: Received: from localhost.localdomain ([69.9.132.159]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8bed12a90f1sm4210535b3a.19.2026.11.16.15.04.16 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 16 Nov 2026 15:04:16 -0800 (PST) From: Tim Orling X-Google-Original-From: Tim Orling To: yocto-patches@lists.yoctoproject.org Cc: Tim Orling Subject: [layerindex-web][PATCH 2/4] layerindex/querysethelper.py: apply the API "orderby" parameter Date: Mon, 16 Nov 2026 15:03:50 -0800 Message-ID: <20261116230401.42917-3-tim.orling@konsulko.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261116230401.42917-1-tim.orling@konsulko.com> References: <20261116230401.42917-1-tim.orling@konsulko.com> MIME-Version: 1.0 List-Id: X-Webhook-Received: from 45-33-107-173.ip.linodeusercontent.com [45.33.107.173] by lin-tor-korg-lkml-alma10-1 with HTTPS for ; Mon, 16 Nov 2026 23:04:22 -0000 X-Groupsio-URL: https://lists.yoctoproject.org/g/yocto-patches/message/5436 params_to_queryset() worked out the column and direction from "orderby" but never ordered the queryset by them, so the parameter had no effect. Order by it, with a 400 for a field path that doesn't exist. It no longer evaluates the whole queryset just to check whether it is empty first. [YOCTO #16518] AI-Generated: Claude Opus 5.5 Signed-off-by: Tim Orling --- layerindex/querysethelper.py | 27 +++++++++++++++++++++++---- tests/test_restapi.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/layerindex/querysethelper.py b/layerindex/querysethelper.py index 2ca466a..26f687a 100644 --- a/layerindex/querysethelper.py +++ b/layerindex/querysethelper.py @@ -8,7 +8,7 @@ import operator import functools -from django.core.exceptions import FieldError, ValidationError +from django.core.exceptions import FieldDoesNotExist, FieldError, ValidationError from django.db.models import Q, CharField from rest_framework.exceptions import ParseError @@ -124,6 +124,18 @@ def get_search_tuple(request, model): return (filter_string, search_term, ordering_string) +def _check_field_path(model, path): + """Raise ParseError unless path (e.g. "layerbranch__layer__name") names a field""" + for name in path.split('__'): + if model is None: + raise ParseError("Invalid orderby: " + path) + try: + field = model._meta.get_field(name) + except FieldDoesNotExist: + raise ParseError("Invalid orderby: " + path) + model = field.related_model + + # returns a lazy-evaluated queryset for a filter/search/order combination def params_to_queryset(model, queryset, filter_string, search_term, ordering_string): if filter_string: @@ -139,12 +151,19 @@ def params_to_queryset(model, queryset, filter_string, search_term, ordering_str if search_term: queryset = _get_search_results(search_term, queryset, model) - if ordering_string and queryset: + # insure only distinct records (e.g. from multiple search hits) are returned + queryset = queryset.distinct() + + if ordering_string: column, order = ordering_string.split(':') if order.lower() == DESCENDING: column = '-' + column + # The ordering was worked out but never applied. A bad field path + # only fails when the query runs, so check it here (AI-generated, + # Claude Opus 5.5) + _check_field_path(model, column.lstrip('-')) + queryset = queryset.order_by(column) - # insure only distinct records (e.g. from multiple search hits) are returned - return queryset.distinct() + return queryset diff --git a/tests/test_restapi.py b/tests/test_restapi.py index a1f733e..fde1aec 100644 --- a/tests/test_restapi.py +++ b/tests/test_restapi.py @@ -60,3 +60,33 @@ class TestRestApiInvalidParameters(TestCase): {'filter': 'name:meta-test', 'format': 'json'}) self.assertEqual(response.status_code, 200) self.assertEqual([i['name'] for i in response.json()], ['meta-test']) + + +@pytest.mark.django_db +class TestRestApiOrderby(TestCase): + """orderby sorts the results ([YOCTO #16518])""" + + def setUp(self): + for name in ('meta-b', 'meta-a', 'meta-c'): + LayerItem.objects.create(name=name, status='P', layer_type='A', + summary='test', description='test', + vcs_url='git://example.com/%s.git' % name) + + def names(self, orderby): + response = self.client.get('/layerindex/api/layerItems/', + {'orderby': orderby, 'format': 'json'}) + self.assertEqual(response.status_code, 200) + return [i['name'] for i in response.json()] + + def test_ascending(self): + self.assertEqual(self.names('name:+'), ['meta-a', 'meta-b', 'meta-c']) + + def test_descending(self): + # DESCENDING in querysethelper.py + self.assertEqual(self.names('name:-'), ['meta-c', 'meta-b', 'meta-a']) + + def test_unknown_related_field(self): + # "layerbranch" passes the field-name check; the rest of the path is wrong + response = self.client.get('/layerindex/api/layerItems/', + {'orderby': 'layerbranch__nosuchfield:+', 'format': 'json'}) + self.assertEqual(response.status_code, 400) From patchwork Mon Nov 16 23:03:51 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tim Orling X-Patchwork-Id: 104564 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by mx.groups.io with SMTP id smtpd.msgproc02-g2.6020.1794870261980592503 for ; Mon, 16 Nov 2026 15:04:22 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20260707 header.b=xoyWU/wj; spf=pass (domain: gmail.com, ip: 209.85.215.180, mailfrom: ticotimo@gmail.com) Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-cddff772fffso407230a12.0 for ; Mon, 16 Nov 2026 15:04:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794870261; x=1795475061; darn=lists.yoctoproject.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=OQQC1k4aWDZyYdwWVP9lWSpW1VZaU3LoCi1BNAO83eY=; b=xoyWU/wjB6hRVkYB86UCeMmUtKLU+h8YdjFQV46NcOxVVC+1T3eaobv8SBcivQLWzT iNsm0G/jlB5dcqqna7rVbyCcDtyvTlM85lWCZd1O4pXJ/5UTQcWtdjTlPNyQ/BHG/7d/ dLiU1dO6/R0+LzegDOb/LiISmv7NIz14J6QYPWkxKbrD4SbbChUwQUqk1ecYmnizKy12 IUuide56tRppwF2lDVnygQBmxhgICh9OgUYfMmrLthBBS8mcp4vDnTSH0Cazhug9nmGI ZSZaGoR/le0La/Rg9mxjfbWACjwpuDLc47OrFN/17b9l3X09xQ7BaUADvNo7VRr/ikxw tMuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794870261; x=1795475061; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=OQQC1k4aWDZyYdwWVP9lWSpW1VZaU3LoCi1BNAO83eY=; b=koL5ldO3OC6TAaRguI63uAs4vUb/Vs8WmcTWC/acQ17JiwSMslrUhkNUQp88xNc8qj Ku/aqv56cO1Wqg1esDwz4NQOgQKGlCg39EvInH5pRXPimvgKAjAdhqB3zFYMrvyWjo0T jPOlk6GcCaOock+d0RXW5rMGlahn0aNzpe881d9ZzHU6X5hIk6N3yDT8Wm47PKptft6b +mCupqr8sddcrituaXsi57idGTx1B/arnVN9XGSS46apNCIuUcWuOXf4UubFKwhK27ee Jz3Vel7gaS0GOYy9VCkrCDydBWB4c+TwGAXnLlsaIHhWvAXmi12CEzSuFFjaPe2uFItk xr9Q== X-Gm-Message-State: APuU4Z98Uqai65r3/N+It+vbKlzCKXx0zzpBcrmI4L75D739UfKmYYJl edPqi9UzhE8ResabS6xSPUEXy4jCrAEaIKedaXseDH2Zm5PrXmplCKlF3Obg7A== X-Gm-Gg: AYBFou1z3syT060I5I71nGDXKnO7IKZ18lgBh488wp2J7a1Zr7Fwggt+lm0/vN4BaNd zytElcNmKmLD/YPnNRQ2d36YyXiyqjrtviGT+1iV5TotoRZq/aAqAW8+08vgW6TbMsVjyAj/hhE o9YDMFut6UTq/jY0J/apbktorGuzBU8+6VygI1o8mAMWbWHzILe53Npdi61krv0b/h7dW/emkk4 JsNWxHTn2x0N+p7NC06pFcT1Aw7Y84DDaQKS+NTAgcifKikivR2ISJ6ILpdKQ53wpzjzkr42APx agkpwYlLmGGRkssjDOk41s4hxFH4oWl777O5MzktbzzgDJic2mbiuEjgnlP+0ZCHYtw3wBG+vqY 0b3j3ojg6sh8ULxI2cfotGurrDeyj0cR9eD1fShcjfDsMZBqkl+tIE5bRU6QXS/CRO2bZT/siTN laJ2aqZ0as18c6a+b+ri7sWVKKHFvt4SpKppdn/98Kt1jRHaoh4E93Ojko3/rgAPJAAPs9FgitT rh0CwsvaLqRZg6UlJ+sq2sjsCj70+hxx7GsDSppEzkLBTnF5Cc8ymXTAjcXohxWbKtcPZ16OfeF guzsC6HMO5CTlZ/Q4gwVPg9nIXPlWHbnRNB/R99K2QRfmb8ma+c6SHD1Coxctw== X-Received: by 2002:a05:6a21:3d95:b0:3fa:df2d:d3e2 with SMTP id adf61e73a8af0-3fadf2dd835mr3882629637.16.1794870261072; Mon, 16 Nov 2026 15:04:21 -0800 (PST) Return-Path: Received: from localhost.localdomain ([69.9.132.159]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8bed12a90f1sm4210535b3a.19.2026.11.16.15.04.19 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 16 Nov 2026 15:04:20 -0800 (PST) From: Tim Orling X-Google-Original-From: Tim Orling To: yocto-patches@lists.yoctoproject.org Cc: Tim Orling Subject: [layerindex-web][PATCH 3/4] layerindex: give a 404 for a non-numeric id in a URL Date: Mon, 16 Nov 2026 15:03:51 -0800 Message-ID: <20261116230401.42917-4-tim.orling@konsulko.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261116230401.42917-1-tim.orling@konsulko.com> References: <20261116230401.42917-1-tim.orling@konsulko.com> MIME-Version: 1.0 List-Id: X-Webhook-Received: from 45-33-107-173.ip.linodeusercontent.com [45.33.107.173] by lin-tor-korg-lkml-alma10-1 with HTTPS for ; Mon, 16 Nov 2026 23:04:22 -0000 X-Groupsio-URL: https://lists.yoctoproject.org/g/yocto-patches/message/5437 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 --- 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 --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[-\.\w]+)/addnote/$', edit_layernote_view, {'template_name': 'layerindex/editlayernote.html'}, name="add_layernote"), - re_path(r'^layer/(?P[-\.\w]+)/editnote/(?P[-\w]+)/$', + re_path(r'^layer/(?P[-\.\w]+)/editnote/(?P\d+)/$', edit_layernote_view, {'template_name': 'layerindex/editlayernote.html'}, name="edit_layernote"), - re_path(r'^layer/(?P[-\.\w]+)/deletenote/(?P[-\w]+)/$', + re_path(r'^layer/(?P[-\.\w]+)/deletenote/(?P\d+)/$', delete_layernote_view, {'template_name': 'layerindex/deleteconfirm.html'}, name="delete_layernote"), re_path(r'^layer/(?P[-\.\w]+)/delete/$', delete_layer_view, {'template_name': 'layerindex/deleteconfirm.html'}, name="delete_layer"), - re_path(r'^recipe/(?P[-\w]+)/$', + re_path(r'^recipe/(?P\d+)/$', RecipeDetailView.as_view( template_name='layerindex/recipedetail.html'), name='recipe'), re_path(r'^layer/(?P[-\.\w]+)/publish/$', publish_view, name="publish"), re_path(r'^layers/published/rss/$', PublishedLayersFeed(), name='published_layers_feed'), - re_path(r'^layerupdate/(?P[-\w]+)/$', + re_path(r'^layerupdate/(?P\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[-\w]+)/$', + re_path(r'^updates/(?P\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[-\w]+)/$', + re_path(r'^oe-classic/recipe/(?P\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[-\w]+)/$', + re_path(r'^comparison/recipe/(?P\d+)/$', ClassicRecipeDetailView.as_view( template_name='layerindex/classicrecipedetail.html'), name='comparison_recipe'), - re_path(r'^comparison/select/(?P[-\w]+)/$', + re_path(r'^comparison/select/(?P\d+)/$', ComparisonRecipeSelectView.as_view( template_name='layerindex/comparisonrecipeselect.html'), name='comparison_select'), - re_path(r'^comparison/selectdetail/(?P[-\w]+)/(?P[-\w]+)/$', + re_path(r'^comparison/selectdetail/(?P[-\w]+)/(?P\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) From patchwork Mon Nov 16 23:03:52 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim Orling X-Patchwork-Id: 104565 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by mx.groups.io with SMTP id smtpd.msgproc02-g2.6024.1794870265338664869 for ; Mon, 16 Nov 2026 15:04:25 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20260707 header.b=piyBb1+Q; spf=pass (domain: gmail.com, ip: 209.85.210.169, mailfrom: ticotimo@gmail.com) Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-8a60f90a924so1605197b3a.3 for ; Mon, 16 Nov 2026 15:04:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794870265; x=1795475065; darn=lists.yoctoproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=aSalTJNQ99lPip3G9fhVoi79pdi+vMUsr25YydVxubg=; b=piyBb1+QUJdK7wTVGa7owbeiR1rBLuyidxxy41Yeer/Utnz9hlkZlnVrn/nF+kJoST OD2qyWiZBhNs3FgagPcofbXtyKki2OueIA3AlvCQiYcGlBsoQoopAb/F9SiQuiCXZ9eI 0D3/zA9NqgFx+2xh0rErcY0q32PdzvJvDKQqxC4Am2y9aObNmxaBN11nmT+okrFX26qd N2xy274keKw+K5lZrh6p14vwdpb/483BZapvhLHSDV3cMl8kI3w57DNf3y7OAx6eVnFi ZVuqRSWSmtbhQ4RmJ6NR5qge3aAsNwhEMPPuZiaAdcOPL+ToQVCGcxgVL7eMcRqBPr/D ySIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794870265; x=1795475065; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=aSalTJNQ99lPip3G9fhVoi79pdi+vMUsr25YydVxubg=; b=mN01jiS/CH2s/mrCA5ZQLEG0VwhDHdlU8RglP41MMYISHd1SaIp/cefVg33rT1YOd8 qOqOUx3yV615H+FE9TvQPdmhDKaoO5yCLvhA11iXvv3dN49LIYe5EBDrgE27BE5CGWMe IKqYyDTNWNbkGEFBuJCnIEsPPU0IRkDrLq/QCcz+LNFtldin1IilLUyunCeKCDTdOj8W 9vQfz0AiOQEeYaJcEWxCtAffMJGpRQaauzN8ZCevtCv9Lxx7/+H9UKGecyjYxBU9pTj9 SmWMu1KNEJguRDp/bsgsfxgOJl9lIFF7cGV/XsCSNwNl1+uEi468nN28C9aMcA6DvhN4 8KHA== X-Gm-Message-State: APuU4Z88Ez2LTdPNL09rcfukNCP+nslmnPQeMKyD0/+anNAncaMiJmlJ t/tyj4a0/USyKz8jhbbKIlxZvd2baFXueiSPy9iMRCzG2NRxDCbpQ74gSWlwCA== X-Gm-Gg: AYBFou1QVTBy2NAcvHESOr6EmoyGbS0Ty5GJ+5m2T9MdSGORfiWo6dQu0+SNRYp/wUB xN6GMKgfSGrbWj72OulskQifuZc93PUzRJ2EAX36tU2BjNZP7rnuu+z/SRXDk/6ShMmiZ6mRxtJ cygWg0aXkbwrzO4tr+DsDltJshQdlPKeTMRuboNnA4v+tIa/KyU2Z9QeP3d2gKF0VVLsL9XmDLe 8AUCKUBv1ude7YNBIiQD19CaUuBx0rKS2bXGWKFIj7JUs/cor/5bEux3VrTW3V6PQGdETopyKL/ p6BrP0/kLLlUBCdIdclr/wPmq867FP4rnUb6MMQ2S6ntqIPQAsqrptFrzXyLwryl1CY/ikH4uho 56r7H36WU/k0/DRmC/wfG/XYTZKIkSMCA9MVVm841gAo5EfSVPw2/T4Xq69RPhvjkS5weAhs+Ah gxz0DUnrn7waqLbQaj4x/je49dtqvVsmOBb+GO9XLrWD8WF/+KgdfiUx0ARq/0SKp86OYZLCeEw JYvqWvfFCvNq8/qIb7CpsDW5LqSfyTDNmPGkrnuW1hx7HtRfE5jukHBF/FgBZxQY5eBS/Kyvbje 8Q6BNlmzHDiE58OmMZzN6hS3XDYph1INkYt3YfpcCc9sl/ejHMDk3o1s9wYNkg== X-Received: by 2002:a05:6a20:e210:b0:3f5:ef59:3aaa with SMTP id adf61e73a8af0-3f7a70b1d36mr11527541637.16.1794870264571; Mon, 16 Nov 2026 15:04:24 -0800 (PST) Return-Path: Received: from localhost.localdomain ([69.9.132.159]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8bed12a90f1sm4210535b3a.19.2026.11.16.15.04.22 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 16 Nov 2026 15:04:22 -0800 (PST) From: Tim Orling X-Google-Original-From: Tim Orling To: yocto-patches@lists.yoctoproject.org Cc: Tim Orling Subject: [layerindex-web][PATCH 4/4] layerindex/middleware.py: reject NUL characters in the URL with a 400 Date: Mon, 16 Nov 2026 15:03:52 -0800 Message-ID: <20261116230401.42917-5-tim.orling@konsulko.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261116230401.42917-1-tim.orling@konsulko.com> References: <20261116230401.42917-1-tim.orling@konsulko.com> MIME-Version: 1.0 List-Id: X-Webhook-Received: from 45-33-107-173.ip.linodeusercontent.com [45.33.107.173] by lin-tor-korg-lkml-alma10-1 with HTTPS for ; Mon, 16 Nov 2026 23:04:32 -0000 X-Groupsio-URL: https://lists.yoctoproject.org/g/yocto-patches/message/5438 PostgreSQL text can't hold a NUL character, so a search term, API filter or URL path containing one, e.g. ?q=%00, raised DataError and gave a 500. MariaDB just found nothing. Forms already reject NUL in what they clean, but many views read the URL directly, so reject it once in a middleware. [YOCTO #16520] AI-Generated: Claude Opus 5.5 Signed-off-by: Tim Orling --- docker/settings.py | 1 + layerindex/middleware.py | 21 +++++++++++++++- settings.py | 1 + tests/test_null_characters.py | 45 +++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/test_null_characters.py diff --git a/docker/settings.py b/docker/settings.py index cd8bacb..0cfcd83 100644 --- a/docker/settings.py +++ b/docker/settings.py @@ -96,6 +96,7 @@ SECRET_KEY = os.getenv('SECRET_KEY', '') MIDDLEWARE = ( 'corsheaders.middleware.CorsMiddleware', + 'layerindex.middleware.RejectNullCharactersMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', diff --git a/layerindex/middleware.py b/layerindex/middleware.py index 6766b1d..1112448 100644 --- a/layerindex/middleware.py +++ b/layerindex/middleware.py @@ -7,9 +7,28 @@ # SPDX-License-Identifier: MIT from django.utils.deprecation import MiddlewareMixin -from django.http import HttpResponseRedirect +from django.http import HttpResponseBadRequest, HttpResponseRedirect from django.urls import reverse from reversion.middleware import RevisionMiddleware class NonAtomicRevisionMiddleware(RevisionMiddleware): atomic = False + + +class RejectNullCharactersMiddleware: + """ + Return a 400 for a request with a NUL character in its path or query + string. PostgreSQL text can't hold NUL, so any lookup with such a value + raised DataError and gave a 500. Forms already reject NUL in what they + clean; this covers the views that read the URL directly. + (AI-generated, Claude Opus 5.5) + """ + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + if '\x00' in request.path or any( + '\x00' in key or any('\x00' in value for value in values) + for key, values in request.GET.lists()): + return HttpResponseBadRequest('NUL characters are not allowed') + return self.get_response(request) diff --git a/settings.py b/settings.py index 495af1e..573311a 100644 --- a/settings.py +++ b/settings.py @@ -96,6 +96,7 @@ SECRET_KEY = '' MIDDLEWARE = ( 'corsheaders.middleware.CorsMiddleware', + 'layerindex.middleware.RejectNullCharactersMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', diff --git a/tests/test_null_characters.py b/tests/test_null_characters.py new file mode 100644 index 0000000..e4c685d --- /dev/null +++ b/tests/test_null_characters.py @@ -0,0 +1,45 @@ +# layerindex-web - tests for NUL characters in requests +# +# Licensed under the MIT license, see COPYING.MIT for details +# +# SPDX-License-Identifier: MIT +# +# AI-generated, Claude Opus 5.5 + +# [YOCTO #16520]: PostgreSQL text can't hold NUL, so a search or URL with +# one raised DataError (a 500). MariaDB returned no results, so these tests +# check for the 400 rather than for the DataError. + +import pytest +from django.test import TestCase + +from layerindex.models import Branch + + +@pytest.mark.django_db +class TestNullCharacters(TestCase): + + def setUp(self): + # The migrations may have created it already + Branch.objects.get_or_create(name='master', defaults=dict( + bitbake_branch='master', short_description='Master', sort_priority=1)) + + def assert_bad_request(self, url, params=None): + response = self.client.get(url, params or {}) + self.assertEqual(response.status_code, 400, (url, params)) + + def test_search(self): + self.assert_bad_request('/layerindex/branch/master/recipes/', {'q': 'a\x00b'}) + + def test_api_filter(self): + self.assert_bad_request('/layerindex/api/recipes/', {'filter': 'pn:\x00'}) + + def test_parameter_name(self): + self.assert_bad_request('/layerindex/branch/master/recipes/', {'q\x00': 'a'}) + + def test_path(self): + self.assert_bad_request('/layerindex/branch/master/layer/a%00b/') + + def test_search_without_nul(self): + response = self.client.get('/layerindex/branch/master/recipes/', {'q': 'ab'}) + self.assertEqual(response.status_code, 200)