Skip to content

Commit 08387f0

Browse files
feat: smartlink redirects directly to CRE when only one CRE is linked (#938)
Closes #486
1 parent 912e2a7 commit 08387f0

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

application/tests/web_main_test.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ def test_smartlink(self) -> None:
585585
for head in response.headers:
586586
if head[0] == "Location":
587587
location = head[1]
588-
self.assertEqual(location, "/node/standard/CWE/sectionid/456")
588+
self.assertEqual(location, "/cre/222-222")
589589
self.assertEqual(302, response.status_code)
590590

591591
response = client.get(
@@ -596,7 +596,28 @@ def test_smartlink(self) -> None:
596596
for head in response.headers:
597597
if head[0] == "Location":
598598
location = head[1]
599-
self.assertEqual(location, "/node/standard/ASVS/section/v0.1.2")
599+
self.assertEqual(location, "/cre/333-333")
600+
self.assertEqual(302, response.status_code)
601+
602+
# Multiple CREs linked to same standard → should fall back to node page
603+
cwe_multi = defs.Standard(name="CWEmulti", sectionID="789")
604+
ce = defs.CRE(id="444-444", description="CE", name="CE", tags=[])
605+
cf = defs.CRE(id="555-555", description="CF", name="CF", tags=[])
606+
dcwe_multi = collection.add_node(cwe_multi)
607+
dce = collection.add_cre(ce)
608+
dcf = collection.add_cre(cf)
609+
collection.add_link(dce, dcwe_multi, ltype=defs.LinkTypes.LinkedTo)
610+
collection.add_link(dcf, dcwe_multi, ltype=defs.LinkTypes.LinkedTo)
611+
612+
response = client.get(
613+
"/smartlink/standard/CWEmulti/789",
614+
headers={"Content-Type": "application/json"},
615+
)
616+
location = ""
617+
for head in response.headers:
618+
if head[0] == "Location":
619+
location = head[1]
620+
self.assertEqual(location, "/node/standard/CWEmulti/sectionid/789")
600621
self.assertEqual(302, response.status_code)
601622

602623
# negative test, this cwe does not exist, therefore we redirect to Mitre!

application/web/web_main.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,15 @@ def index(path: str) -> Any:
656656
def smartlink(
657657
name: str, ntype: str = defs.Credoctypes.Standard.value, section: str = ""
658658
) -> Any:
659-
"""if node is found, show node, else redirect"""
659+
"""If a standard section node is found, redirect to it.
660+
661+
If the node has exactly one linked CRE, redirect directly to that CRE
662+
page instead of the intermediate standard-section page, so the user
663+
immediately sees the full wealth of CRE information.
664+
If the node has multiple CRE links, redirect to the standard-section
665+
node page as before. If the node is not found in the database, fall
666+
back to any known external redirect (e.g. the Mitre CWE catalogue).
667+
"""
660668
# ATTENTION: DO NOT MESS WITH THIS FUNCTIONALITY WITHOUT A TICKET AND CORE CONTRIBUTORS APPROVAL!
661669
# CRITICAL FUNCTIONALITY DEPENDS ON THIS!
662670
if posthog:
@@ -694,6 +702,17 @@ def smartlink(
694702
logger.info(
695703
f"found node of type {ntype}, name {name} and section {section}, redirecting to opencre"
696704
)
705+
cre_links = [
706+
lnk
707+
for lnk in nodes[0].links
708+
if lnk.document.doctype == defs.Credoctypes.CRE
709+
]
710+
if len(cre_links) == 1:
711+
logger.info(
712+
f"exactly one CRE linked to {ntype}/{name}/{section},"
713+
f" redirecting directly to CRE {cre_links[0].document.id}"
714+
)
715+
return redirect(f"/cre/{cre_links[0].document.id}")
697716
if found_section_id:
698717
return redirect(f"/node/{ntype}/{name}/sectionid/{section}")
699718
return redirect(f"/node/{ntype}/{name}/section/{section}")

0 commit comments

Comments
 (0)