File: CSPViolationReportBody.cpp

package info (click to toggle)
webkit2gtk 2.42.2-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 362,452 kB
  • sloc: cpp: 2,881,971; javascript: 282,447; ansic: 134,088; python: 43,789; ruby: 18,308; perl: 15,872; asm: 14,389; xml: 4,395; yacc: 2,350; sh: 2,074; java: 1,734; lex: 1,323; makefile: 288; pascal: 60
file content (143 lines) | stat: -rw-r--r-- 6,936 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (C) 2022 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "CSPViolationReportBody.h"

#include "ContentSecurityPolicyClient.h"
#include "FormData.h"
#include "SecurityPolicyViolationEvent.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/JSONValues.h>
#include <wtf/NeverDestroyed.h>

namespace WebCore {

using Init = SecurityPolicyViolationEventInit;

WTF_MAKE_ISO_ALLOCATED_IMPL(CSPViolationReportBody);

CSPViolationReportBody::CSPViolationReportBody(Init&& init)
    : m_documentURL(WTFMove(init.documentURI))
    , m_referrer(init.referrer.isNull() ? emptyString() : WTFMove(init.referrer))
    , m_blockedURL(WTFMove(init.blockedURI))
    , m_effectiveDirective(WTFMove(init.effectiveDirective))
    , m_originalPolicy(WTFMove(init.originalPolicy))
    , m_sourceFile(WTFMove(init.sourceFile))
    , m_sample(WTFMove(init.sample))
    , m_disposition(init.disposition)
    , m_statusCode(init.statusCode)
    , m_lineNumber(init.lineNumber)
    , m_columnNumber(init.columnNumber)
{
}

CSPViolationReportBody::CSPViolationReportBody(String&& documentURL, String&& referrer, String&& blockedURL, String&& effectiveDirective, String&& originalPolicy, String&& sourceFile, String&& sample, SecurityPolicyViolationEventDisposition disposition, unsigned short statusCode, unsigned long lineNumber, unsigned long columnNumber)
    : m_documentURL(WTFMove(documentURL))
    , m_referrer(WTFMove(referrer))
    , m_blockedURL(WTFMove(blockedURL))
    , m_effectiveDirective(WTFMove(effectiveDirective))
    , m_originalPolicy(WTFMove(originalPolicy))
    , m_sourceFile(WTFMove(sourceFile))
    , m_sample(WTFMove(sample))
    , m_disposition(disposition)
    , m_statusCode(statusCode)
    , m_lineNumber(lineNumber)
    , m_columnNumber(columnNumber)
{
}

Ref<CSPViolationReportBody> CSPViolationReportBody::create(Init&& init)
{
    return adoptRef(*new CSPViolationReportBody(WTFMove(init)));
}

Ref<CSPViolationReportBody> CSPViolationReportBody::create(String&& documentURL, String&& referrer, String&& blockedURL, String&& effectiveDirective, String&& originalPolicy, String&& sourceFile, String&& sample, SecurityPolicyViolationEventDisposition disposition, unsigned short statusCode, unsigned long lineNumber, unsigned long columnNumber)
{
    return adoptRef(*new CSPViolationReportBody(WTFMove(documentURL), WTFMove(referrer), WTFMove(blockedURL), WTFMove(effectiveDirective), WTFMove(originalPolicy), WTFMove(sourceFile), WTFMove(sample), disposition, statusCode, lineNumber, columnNumber));
}

const String& CSPViolationReportBody::type() const
{
    static NeverDestroyed<const String> cspReportType(MAKE_STATIC_STRING_IMPL("csp-violation"));
    return cspReportType;
}

Ref<FormData> CSPViolationReportBody::createReportFormDataForViolation(bool usesReportTo, bool isReportOnly) const
{
    // We need to be careful here when deciding what information to send to the
    // report-uri. Currently, we send only the current document's URL and the
    // directive that was violated. The document's URL is safe to send because
    // it's the document itself that's requesting that it be sent. You could
    // make an argument that we shouldn't send HTTPS document URLs to HTTP
    // report-uris (for the same reasons that we suppress the Referer in that
    // case), but the Referrer is sent implicitly whereas this request is only
    // sent explicitly. As for which directive was violated, that's pretty
    // harmless information.

    auto cspReport = JSON::Object::create();

    if (usesReportTo) {
        // It looks like WPT expect the body for modern reports to use the same
        // syntax as the JSON object (not the hyphenated versions in the original
        // CSP spec.
        cspReport->setString("documentURL"_s, documentURL());
        cspReport->setString("disposition"_s, isReportOnly ? "report"_s : "enforce"_s);
        cspReport->setString("referrer"_s, referrer());
        cspReport->setString("effectiveDirective"_s, effectiveDirective());
        cspReport->setString("blockedURL"_s, blockedURL());
        cspReport->setString("originalPolicy"_s, originalPolicy());
        cspReport->setInteger("statusCode"_s, statusCode());
        cspReport->setString("sample"_s, sample());
        if (!sourceFile().isNull()) {
            cspReport->setString("sourceFile"_s, sourceFile());
            cspReport->setInteger("lineNumber"_s, lineNumber());
            cspReport->setInteger("columnNumber"_s, columnNumber());
        }
    } else {
        cspReport->setString("document-uri"_s, documentURL());
        cspReport->setString("referrer"_s, referrer());
        cspReport->setString("violated-directive"_s, effectiveDirective());
        cspReport->setString("effective-directive"_s, effectiveDirective());
        cspReport->setString("original-policy"_s, originalPolicy());
        cspReport->setString("blocked-uri"_s, blockedURL());
        cspReport->setInteger("status-code"_s, statusCode());
        if (!sourceFile().isNull()) {
            cspReport->setString("source-file"_s, sourceFile());
            cspReport->setInteger("line-number"_s, lineNumber());
            cspReport->setInteger("column-number"_s, columnNumber());
        }
    }

    // https://www.w3.org/TR/reporting-1/#queue-report, step 2.3.1.
    auto reportObject = JSON::Object::create();
    reportObject->setString("type"_s, type());
    reportObject->setString("url"_s, documentURL());
    reportObject->setObject(usesReportTo ? "body"_s : "csp-report"_s, WTFMove(cspReport));

    return FormData::create(reportObject->toJSONString().utf8());
}

} // namespace WebCore