<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/

import urlparse
import stooge.curly as curly
import csp_validator.csp

# Utils

def final_response(http_responses, https_responses):
    if https_responses:
        return https_responses[-1]
    else:
        return http_responses[-1]

def is_http(response):
    return response["url"].startswith("http://")

def is_https(response):
    return response["url"].startswith("https://")

def get_result(results, category, check):
    for r in results.get(category,[]):
        if r["name"] == check:
            return r["result"]

# Basic checks

def ssl(site, results, http_responses, https_responses):
    r = final_response(http_responses, https_responses)
    return is_https(r)

def xfo(site, results, http_responses, https_responses):
    r = final_response(http_responses, https_responses)
    return "x-frame-options" in r["headers"]

def server(site, results, http_responses, https_responses):
    for r in http_responses:
        if "server" in r["headers"] and "/" in r["headers"]["server"]:
            return False
        if "x-powered-by" in r["headers"]:
            return False
    for r in https_responses:
        if "server" in r["headers"] and "/" in r["headers"]["server"]:
            return False
        if "x-powered-by" in r["headers"]:
            return False
    return True

def xxp(site, results, http_responses, https_responses):
    r = final_response(http_responses, https_responses)
    return "x-xss-protection" in r["headers"]

def xcto(site, results, http_responses, https_responses):
    r = final_response(http_responses, https_responses)
    return "x-content-type-options" in r["headers"]

# CSP Checks

def csp_present(site, results, http_responses, https_responses):
    r = final_response(http_responses, https_responses)
    return "content-security-policy" in r["headers"]

def csp_valid(site, results, http_responses, https_responses):
    if get_result(results, "csp", "csp_present"):
        r = final_response(http_responses, https_responses)
        parsed_csp = csp_validator.csp.validate(r["headers"]["content-security-policy"])
        return parsed_csp['valid']

def csp_reports(site, results, http_responses, https_responses):
    if get_result(results, "csp", "csp_valid"):
        r = final_response(http_responses, https_responses)
        policy = csp_validator.csp.parse_policy(r["headers"]["content-security-policy"])
        return 'report-uri' in policy

# SSL Checks

def ssl_present(site, results, http_responses, https_responses):
    return is_https(http_responses[-1]) or len(https_responses) != 0

def ssl_grade(site, results, http_responses, https_responses):
    if get_result(results, "ssl", "ssl_present") and site["ssllabs"]:
        return site["ssllabs"]["endpoints"][0]["grade"]

def ssl_pfs(site, results, http_responses, https_responses):
    if get_result(results, "ssl", "ssl_present") and site["ssllabs"]:
        return site["ssllabs"]["endpoints"][0]["results"]["details"]["forwardSecrecy"]

def ssl_heartbleed(site, results, http_responses, https_responses):
    if get_result(results, "ssl", "ssl_present") and site["ssllabs"]:
        return not site["ssllabs"]["endpoints"][0]["results"]["details"]["heartbleed"]

def sslredirect(site, results, http_responses, https_responses):
    if get_result(results, "ssl", "ssl_present"):
        first_response = http_responses[0]<commit_msg>Fixes #33 SSL grade of A+ is showing as A<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/

import urlparse
import stooge.curly as curly
import csp_validator.csp

# Utils

def final_response(http_responses, https_responses):
    if https_responses:
        return https_responses[-1]
    else:
        return http_responses[-1]

def is_http(response):
    return response["url"].startswith("http://")

def is_https(response):
    return response["url"].startswith("https://")

def get_result(results, category, check):
    for r in results.get(category,[]):
        if r["name"] == check:
            return r["result"]

# Basic checks

def ssl(site, results, http_responses, https_responses):
    r = final_response(http_responses, https_responses)
    return is_https(r)

def xfo(site, results, http_responses, https_responses):
    r = final_response(http_responses, https_responses)
    return "x-frame-options" in r["headers"]

def server(site, results, http_responses, https_responses):
    for r in http_responses:
        if "server" in r["headers"] and "/" in r["headers"]["server"]:
            return False
        if "x-powered-by" in r["headers"]:
            return False
    for r in https_responses:
        if "server" in r["headers"] and "/" in r["headers"]["server"]:
            return False
        if "x-powered-by" in r["headers"]:
            return False
    return True

def xxp(site, results, http_responses, https_responses):
    r = final_response(http_responses, https_responses)
    return "x-xss-protection" in r["headers"]

def xcto(site, results, http_responses, https_responses):
    r = final_response(http_responses, https_responses)
    return "x-content-type-options" in r["headers"]

# CSP Checks

def csp_present(site, results, http_responses, https_responses):
    r = final_response(http_responses, https_responses)
    return "content-security-policy" in r["headers"]

def csp_valid(site, results, http_responses, https_responses):
    if get_result(results, "csp", "csp_present"):
        r = final_response(http_responses, https_responses)
        parsed_csp = csp_validator.csp.validate(r["headers"]["content-security-policy"])
        return parsed_csp['valid']

def csp_reports(site, results, http_responses, https_responses):
    if get_result(results, "csp", "csp_valid"):
        r = final_response(http_responses, https_responses)
        policy = csp_validator.csp.parse_policy(r["headers"]["content-security-policy"])
        return 'report-uri' in policy

# SSL Checks

def ssl_present(site, results, http_responses, https_responses):
    return is_https(http_responses[-1]) or len(https_responses) != 0

def ssl_grade(site, results, http_responses, https_responses):
    if get_result(results, "ssl", "ssl_present") and site["ssllabs"]:
        return site["ssllabs"]["endpoints"][0]["gradePlus"]

def ssl_pfs(site, results, http_responses, https_responses):
    if get_result(results, "ssl", "ssl_present") and site["ssllabs"]:
        return site["ssllabs"]["endpoints"][0]["results"]["details"]["forwardSecrecy"]

def ssl_heartbleed(site, results, http_responses, https_responses):
    if get_result(results, "ssl", "ssl_present") and site["ssllabs"]:
        return not site["ssllabs"]["endpoints"][0]["results"]["details"]["heartbleed"]

def sslredirect(site, results, http_responses, https_responses):
    if get_result(results, "ssl", "ssl_present"):
        first_response = http_responses[0]
{'fraction_non_alphanumeric': 0.1279489314460172, 'fraction_numerical': 0.0036081043574798777, 'mean_word_length': 4.402548725637182, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6311113', 'n_tokens_mistral': 2328, 'n_tokens_neox': 2368, 'n_words': 566}