|
| 1 | +import logging |
| 2 | + |
| 3 | +import bleach |
| 4 | +import markdown |
| 5 | +import requests |
| 6 | +from django.core.cache import cache |
| 7 | + |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | +BUCKET_URL = "https://storage.googleapis.com/defectdojo-os-messages-prod/open_source_message.md" |
| 11 | +CACHE_SECONDS = 3600 |
| 12 | +HTTP_TIMEOUT_SECONDS = 2 |
| 13 | +CACHE_KEY = "os_message:v1" |
| 14 | + |
| 15 | +INLINE_TAGS = ["strong", "em", "a"] |
| 16 | +INLINE_ATTRS = {"a": ["href", "title"]} |
| 17 | + |
| 18 | +# Keep BLOCK_TAGS / BLOCK_ATTRS in sync with the DaaS publisher's |
| 19 | +# MARKDOWNIFY["default"]["WHITELIST_TAGS"] / WHITELIST_ATTRS so previews |
| 20 | +# on DaaS and rendering in OSS stay byte-identical. |
| 21 | +BLOCK_TAGS = [ |
| 22 | + "p", "ul", "ol", "li", "a", "strong", "em", "code", "pre", |
| 23 | + "blockquote", "h2", "h3", "h4", "hr", "br", "b", "i", |
| 24 | + "abbr", "acronym", |
| 25 | +] |
| 26 | +BLOCK_ATTRS = { |
| 27 | + "a": ["href", "title"], |
| 28 | + "abbr": ["title"], |
| 29 | + "acronym": ["title"], |
| 30 | +} |
| 31 | + |
| 32 | +_MISS = object() |
| 33 | + |
| 34 | + |
| 35 | +def fetch_os_message(): |
| 36 | + cached = cache.get(CACHE_KEY, default=_MISS) |
| 37 | + if cached is not _MISS: |
| 38 | + return cached |
| 39 | + |
| 40 | + try: |
| 41 | + response = requests.get(BUCKET_URL, timeout=HTTP_TIMEOUT_SECONDS) |
| 42 | + except Exception: |
| 43 | + logger.debug("os_message: fetch failed", exc_info=True) |
| 44 | + cache.set(CACHE_KEY, None, CACHE_SECONDS) |
| 45 | + return None |
| 46 | + |
| 47 | + if response.status_code != 200 or not response.text.strip(): |
| 48 | + cache.set(CACHE_KEY, None, CACHE_SECONDS) |
| 49 | + return None |
| 50 | + |
| 51 | + cache.set(CACHE_KEY, response.text, CACHE_SECONDS) |
| 52 | + return response.text |
| 53 | + |
| 54 | + |
| 55 | +def _strip_outer_p(html): |
| 56 | + stripped = html.strip() |
| 57 | + if stripped.startswith("<p>") and stripped.endswith("</p>"): |
| 58 | + return stripped[3:-4] |
| 59 | + return stripped |
| 60 | + |
| 61 | + |
| 62 | +def parse_os_message(text): |
| 63 | + lines = text.splitlines() |
| 64 | + |
| 65 | + headline_source = None |
| 66 | + body_start = None |
| 67 | + for index, line in enumerate(lines): |
| 68 | + if line.startswith("# "): |
| 69 | + headline_source = line[2:].strip() |
| 70 | + body_start = index + 1 |
| 71 | + break |
| 72 | + |
| 73 | + if not headline_source: |
| 74 | + return None |
| 75 | + |
| 76 | + headline_source = headline_source[:100] |
| 77 | + headline_rendered = markdown.markdown(headline_source) |
| 78 | + headline_cleaned = bleach.clean( |
| 79 | + headline_rendered, |
| 80 | + tags=INLINE_TAGS, |
| 81 | + attributes=INLINE_ATTRS, |
| 82 | + strip=True, |
| 83 | + ) |
| 84 | + headline_html = _strip_outer_p(headline_cleaned) |
| 85 | + |
| 86 | + expanded_html = None |
| 87 | + expanded_marker = "## Expanded Message" |
| 88 | + expanded_body_lines = None |
| 89 | + for offset, line in enumerate(lines[body_start:], start=body_start): |
| 90 | + if line.strip() == expanded_marker: |
| 91 | + expanded_body_lines = lines[offset + 1:] |
| 92 | + break |
| 93 | + |
| 94 | + if expanded_body_lines is not None: |
| 95 | + expanded_source = "\n".join(expanded_body_lines).strip() |
| 96 | + if expanded_source: |
| 97 | + expanded_rendered = markdown.markdown( |
| 98 | + expanded_source, |
| 99 | + extensions=["extra", "fenced_code", "nl2br"], |
| 100 | + ) |
| 101 | + expanded_html = bleach.clean( |
| 102 | + expanded_rendered, |
| 103 | + tags=BLOCK_TAGS, |
| 104 | + attributes=BLOCK_ATTRS, |
| 105 | + strip=True, |
| 106 | + ) |
| 107 | + |
| 108 | + return {"message": headline_html, "expanded_html": expanded_html} |
| 109 | + |
| 110 | + |
| 111 | +def get_os_banner(): |
| 112 | + try: |
| 113 | + text = fetch_os_message() |
| 114 | + if not text: |
| 115 | + return None |
| 116 | + return parse_os_message(text) |
| 117 | + except Exception: |
| 118 | + logger.debug("os_message: get_os_banner failed", exc_info=True) |
| 119 | + return None |
0 commit comments