Skip to content
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

Add artificial limit for PDF renderer #197

Merged
merged 2 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Possibility to transform '%Y-%m-%dT%H:%M:%S%z' into '%a, %b %d, %Y %I %p %Z' within a template [193](https://github.com/greenbone/pheme/pull/193)
- Possibility to limit PDF report size, by limiting included hosts/results [197](https://github.com/greenbone/pheme/pull/197)
### Changed
- Orientation marker in bar charts are configured as a amount of lines instead of every amount draw a line [186](https://github.com/greenbone/pheme/pull/186)
- NVT severity in float instead of int [194](https://github.com/greenbone/pheme/pull/194)
Expand Down
45 changes: 43 additions & 2 deletions pheme/transformation/scanreport/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,47 @@ def _replace_inline_svg_with_img_tags(
from_index = to_index


def enforce_limit(
data: Dict, parameter: Dict, format_type: str = "pdf"
) -> Dict:
if not "results" in data:
return data

limits = parameter.get("limits", {}).get(format_type, {})
max_hosts = limits.get("hosts")
max_results_in_host = limits.get("results")
logger.debug(
"limit max_results %s and max_hosts %s", max_results_in_host, max_hosts
nichtsfrei marked this conversation as resolved.
Show resolved Hide resolved
)
if not max_hosts and not max_results_in_host:
return data
host_cut = False
if max_hosts and len(data["results"]) > max_hosts:
data["results"] = data["results"][:max_hosts]
host_cut = True
result_cut = False
if max_results_in_host:
for result in data["results"]:
if len(result["results"]) > max_results_in_host:
result["results"] = result["results"][:max_results_in_host]
result_cut = True
if host_cut and not result_cut:
data["comment"] = limits.get(
"host_limit_msg", "Host limit: {host_limit}."
).format(host_limit=max_hosts)
elif result_cut and not host_cut:
data["comment"] = limits.get(
"result_limit_msg", "Result limit: {result_limit}."
).format(result_limit=max_results_in_host)
elif host_cut and result_cut:
data["comment"] = limits.get(
"host_result_limit_msg",
"Host limit {host_limit}; Result limit: {result_limit}.",
).format(host_limit=max_hosts, result_limit=max_results_in_host)

return data


class VulnerabilityPDFReport(Report):
"""
Is used to generate vulnerability reports in PDF.
Expand All @@ -161,11 +202,11 @@ class VulnerabilityPDFReport(Report):
format = "binary"

def apply(self, name: str, data: Dict, parameter: Dict):
logger.debug("got template: %s", self.__template)
data = enforce_limit(data, parameter)
css = _load_template(self.__css_template, parameter).render(
Context(parameter)
)
html_template = _load_template(self.__template)
html_template = _load_template(self.__template, parameter)
html = html_template.render(Context(_enrich(name, data, parameter)))
html = _replace_inline_svg_with_img_tags(html)
logger.debug("created html")
Expand Down