Skip to content

Commit

Permalink
chg: More new annotations in website
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafiot committed Jan 16, 2024
1 parent 57eb38e commit ea9a634
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
6 changes: 3 additions & 3 deletions bin/start_website.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

class Website(AbstractManager):

def __init__(self, loglevel: int | None=None):
def __init__(self, loglevel: int | None=None) -> None:
super().__init__(loglevel)
self.script_name = 'website'
self.process = self._launch_website()
self.set_running()

def _launch_website(self):
def _launch_website(self) -> Popen: # type: ignore[type-arg]
website_dir = get_homedir() / 'website'
ip = get_config('generic', 'website_listen_ip')
port = get_config('generic', 'website_listen_port')
Expand All @@ -33,7 +33,7 @@ def _launch_website(self):
cwd=website_dir)


def main():
def main() -> None:
w = Website()
w.run(sleep_in_sec=10)

Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ warn_return_any = False
show_error_context = True
pretty = True

exclude = website|vulnerabilitylookup/feeders/cvelistv5/|vulnerabilitylookup/feeders/github/|vulnerabilitylookup/feeders/gsd/|vulnerabilitylookup/feeders/pysec/|dumps/
exclude = vulnerabilitylookup/feeders/cvelistv5/|vulnerabilitylookup/feeders/github/|vulnerabilitylookup/feeders/gsd/|vulnerabilitylookup/feeders/pysec/|dumps/
35 changes: 18 additions & 17 deletions website/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import logging.config

from importlib.metadata import version
from typing import Optional
from typing import Any

from flask import Flask, request, render_template, flash, redirect, url_for
from flask_bootstrap import Bootstrap5 # type: ignore
from flask_restx import Api, Resource # type: ignore
from werkzeug import Response as WerkzeugResponse

from vulnerabilitylookup.vulnerabilitylookup import VulnerabilityLookup

Expand Down Expand Up @@ -49,7 +50,7 @@ def get_sri(directory: str, filename: str) -> str:

@app.route('/', methods=['GET', 'POST'])
@app.route('/search', methods=['GET', 'POST'])
def search():
def search() -> str | WerkzeugResponse:
if request.method == 'HEAD':
# Just returns ack if the webserver is running
return 'Ack'
Expand Down Expand Up @@ -106,7 +107,7 @@ def search():


@app.route('/recent', methods=['GET'])
def recent():
def recent() -> str:
# For the webinterface, we want the most recent entries by source
source_to_show = ['github', 'cvelistv5', 'pysec', 'gsd']
default_source = 'cvelistv5'
Expand Down Expand Up @@ -139,28 +140,28 @@ def vulnerability_view(vulnerability_id: str) -> str:

@api.route('/redis_up')
@api.doc(description='Check if redis is up and running')
class RedisUp(Resource):
class RedisUp(Resource): # type: ignore[misc]

def get(self):
def get(self) -> bool:
return vulnerabilitylookup.check_redis_up()


@api.route('/api/cve/<string:vulnerability_id>')
@api.route('/vulnerability/<string:vulnerability_id>')
@api.doc(description='Get a vulnerability')
class Vulnerability(Resource):
class Vulnerability(Resource): # type: ignore[misc]

def get(self, vulnerability_id: str):
def get(self, vulnerability_id: str) -> dict[str, Any] | None:
with_meta = True if request.args.get('with_meta') else False
return vulnerabilitylookup.get_vulnerability(vulnerability_id, with_meta=with_meta)


@api.route('/api/dbInfo')
@api.route('/info')
@api.doc(description='Get more information about the current databases in use and when it was updated')
class Info(Resource):
class Info(Resource): # type: ignore[misc]

def get(self):
def get(self) -> dict[str, Any]:
return vulnerabilitylookup.get_info()


Expand All @@ -170,31 +171,31 @@ def get(self):
@api.route('/last/<string:source>')
@api.route('/last/<string:source>/<int:number>')
@api.doc(description='Get the last CVEs')
class Last(Resource):
class Last(Resource): # type: ignore[misc]

def get(self, source: str | None=None, number: int | None=30):
def get(self, source: str | None=None, number: int | None=30) -> list[dict[str, Any]]:
return vulnerabilitylookup.get_last(source, number)


@api.route('/api/browse')
@api.doc(description='Get the known vendors')
class Vendors(Resource):
class Vendors(Resource): # type: ignore[misc]

def get(self):
def get(self) -> list[str]:
return list(vulnerabilitylookup.get_vendors())


@api.route('/api/browse/<string:vendor>')
@api.doc(description='Get the known products for a vendor')
class VendorProducts(Resource):
class VendorProducts(Resource): # type: ignore[misc]

def get(self, vendor: str):
def get(self, vendor: str) -> list[str]:
return list(vulnerabilitylookup.get_vendor_products(vendor))


@api.route('/api/search/<string:vendor>/<string:product>')
@api.doc(description='Get the the vulnerabilities per vendor and a specific product')
class VendorProductVulnerabilities(Resource):
class VendorProductVulnerabilities(Resource): # type: ignore[misc]

def get(self, vendor: str, product: str):
def get(self, vendor: str, product: str) -> dict[str, list[tuple[str, dict[str, Any]]]]:
return vulnerabilitylookup.get_vendor_product_vulnerabilities(vendor, product)
8 changes: 6 additions & 2 deletions website/web/helpers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#!/usr/bin/env python3

from __future__ import annotations

import json
import os

from functools import lru_cache
from pathlib import Path
from typing import Dict

from flask import Request

from vulnerabilitylookup.default import get_homedir


def src_request_ip(request) -> str:
def src_request_ip(request: Request) -> str | None:
# NOTE: X-Real-IP is the IP passed by the reverse proxy in the headers.
real_ip = request.headers.get('X-Real-IP')
if not real_ip:
Expand All @@ -30,6 +34,6 @@ def get_secret_key() -> bytes:


@lru_cache(64)
def sri_load() -> Dict[str, Dict[str, str]]:
def sri_load() -> dict[str, dict[str, str]]:
with (get_homedir() / 'website' / 'web' / 'sri.txt').open() as f:
return json.load(f)

0 comments on commit ea9a634

Please sign in to comment.