-
-
Notifications
You must be signed in to change notification settings - Fork 532
Detect OWASP project level non-compliance and adjust health scores #3354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
saichethana28
wants to merge
11
commits into
OWASP:main
Choose a base branch
from
saichethana28:feature/project-level-compliance-simple
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dee13ab
feat: add project level non-compliance flag to health metrics
saichethana28 d76cdb1
detect OWASP project level non-compliance
saichethana28 093a8b7
detect project level non-compliance and penalize health score
saichethana28 4ff4573
Add project level compliance and integrate with health scoring
saichethana28 f00db44
Fix docstring formatting and satisfy ruff checks
saichethana28 042ddd6
Use latest health metrics for project level compliance check
saichethana28 69e3e23
Update project level compliance using latest metrics and align tests
saichethana28 9cc930d
Handle JSON decode errors when fetching project levels
saichethana28 564b7b9
Merge branch 'main' into feature/project-level-compliance-simple
saichethana28 d6bca1a
Fix incorrect compliance update count after bulk save
saichethana28 1ca60a1
Fix NaN issue
saichethana28 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
backend/apps/owasp/management/commands/owasp_update_project_level_compliance.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| """Update OWASP project level compliance status. | ||
|
|
||
| This command compares locally stored project levels against | ||
| official OWASP project classification data and determines | ||
| whether each project is level-compliant. | ||
|
|
||
| Projects whose locally assigned level does not match the | ||
| official OWASP classification are flagged as non-compliant. | ||
| This compliance flag is later used during health score | ||
| calculation to apply penalties where appropriate. | ||
| """ | ||
|
|
||
| import re | ||
| from decimal import Decimal, InvalidOperation | ||
|
|
||
| import requests | ||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from apps.owasp.models.project_health_metrics import ProjectHealthMetrics | ||
| from apps.owasp.utils.project_level import map_level | ||
|
|
||
| LEVELS_URL = ( | ||
| "https://raw.githubusercontent.com/OWASP/owasp.github.io/main/_data/project_levels.json" | ||
| ) | ||
|
|
||
|
|
||
| def normalize_name(name: str) -> str: | ||
| """Normalize project names for comparison.""" | ||
| return re.sub(r"[^a-z0-9]+", "", name.lower().replace("owasp", "")) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| """Detect and persist OWASP project level compliance. | ||
|
|
||
| This command fetches official OWASP project level data, | ||
| maps it to the internal ProjectLevel enum, and updates | ||
| project health metrics to indicate whether a project's | ||
| stored level matches the official classification. | ||
| """ | ||
|
|
||
| help = "Detect and flag OWASP project level non-compliance." | ||
|
|
||
| def handle(self, *args, **options) -> None: | ||
| """Execute project level compliance detection. | ||
|
|
||
| For each project health metric entry, this method | ||
| determines the expected project level based on | ||
| official OWASP data and marks the project as | ||
| level non-compliant when a mismatch is detected. | ||
| """ | ||
| try: | ||
| response = requests.get(LEVELS_URL, timeout=15) | ||
| response.raise_for_status() | ||
| official_data = response.json() | ||
| except (requests.RequestException, ValueError) as exc: | ||
| self.stderr.write(self.style.ERROR(f"Failed to fetch official project levels: {exc}")) | ||
| return | ||
|
|
||
saichethana28 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| by_repo: dict[str, Decimal] = {} | ||
| by_name: dict[str, Decimal] = {} | ||
|
|
||
| for item in official_data: | ||
| raw_level = item.get("level") | ||
| try: | ||
| level = Decimal(str(raw_level)) | ||
| except (InvalidOperation, TypeError, ValueError): | ||
| continue | ||
|
|
||
| if repo := item.get("repo"): | ||
| by_repo[repo.lower()] = level | ||
|
|
||
| if name := item.get("name"): | ||
| by_name[normalize_name(name)] = level | ||
|
|
||
| metrics = ProjectHealthMetrics.get_latest_health_metrics().select_related("project") | ||
| updated_metrics = [] | ||
|
|
||
| for metric in metrics: | ||
| project = metric.project | ||
| official_level = None | ||
|
|
||
| if project.owasp_url: | ||
| slug = project.owasp_url.rstrip("/").split("/")[-1].lower() | ||
| official_level = by_repo.get(slug) | ||
|
|
||
| if official_level is None: | ||
| official_level = by_name.get(normalize_name(project.name)) | ||
|
|
||
| if official_level is None: | ||
| continue | ||
|
|
||
| expected_level = map_level(official_level) | ||
| if expected_level is None: | ||
| continue | ||
|
|
||
| metric.level_non_compliant = project.level != expected_level | ||
| updated_metrics.append(metric) | ||
|
|
||
| updated_count = len(updated_metrics) | ||
|
|
||
| if updated_metrics: | ||
| ProjectHealthMetrics.bulk_save( | ||
| updated_metrics, | ||
| fields=["level_non_compliant"], | ||
| ) | ||
|
|
||
| self.stdout.write( | ||
| self.style.SUCCESS(f"Updated level compliance for {updated_count} projects.") | ||
| ) | ||
21 changes: 21 additions & 0 deletions
21
backend/apps/owasp/migrations/0070_projecthealthmetrics_level_non_compliant.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Generated by Django on 2026-01-14 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("owasp", "0069_alter_project_contribution_data_and_more"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="projecthealthmetrics", | ||
| name="level_non_compliant", | ||
| field=models.BooleanField( | ||
| default=False, | ||
| verbose_name="Is level non-compliant", | ||
| help_text="True when local project level differs from official OWASP level.", | ||
| ), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """Utilities for mapping OWASP official numeric project levels. | ||
|
|
||
| This module acts as a translation layer between the OWASP | ||
| project level definitions published upstream and the | ||
| Nest internal project classification system. | ||
| """ | ||
|
|
||
| from decimal import Decimal, InvalidOperation | ||
| from typing import cast | ||
|
|
||
| from apps.owasp.models.enums.project import ProjectLevel | ||
|
|
||
| _LEVEL_MAP = { | ||
| Decimal(4): ProjectLevel.FLAGSHIP, | ||
| Decimal("3.5"): ProjectLevel.FLAGSHIP, | ||
| Decimal(3): ProjectLevel.PRODUCTION, | ||
| Decimal(2): ProjectLevel.INCUBATOR, | ||
| Decimal(1): ProjectLevel.LAB, | ||
| Decimal(0): ProjectLevel.OTHER, | ||
| } | ||
|
|
||
|
|
||
| def map_level(level: Decimal) -> ProjectLevel | None: | ||
| """Map an OWASP official numeric project level to ProjectLevel. | ||
|
|
||
| Args: | ||
| level (Decimal): The numeric project level provided by OWASP. | ||
|
|
||
| Returns: | ||
| ProjectLevel | None: The mapped ProjectLevel value if valid, | ||
| otherwise None for unsupported or invalid levels. | ||
|
|
||
| """ | ||
| try: | ||
| parsed_level = Decimal(str(level)) | ||
| except (InvalidOperation, TypeError, ValueError): | ||
| return None | ||
|
|
||
| if not parsed_level.is_finite(): | ||
| return None | ||
|
|
||
| if parsed_level < 0: | ||
cubic-dev-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return None | ||
|
|
||
| return cast("ProjectLevel | None", _LEVEL_MAP.get(parsed_level)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.