Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: third_party/WebKit/Source/modules/websockets/DocumentWebSocketChannel.cpp

Issue 2714573002: Enable websocket filtering via SubresourceFilter (Closed)
Patch Set: sync to #455176 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/modules/websockets/DocumentWebSocketChannel.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 15 matching lines...) Expand all
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "modules/websockets/DocumentWebSocketChannel.h" 31 #include "modules/websockets/DocumentWebSocketChannel.h"
32 32
33 #include <memory> 33 #include <memory>
34 #include "core/dom/DOMArrayBuffer.h" 34 #include "core/dom/DOMArrayBuffer.h"
35 #include "core/dom/ExecutionContext.h" 35 #include "core/dom/ExecutionContext.h"
36 #include "core/dom/TaskRunnerHelper.h"
36 #include "core/fileapi/FileReaderLoader.h" 37 #include "core/fileapi/FileReaderLoader.h"
37 #include "core/fileapi/FileReaderLoaderClient.h" 38 #include "core/fileapi/FileReaderLoaderClient.h"
38 #include "core/frame/LocalFrame.h" 39 #include "core/frame/LocalFrame.h"
39 #include "core/frame/LocalFrameClient.h" 40 #include "core/frame/LocalFrameClient.h"
40 #include "core/inspector/ConsoleMessage.h" 41 #include "core/inspector/ConsoleMessage.h"
41 #include "core/inspector/InspectorInstrumentation.h" 42 #include "core/inspector/InspectorInstrumentation.h"
43 #include "core/loader/DocumentLoader.h"
42 #include "core/loader/FrameLoader.h" 44 #include "core/loader/FrameLoader.h"
43 #include "core/loader/MixedContentChecker.h" 45 #include "core/loader/MixedContentChecker.h"
46 #include "core/loader/SubresourceFilter.h"
44 #include "core/loader/ThreadableLoadingContext.h" 47 #include "core/loader/ThreadableLoadingContext.h"
45 #include "modules/websockets/InspectorWebSocketEvents.h" 48 #include "modules/websockets/InspectorWebSocketEvents.h"
46 #include "modules/websockets/WebSocketChannelClient.h" 49 #include "modules/websockets/WebSocketChannelClient.h"
47 #include "modules/websockets/WebSocketFrame.h" 50 #include "modules/websockets/WebSocketFrame.h"
48 #include "modules/websockets/WebSocketHandleImpl.h" 51 #include "modules/websockets/WebSocketHandleImpl.h"
49 #include "platform/WebFrameScheduler.h" 52 #include "platform/WebFrameScheduler.h"
53 #include "platform/WebTaskRunner.h"
50 #include "platform/loader/fetch/UniqueIdentifier.h" 54 #include "platform/loader/fetch/UniqueIdentifier.h"
51 #include "platform/network/NetworkLog.h" 55 #include "platform/network/NetworkLog.h"
52 #include "platform/network/WebSocketHandshakeRequest.h" 56 #include "platform/network/WebSocketHandshakeRequest.h"
53 #include "platform/weborigin/SecurityOrigin.h" 57 #include "platform/weborigin/SecurityOrigin.h"
54 #include "public/platform/InterfaceProvider.h" 58 #include "public/platform/InterfaceProvider.h"
55 #include "public/platform/Platform.h" 59 #include "public/platform/Platform.h"
60 #include "public/platform/WebTraceLocation.h"
61 #include "wtf/Functional.h"
56 #include "wtf/PtrUtil.h" 62 #include "wtf/PtrUtil.h"
57 63
58 namespace blink { 64 namespace blink {
59 65
60 class DocumentWebSocketChannel::BlobLoader final 66 class DocumentWebSocketChannel::BlobLoader final
61 : public GarbageCollectedFinalized<DocumentWebSocketChannel::BlobLoader>, 67 : public GarbageCollectedFinalized<DocumentWebSocketChannel::BlobLoader>,
62 public FileReaderLoaderClient { 68 public FileReaderLoaderClient {
63 public: 69 public:
64 BlobLoader(PassRefPtr<BlobDataHandle>, DocumentWebSocketChannel*); 70 BlobLoader(PassRefPtr<BlobDataHandle>, DocumentWebSocketChannel*);
65 ~BlobLoader() override {} 71 ~BlobLoader() override {}
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 m_url = url; 183 m_url = url;
178 Vector<String> protocols; 184 Vector<String> protocols;
179 // Avoid placing an empty token in the Vector when the protocol string is 185 // Avoid placing an empty token in the Vector when the protocol string is
180 // empty. 186 // empty.
181 if (!protocol.isEmpty()) { 187 if (!protocol.isEmpty()) {
182 // Since protocol is already verified and escaped, we can simply split 188 // Since protocol is already verified and escaped, we can simply split
183 // it. 189 // it.
184 protocol.split(", ", true, protocols); 190 protocol.split(", ", true, protocols);
185 } 191 }
186 192
193 // If the connection needs to be filtered, asynchronously fail. Synchronous
194 // failure blocks the worker thread which should be avoided. Note that
195 // returning "true" just indicates that this was not a mixed content error.
196 if (shouldDisallowConnection(url)) {
197 TaskRunnerHelper::get(TaskType::Networking, document())
198 ->postTask(
199 BLINK_FROM_HERE,
200 WTF::bind(&DocumentWebSocketChannel::tearDownFailedConnection,
201 wrapPersistent(this)));
202 return true;
203 }
204
187 // TODO(kinuko): document() should return nullptr if we don't 205 // TODO(kinuko): document() should return nullptr if we don't
188 // have valid document/frame that returns non-empty interface provider. 206 // have valid document/frame that returns non-empty interface provider.
189 if (document() && document()->frame() && 207 if (document() && document()->frame() &&
190 document()->frame()->interfaceProvider() != 208 document()->frame()->interfaceProvider() !=
191 InterfaceProvider::getEmptyInterfaceProvider()) { 209 InterfaceProvider::getEmptyInterfaceProvider()) {
192 // Initialize the WebSocketHandle with the frame's InterfaceProvider to 210 // Initialize the WebSocketHandle with the frame's InterfaceProvider to
193 // provide the WebSocket implementation with context about this frame. 211 // provide the WebSocket implementation with context about this frame.
194 // This is important so that the browser can show UI associated with 212 // This is important so that the browser can show UI associated with
195 // the WebSocket (e.g., for certificate errors). 213 // the WebSocket (e.g., for certificate errors).
196 m_handle->initialize(document()->frame()->interfaceProvider()); 214 m_handle->initialize(document()->frame()->interfaceProvider());
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 unsigned short codeToSend = static_cast<unsigned short>( 307 unsigned short codeToSend = static_cast<unsigned short>(
290 code == CloseEventCodeNotSpecified ? CloseEventCodeNoStatusRcvd : code); 308 code == CloseEventCodeNotSpecified ? CloseEventCodeNoStatusRcvd : code);
291 m_messages.append(new Message(codeToSend, reason)); 309 m_messages.append(new Message(codeToSend, reason));
292 processSendQueue(); 310 processSendQueue();
293 } 311 }
294 312
295 void DocumentWebSocketChannel::fail(const String& reason, 313 void DocumentWebSocketChannel::fail(const String& reason,
296 MessageLevel level, 314 MessageLevel level,
297 std::unique_ptr<SourceLocation> location) { 315 std::unique_ptr<SourceLocation> location) {
298 NETWORK_DVLOG(1) << this << " fail(" << reason << ")"; 316 NETWORK_DVLOG(1) << this << " fail(" << reason << ")";
299 // m_handle and m_client can be null here.
300
301 connection_handle_for_scheduler_.reset();
302
303 if (document()) { 317 if (document()) {
304 probe::didReceiveWebSocketFrameError(document(), m_identifier, reason); 318 probe::didReceiveWebSocketFrameError(document(), m_identifier, reason);
305 const String message = "WebSocket connection to '" + m_url.elidedString() + 319 const String message = "WebSocket connection to '" + m_url.elidedString() +
306 "' failed: " + reason; 320 "' failed: " + reason;
307 document()->addConsoleMessage(ConsoleMessage::create( 321 document()->addConsoleMessage(ConsoleMessage::create(
308 JSMessageSource, level, message, std::move(location))); 322 JSMessageSource, level, message, std::move(location)));
309 } 323 }
310
311 if (m_client)
312 m_client->didError();
313 // |reason| is only for logging and should not be provided for scripts, 324 // |reason| is only for logging and should not be provided for scripts,
314 // hence close reason must be empty. 325 // hence close reason must be empty in tearDownFailedConnection.
315 handleDidClose(false, CloseEventCodeAbnormalClosure, String()); 326 tearDownFailedConnection();
316 // handleDidClose may delete this object.
317 } 327 }
318 328
319 void DocumentWebSocketChannel::disconnect() { 329 void DocumentWebSocketChannel::disconnect() {
320 NETWORK_DVLOG(1) << this << " disconnect()"; 330 NETWORK_DVLOG(1) << this << " disconnect()";
321 if (m_identifier) { 331 if (m_identifier) {
322 TRACE_EVENT_INSTANT1( 332 TRACE_EVENT_INSTANT1(
323 "devtools.timeline", "WebSocketDestroy", TRACE_EVENT_SCOPE_THREAD, 333 "devtools.timeline", "WebSocketDestroy", TRACE_EVENT_SCOPE_THREAD,
324 "data", InspectorWebSocketEvent::data(document(), m_identifier)); 334 "data", InspectorWebSocketEvent::data(document(), m_identifier));
325 probe::didCloseWebSocket(document(), m_identifier); 335 probe::didCloseWebSocket(document(), m_identifier);
326 } 336 }
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 m_blobLoader.clear(); 682 m_blobLoader.clear();
673 if (errorCode == FileError::kAbortErr) { 683 if (errorCode == FileError::kAbortErr) {
674 // The error is caused by cancel(). 684 // The error is caused by cancel().
675 return; 685 return;
676 } 686 }
677 // FIXME: Generate human-friendly reason message. 687 // FIXME: Generate human-friendly reason message.
678 failAsError("Failed to load Blob: error code = " + String::number(errorCode)); 688 failAsError("Failed to load Blob: error code = " + String::number(errorCode));
679 // |this| can be deleted here. 689 // |this| can be deleted here.
680 } 690 }
681 691
692 void DocumentWebSocketChannel::tearDownFailedConnection() {
693 // m_handle and m_client can be null here.
694 connection_handle_for_scheduler_.reset();
695
696 if (m_client)
697 m_client->didError();
698
699 handleDidClose(false, CloseEventCodeAbnormalClosure, String());
700 // handleDidClose may delete this object.
701 }
702
703 bool DocumentWebSocketChannel::shouldDisallowConnection(const KURL& url) {
704 DCHECK(m_handle);
705 DocumentLoader* loader = document()->loader();
706 if (!loader)
707 return false;
708 SubresourceFilter* subresourceFilter = loader->subresourceFilter();
709 if (!subresourceFilter)
710 return false;
711 return !subresourceFilter->allowWebSocketConnection(url);
712 }
713
682 DEFINE_TRACE(DocumentWebSocketChannel) { 714 DEFINE_TRACE(DocumentWebSocketChannel) {
683 visitor->trace(m_blobLoader); 715 visitor->trace(m_blobLoader);
684 visitor->trace(m_messages); 716 visitor->trace(m_messages);
685 visitor->trace(m_client); 717 visitor->trace(m_client);
686 visitor->trace(m_loadingContext); 718 visitor->trace(m_loadingContext);
687 WebSocketChannel::trace(visitor); 719 WebSocketChannel::trace(visitor);
688 } 720 }
689 721
690 std::ostream& operator<<(std::ostream& ostream, 722 std::ostream& operator<<(std::ostream& ostream,
691 const DocumentWebSocketChannel* channel) { 723 const DocumentWebSocketChannel* channel) {
692 return ostream << "DocumentWebSocketChannel " 724 return ostream << "DocumentWebSocketChannel "
693 << static_cast<const void*>(channel); 725 << static_cast<const void*>(channel);
694 } 726 }
695 727
696 } // namespace blink 728 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/websockets/DocumentWebSocketChannel.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698