From 042d14ff7e43936615b0f6195dd4305bdb78f376 Mon Sep 17 00:00:00 2001 From: maybe-hello-world Date: Sat, 22 Jul 2023 19:16:18 +0000 Subject: [PATCH 1/4] fix: return 404 for non-existing revisions Links to non-existing revisions to docs should return 404 --- ietf/doc/tests.py | 9 +++++++-- ietf/doc/views_doc.py | 7 ++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ietf/doc/tests.py b/ietf/doc/tests.py index 5a949b091c..2633fa4616 100644 --- a/ietf/doc/tests.py +++ b/ietf/doc/tests.py @@ -1558,9 +1558,9 @@ def test_document_primary_and_history_views(self): self.assertEqual(r.status_code, 200) self.assertContains(r, "%s-01"%docname) - # Fetch version number which is too large, that should redirect to main page + # Fetch version number which is too large, that should return 404 r = self.client.get(urlreverse("ietf.doc.views_doc.document_main", kwargs=dict(name=doc.name,rev="02"))) - self.assertEqual(r.status_code, 302) + self.assertEqual(r.status_code, 404) # Fetch 00 version which should result that version r = self.client.get(urlreverse("ietf.doc.views_doc.document_main", kwargs=dict(name=doc.name,rev="00"))) @@ -2033,6 +2033,11 @@ def test_document_bibtex(self): # self.assertNotIn('doi', entry) + # check for non-existent revision + url = urlreverse('ietf.doc.views_doc.document_bibtex', kwargs=dict(name=draft.name, rev='99')) + r = self.client.get(url) + self.assertEqual(r.status_code, 404) + def test_document_bibxml(self): draft = IndividualDraftFactory.create() docname = '%s-%s' % (draft.name, draft.rev) diff --git a/ietf/doc/views_doc.py b/ietf/doc/views_doc.py index c87602a9fc..9974eaac0b 100644 --- a/ietf/doc/views_doc.py +++ b/ietf/doc/views_doc.py @@ -218,9 +218,8 @@ def document_main(request, name, rev=None, document_html=False): snapshot = True doc = h break - - if not snapshot and document_html is False: - return redirect('ietf.doc.views_doc.document_main', name=name) + else: + raise Http404() if doc.type_id == "charter": # find old group, too @@ -1103,6 +1102,8 @@ def document_bibtex(request, name, rev=None): if rev == h.rev: doc = h break + else: + raise Http404() if doc.is_rfc(): # This needs to be replaced with a lookup, as the mapping may change From ebc654c0359c2ebc0746718a5789dae98cc3ac84 Mon Sep 17 00:00:00 2001 From: maybe-hello-world Date: Sat, 22 Jul 2023 22:11:22 +0000 Subject: [PATCH 2/4] fix: change rfc/rev and search behaviour --- ietf/doc/tests.py | 19 +++++++++++++++++-- ietf/doc/views_doc.py | 8 ++++++-- ietf/doc/views_search.py | 4 ++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/ietf/doc/tests.py b/ietf/doc/tests.py index 24673d6c4c..8819861a36 100644 --- a/ietf/doc/tests.py +++ b/ietf/doc/tests.py @@ -261,6 +261,17 @@ def test_search_for_name(self): parsed = urlparse(r["Location"]) self.assertEqual(parsed.path, urlreverse('ietf.doc.views_search.search')) self.assertEqual(parse_qs(parsed.query)["name"][0], "draft-ietf-doesnotexist-42") + + def test_search_rfc(self): + rfc = WgRfcFactory(name="rfc0000") + + # search for existing RFC should redirect directly to the RFC page + r = self.client.get(urlreverse('ietf.doc.views_search.search_for_name', kwargs=dict(name=rfc.name))) + self.assertRedirects(r, f'/doc/{rfc.name}/', status_code=302, target_status_code=200) + + # search for existing RFC with revision number should redirect to the RFC page + r = self.client.get(urlreverse('ietf.doc.views_search.search_for_name', kwargs=dict(name=rfc.name + "-99")), follow=True) + self.assertRedirects(r, f'/doc/{rfc.name}/', status_code=302, target_status_code=200) def test_frontpage(self): r = self.client.get("/") @@ -1558,9 +1569,9 @@ def test_document_primary_and_history_views(self): self.assertEqual(r.status_code, 200) self.assertContains(r, "%s-01"%docname) - # Fetch version number which is too large, that should return 404 + # Fetch version number which is too large, that should redirect to main page r = self.client.get(urlreverse("ietf.doc.views_doc.document_main", kwargs=dict(name=doc.name,rev="02"))) - self.assertEqual(r.status_code, 404) + self.assertEqual(r.status_code, 302) # Fetch 00 version which should result that version r = self.client.get(urlreverse("ietf.doc.views_doc.document_main", kwargs=dict(name=doc.name,rev="00"))) @@ -1617,6 +1628,10 @@ def test_document_charter(self): CharterFactory(name='charter-ietf-mars') r = self.client.get(urlreverse("ietf.doc.views_doc.document_main", kwargs=dict(name="charter-ietf-mars"))) self.assertEqual(r.status_code, 200) + + def test_incorrect_rfc_url(self): + r = self.client.get(urlreverse("ietf.doc.views_doc.document_main", kwargs=dict(name="rfc8989", rev="00"))) + self.assertEqual(r.status_code, 404) def test_document_conflict_review(self): ConflictReviewFactory(name='conflict-review-imaginary-irtf-submission') diff --git a/ietf/doc/views_doc.py b/ietf/doc/views_doc.py index 9974eaac0b..22f7a7a196 100644 --- a/ietf/doc/views_doc.py +++ b/ietf/doc/views_doc.py @@ -191,6 +191,9 @@ def interesting_doc_relations(doc): return interesting_relations_that, interesting_relations_that_doc def document_main(request, name, rev=None, document_html=False): + if name.startswith("rfc") and rev is not None: + raise Http404() + doc = get_object_or_404(Document.objects.select_related(), docalias__name=name) # take care of possible redirections @@ -218,8 +221,9 @@ def document_main(request, name, rev=None, document_html=False): snapshot = True doc = h break - else: - raise Http404() + + if not snapshot and document_html is False: + return redirect('ietf.doc.views_doc.document_main', name=name) if doc.type_id == "charter": # find old group, too diff --git a/ietf/doc/views_search.py b/ietf/doc/views_search.py index c500e702eb..8b66c7c8d4 100644 --- a/ietf/doc/views_search.py +++ b/ietf/doc/views_search.py @@ -290,8 +290,8 @@ def cached_redirect(cache_key, url): redirect_to = find_unique(rev_split.group(1)) if redirect_to: rev = rev_split.group(2) - # check if we can redirect directly to the rev - if DocHistory.objects.filter(doc__docalias__name=redirect_to, rev=rev).exists(): + # check if we can redirect directly to the rev if it's draft, if rfc - always redirect to main page + if not redirect_to.startswith('rfc') and DocHistory.objects.filter(doc__docalias__name=redirect_to, rev=rev).exists(): return cached_redirect(cache_key, urlreverse("ietf.doc.views_doc.document_main", kwargs={ "name": redirect_to, "rev": rev })) else: return cached_redirect(cache_key, urlreverse("ietf.doc.views_doc.document_main", kwargs={ "name": redirect_to })) From 585badf42d1f52ac0bef9e32de4ae4d642af4fcc Mon Sep 17 00:00:00 2001 From: maybe-hello-world Date: Sat, 22 Jul 2023 22:14:17 +0000 Subject: [PATCH 3/4] refactor: fix tab level --- ietf/doc/views_doc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ietf/doc/views_doc.py b/ietf/doc/views_doc.py index 22f7a7a196..fb4079e9d7 100644 --- a/ietf/doc/views_doc.py +++ b/ietf/doc/views_doc.py @@ -223,7 +223,7 @@ def document_main(request, name, rev=None, document_html=False): break if not snapshot and document_html is False: - return redirect('ietf.doc.views_doc.document_main', name=name) + return redirect('ietf.doc.views_doc.document_main', name=name) if doc.type_id == "charter": # find old group, too From 17fe4d526bb786d2b135d6ca31bee6adf7e57c0f Mon Sep 17 00:00:00 2001 From: maybe-hello-world Date: Sun, 23 Jul 2023 03:26:10 +0000 Subject: [PATCH 4/4] fix: return 404 for rfc revision for bibtex --- ietf/doc/tests.py | 11 ++++++----- ietf/doc/views_doc.py | 5 +++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/ietf/doc/tests.py b/ietf/doc/tests.py index 8819861a36..65fb80a158 100644 --- a/ietf/doc/tests.py +++ b/ietf/doc/tests.py @@ -2008,6 +2008,12 @@ def test_document_bibtex(self): # self.assertNotIn('day', entry) + # test for incorrect case - revision for RFC + rfc = WgRfcFactory(name="rfc0000") + url = urlreverse('ietf.doc.views_doc.document_bibtex', kwargs=dict(name=rfc.name, rev='00')) + r = self.client.get(url) + self.assertEqual(r.status_code, 404) + april1 = IndividualRfcFactory.create( stream_id = 'ise', states = [('draft','rfc'),('draft-iesg','pub')], @@ -2048,11 +2054,6 @@ def test_document_bibtex(self): # self.assertNotIn('doi', entry) - # check for non-existent revision - url = urlreverse('ietf.doc.views_doc.document_bibtex', kwargs=dict(name=draft.name, rev='99')) - r = self.client.get(url) - self.assertEqual(r.status_code, 404) - def test_document_bibxml(self): draft = IndividualDraftFactory.create() docname = '%s-%s' % (draft.name, draft.rev) diff --git a/ietf/doc/views_doc.py b/ietf/doc/views_doc.py index fb4079e9d7..a14026d434 100644 --- a/ietf/doc/views_doc.py +++ b/ietf/doc/views_doc.py @@ -1083,6 +1083,9 @@ def document_history(request, name): def document_bibtex(request, name, rev=None): + if name.startswith('rfc') and rev is not None: + raise Http404() + # Make sure URL_REGEXPS did not grab too much for the rev number if rev != None and len(rev) != 2: mo = re.search(r"^(?P[0-9]{1,2})-(?P[0-9]{2})$", rev) @@ -1106,8 +1109,6 @@ def document_bibtex(request, name, rev=None): if rev == h.rev: doc = h break - else: - raise Http404() if doc.is_rfc(): # This needs to be replaced with a lookup, as the mapping may change