1 | /* |
2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "Decoder.h" |
29 | #include "Encoder.h" |
30 | #include "SandboxExtension.h" |
31 | #include <WebCore/FormData.h> |
32 | |
33 | namespace IPC { |
34 | |
35 | class FormDataReference { |
36 | public: |
37 | FormDataReference() = default; |
38 | explicit FormDataReference(RefPtr<WebCore::FormData>&& data) |
39 | : m_data(WTFMove(data)) |
40 | { |
41 | } |
42 | |
43 | RefPtr<WebCore::FormData> takeData() { return WTFMove(m_data); } |
44 | |
45 | void encode(Encoder& encoder) const |
46 | { |
47 | encoder << !!m_data; |
48 | if (!m_data) |
49 | return; |
50 | |
51 | encoder << *m_data; |
52 | |
53 | auto& elements = m_data->elements(); |
54 | size_t fileCount = std::count_if(elements.begin(), elements.end(), [](auto& element) { |
55 | return WTF::holds_alternative<WebCore::FormDataElement::EncodedFileData>(element.data); |
56 | }); |
57 | |
58 | WebKit::SandboxExtension::HandleArray sandboxExtensionHandles; |
59 | sandboxExtensionHandles.allocate(fileCount); |
60 | size_t extensionIndex = 0; |
61 | for (auto& element : elements) { |
62 | if (auto* fileData = WTF::get_if<WebCore::FormDataElement::EncodedFileData>(element.data)) { |
63 | const String& path = fileData->shouldGenerateFile ? fileData->generatedFilename : fileData->filename; |
64 | WebKit::SandboxExtension::createHandle(path, WebKit::SandboxExtension::Type::ReadOnly, sandboxExtensionHandles[extensionIndex++]); |
65 | } |
66 | } |
67 | encoder << sandboxExtensionHandles; |
68 | } |
69 | |
70 | static Optional<FormDataReference> decode(Decoder& decoder) |
71 | { |
72 | Optional<bool> hasFormData; |
73 | decoder >> hasFormData; |
74 | if (!hasFormData) |
75 | return WTF::nullopt; |
76 | if (!hasFormData.value()) |
77 | return FormDataReference { }; |
78 | |
79 | auto formData = WebCore::FormData::decode(decoder); |
80 | if (!formData) |
81 | return WTF::nullopt; |
82 | |
83 | Optional<WebKit::SandboxExtension::HandleArray> sandboxExtensionHandles; |
84 | decoder >> sandboxExtensionHandles; |
85 | if (!sandboxExtensionHandles) |
86 | return WTF::nullopt; |
87 | |
88 | for (size_t i = 0; i < sandboxExtensionHandles->size(); ++i) |
89 | WebKit::SandboxExtension::consumePermanently(sandboxExtensionHandles->at(i)); |
90 | |
91 | return FormDataReference { formData.releaseNonNull() }; |
92 | } |
93 | |
94 | private: |
95 | RefPtr<WebCore::FormData> m_data; |
96 | }; |
97 | |
98 | } // namespace IPC |
99 | |