Skip to content

Commit

Permalink
new: [pdf] Added pdf export for zap
Browse files Browse the repository at this point in the history
Signed-off-by: Romain Kieffer <[email protected]>
  • Loading branch information
romainkieffer committed Aug 1, 2024
1 parent eb85af6 commit 4613979
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
7 changes: 6 additions & 1 deletion testing/templates/check_zap.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "base.html" %}
{% load static %}

{% load tags %}
{% block content %}

<main class="row p-3 flex-column align-items-center">
Expand Down Expand Up @@ -29,6 +29,11 @@ <h3 class="card-title">Zap Scanner</h3>
<input type="submit" value="Test" class="btn btn-secondary">
</form>
</div>
{% if alerts %}
<div class="d-flex justify-content-center pb-3">
<a href="export/{{ target }}" value="Export to pdf" class="btn btn-secondary">Export this to PDF</a>
</div>
{% endif %}
<div class="card-footer text-center text-muted">
<!-- TODO change href --><a
href="{% url 'knowledge_base' %}#tests-email">About the test</a></div>
Expand Down
3 changes: 3 additions & 0 deletions testing/templates/zap_pdf_wrapper.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Result of {{ test | capitalize }} scan on {{ site }}</h1>

{% include "zap_report.html" %}
5 changes: 2 additions & 3 deletions testing/templates/zap_report.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{% load tags %}
{% if alerts %}
<section class="col-lg-11 col-md-12 row my-5 border border-2 rounded p-4">
{% for alert in alerts %}
Expand All @@ -15,11 +14,11 @@
{% endif %}
{{ alert.alert }}
</div>
<h3>Description:</h3>
<h5>Description:</h5>
<div class="col-lg-12 fs-4">
{{ alert.description }}
</div>
<h3>Solution:</h3>
<h5>Solution:</h5>
<div class="col-lg-12 fs-4">
{{ alert.solution }}
</div>
Expand Down
4 changes: 2 additions & 2 deletions testing/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

urlpatterns = [
path("http-test/", views.http_test, name="http_test"),
path("zap-test/", views.zap_test, name="zap_test"),
path("zap/", views.zap_test, name="zap_test"),
# path("web-test/", views.web_test, name="web_test"),
path("email-test/", views.email_test, name="email_test"),
path("file-test/", views.file_test, name="file_test"),
path("infra-test/", views.web_server_test, name="infra-test"),
path("spf-generator/", views.spf_generator, name="spf-generator"),
path("dmarc-generator/", views.dmarc_generator, name="dmarc-generator"),
path("email-policy-generator/", views.record_generator, name="email_policy_generator"),

path("<test>/export/<site>", views.pdf_from_template, name="pdf_from_template"),
# path('whois-lookup/', views.ping_test, name='ping_test'),

# path("web-test/", views.web_test, name="web_test"),
Expand Down
22 changes: 19 additions & 3 deletions testing/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import datetime
import ipaddress
import os
import re
import socket
import time
import io

import jinja2
import xmltodict
import weasyprint

from typing import Any, Dict
from urllib.parse import parse_qs, urlparse
Expand Down Expand Up @@ -139,7 +141,7 @@ def zap_test(request):
# json_report, html_report = zap_scan(target, api_key)
# context = json_report['site'][0]
alerts = zap_scan(target, api_key)
context = {'alerts': alerts}
context = {'alerts': alerts, 'target': target}
nb_tests += 1
response = render(request, "check_zap.html", context)

Expand Down Expand Up @@ -573,5 +575,19 @@ def export_pdf(request, test):
return FileResponse(buffer, as_attachment=True, filename="hello.pdf")


def pdf_from_template(request, test):
return HttpResponse(request)
def pdf_from_template(request, test, site):
env = jinja2.Environment(loader=jinja2.PackageLoader('testing', 'templates'))
template = env.get_template('zap_pdf_wrapper.html')
report = TestReport.objects.get(tested_site=site, test_ran=test).report

css_path = os.path.join(settings.STATIC_DIR, 'css/style.css')

with open(css_path, 'r') as f:
css_content = f.read()

html_out = template.render(report, static_url=css_path, test=test, site=site)
pdf_file = weasyprint.HTML(string=html_out).write_pdf(stylesheets=[weasyprint.CSS(string=css_content)])

response = HttpResponse(pdf_file, content_type='application/pdf')
response['Content-Disposition'] = f'attachment; filename="{test}_{site}_report.pdf"'
return response

0 comments on commit 4613979

Please sign in to comment.