Example #1
0
 def testReportParser_parse_selfURIFailStrict(self):
     """Tests that parsing fails if strict and no document-uri but another 'self' URI."""
     report = """{"blocked-uri": "self", "other": "value"}"""
     parser = ReportParser(requiredKeys=[],
                           strict=True,
                           uriKeys=["blocked-uri", "document-uri"])
     cspReport = parser.parseString(report)
     assert cspReport == Report.INVALID()
Example #2
0
 def testReportParser_parse_requiredFields(self):
     """Required fields must be present even if strict=False."""
     report = """{"this-is": "a quite empty report"}"""
     expected = Report({"this-is": "a quite empty report"})
     assert ReportParser(requiredKeys=[],
                         strict=False).parseString(report) == expected
     assert ReportParser(
         requiredKeys=["does-not-exist"],
         strict=False).parseString(report) == Report.INVALID()
Example #3
0
    def testReport_eq(self):
        report1a = Report({
            "violated-directive": ReportTest.sampleDirective2a,
            "original-policy": ReportTest.samplePolicy1a
        })
        report1b = Report({
            "violated-directive": ReportTest.sampleDirective2b,
            "original-policy": ReportTest.samplePolicy1b
        })
        report2 = Report({
            "violated-directive": ReportTest.sampleDirective1a,
            "original-policy": ReportTest.samplePolicy1a
        })

        assert report1a == report1b
        assert hash(report1a) == hash(report1b)
        assert report1a != report2
        assert report1a != Report.INVALID()
        assert report2 != Report.INVALID()
Example #4
0
    def parseJsonDict(self, jsonLogEntry):
        """
        Parses the given 'jsonLogEntry' according to the parameters set in the constructor of this LogEntryParser 
        and returns a LogEntry object. 'jsonLogEntry' is expected to be a Python dict object. If 'jsonLogEntry' 
        cannot be parsed because it is syntactically invalid (or empty), LogEntry.INVALID() will be returned.
        """

        # TODO: could also parse the timestamp string etc.
        if "csp-report" in jsonLogEntry:
            jsonLogEntry["csp-report"] = self._reportParser.parseJsonDict(
                jsonLogEntry["csp-report"])
            if self._strict and jsonLogEntry["csp-report"] == Report.INVALID():
                return LogEntry.INVALID()
            else:
                return LogEntry(jsonLogEntry)
        else:
            return LogEntry.INVALID()
Example #5
0
 def testReport_str_invalid(self):
     assert str(Report.INVALID()) == "[invalid]"
Example #6
0
 def testReportParser_parse_failIfStrict(self):
     """The report must be declared invalid in strict mode when a child element is invalid."""
     report = """{"invalid-policy": "awesomeness-src 'self'", "example": true}"""
     assert ReportParser(requiredKeys=[], strict=True, policyKeys=["invalid-policy"]) \
                     .parseString(report) == Report.INVALID()
Example #7
0
 def testReport_generatePolicy_invalid(self):
     assert Report.INVALID().generatePolicy("regular") == Policy.INVALID()