eric6/WebBrowser/SafeBrowsing/SafeBrowsingAPIClient.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7923
91e843545d9a
child 8143
2c730d5fd177
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
3 # Copyright (c) 2017 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the low level interface for Google Safe Browsing.
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import json
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
11 import base64
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
7269
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
13 from PyQt5.QtCore import (
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
14 pyqtSignal, QObject, QDateTime, QUrl, QByteArray, QCoreApplication,
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
15 QEventLoop
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
16 )
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 from WebBrowser.WebBrowserWindow import WebBrowserWindow
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
21 from .SafeBrowsingThreatList import ThreatList
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
22
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 class SafeBrowsingAPIClient(QObject):
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 Class implementing the low level interface for Google Safe Browsing.
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
27
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
28 @signal networkError(str) emitted to indicate a network error
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 """
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 ClientId = "eric6_API_client"
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
31 ClientVersion = "2.0.0"
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 GsbUrlTemplate = "https://safebrowsing.googleapis.com/v4/{0}?key={1}"
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 networkError = pyqtSignal(str)
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 def __init__(self, apiKey, fairUse=True, parent=None):
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 """
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 Constructor
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 @param apiKey API key to be used
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 @type str
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 @param fairUse flag indicating to follow the fair use policy
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 @type bool
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 @param parent reference to the parent object
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 @type QObject
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 """
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
48 super(SafeBrowsingAPIClient, self).__init__(parent)
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
49
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 self.__apiKey = apiKey
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 self.__fairUse = fairUse
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 self.__nextRequestNoSoonerThan = QDateTime()
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 self.__failCount = 0
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
55
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
56 self.__lookupApiCache = {}
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
57 # Temporary cache used by the lookup API (v4)
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
58 # key: URL as string
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
59 # value: dictionary with these entries:
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
60 # "validUntil": (QDateTime)
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
61 # "threatInfo": (list of ThreatList)
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
63 def setApiKey(self, apiKey):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
64 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
65 Public method to set the API key.
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
66
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
67 @param apiKey API key to be set
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
68 @type str
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
69 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
70 self.__apiKey = apiKey
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
71
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 def getThreatLists(self):
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 """
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 Public method to retrieve all available threat lists.
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
75
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
76 @return tuple containing list of threat lists and an error message
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
77 @rtype tuple of (list of dict containing 'threatType', 'platformType'
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
78 and 'threatEntryType', bool)
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 """
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 url = QUrl(self.GsbUrlTemplate.format("threatLists", self.__apiKey))
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 req = QNetworkRequest(url)
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 reply = WebBrowserWindow.networkManager().get(req)
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
83
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
84 while reply.isRunning():
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
85 QCoreApplication.processEvents(QEventLoop.AllEvents, 200)
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
86 # max. 200 ms processing
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
87
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
88 res = None
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
89 error = ""
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
90 if reply.error() != QNetworkReply.NoError:
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
91 error = reply.errorString()
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
92 self.networkError.emit(error)
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
93 else:
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
94 result = self.__extractData(reply)
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
95 res = result["threatLists"]
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
96
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
97 reply.deleteLater()
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
98 return res, error
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99
6230
77a1b22c8a1f Web Browser NG, Safe Browsing: identified places needing a change to support the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
100 #######################################################################
77a1b22c8a1f Web Browser NG, Safe Browsing: identified places needing a change to support the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
101 ## Methods below implement the 'Update API (v4)'
77a1b22c8a1f Web Browser NG, Safe Browsing: identified places needing a change to support the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
102 #######################################################################
77a1b22c8a1f Web Browser NG, Safe Browsing: identified places needing a change to support the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
103
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
104 def getThreatsUpdate(self, clientStates):
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
105 """
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
106 Public method to fetch hash prefix updates for the given threat list.
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
107
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
108 @param clientStates dictionary of client states with keys like
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
109 (threatType, platformType, threatEntryType)
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
110 @type dict
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
111 @return tuple containing the list of threat updates and an error
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
112 message
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
113 @rtype tuple of (list of dict, bool)
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
114 """
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
115 requestBody = {
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
116 "client": {
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
117 "clientId": self.ClientId,
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
118 "clientVersion": self.ClientVersion,
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
119 },
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
120 "listUpdateRequests": [],
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
121 }
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
122
7269
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
123 for (threatType, platformType, threatEntryType), currentState in (
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
124 clientStates.items()
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
125 ):
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
126 requestBody["listUpdateRequests"].append(
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
127 {
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
128 "threatType": threatType,
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
129 "platformType": platformType,
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
130 "threatEntryType": threatEntryType,
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
131 "state": currentState,
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
132 "constraints": {
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
133 "supportedCompressions": ["RAW"],
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
134 }
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
135 }
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
136 )
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
137
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
138 data = QByteArray(json.dumps(requestBody).encode("utf-8"))
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
139 url = QUrl(self.GsbUrlTemplate.format("threatListUpdates:fetch",
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
140 self.__apiKey))
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
141 req = QNetworkRequest(url)
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
142 req.setHeader(QNetworkRequest.ContentTypeHeader, "application/json")
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
143 reply = WebBrowserWindow.networkManager().post(req, data)
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
144
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
145 while reply.isRunning():
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
146 QCoreApplication.processEvents(QEventLoop.AllEvents, 200)
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
147 # max. 200 ms processing
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
148
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
149 res = None
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
150 error = ""
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
151 if reply.error() != QNetworkReply.NoError:
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
152 error = reply.errorString()
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
153 self.networkError.emit(error)
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
154 else:
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
155 result = self.__extractData(reply)
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
156 res = result["listUpdateResponses"]
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
157
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
158 reply.deleteLater()
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
159 return res, error
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
160
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
161 def getFullHashes(self, prefixes, clientState):
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
162 """
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
163 Public method to find full hashes matching hash prefixes.
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
164
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
165 @param prefixes list of hash prefixes to find
7639
422fd05e9c91 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
166 @type list of bytes
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
167 @param clientState dictionary of client states with keys like
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
168 (threatType, platformType, threatEntryType)
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
169 @type dict
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
170 @return dictionary containing the list of found hashes and the
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
171 negative cache duration
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
172 @rtype dict
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
173 """
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
174 requestBody = {
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
175 "client": {
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
176 "clientId": self.ClientId,
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
177 "clientVersion": self.ClientVersion,
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
178 },
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
179 "clientStates": [],
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
180 "threatInfo": {
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
181 "threatTypes": [],
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
182 "platformTypes": [],
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
183 "threatEntryTypes": [],
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
184 "threatEntries": [],
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
185 },
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
186 }
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
188 for prefix in prefixes:
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
189 requestBody["threatInfo"]["threatEntries"].append(
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
190 {"hash": base64.b64encode(prefix).decode("ascii")})
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
191
7269
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
192 for (threatType, platformType, threatEntryType), currentState in (
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
193 clientState.items()
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
194 ):
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
195 requestBody["clientStates"].append(currentState)
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
196 if threatType not in requestBody["threatInfo"]["threatTypes"]:
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
197 requestBody["threatInfo"]["threatTypes"].append(threatType)
7269
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
198 if (
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
199 platformType not in
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
200 requestBody["threatInfo"]["platformTypes"]
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
201 ):
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
202 requestBody["threatInfo"]["platformTypes"].append(
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
203 platformType)
7269
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
204 if (
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
205 threatEntryType not in
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
206 requestBody["threatInfo"]["threatEntryTypes"]
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
207 ):
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
208 requestBody["threatInfo"]["threatEntryTypes"].append(
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
209 threatEntryType)
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
210
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
211 data = QByteArray(json.dumps(requestBody).encode("utf-8"))
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
212 url = QUrl(self.GsbUrlTemplate.format("fullHashes:find",
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
213 self.__apiKey))
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
214 req = QNetworkRequest(url)
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
215 req.setHeader(QNetworkRequest.ContentTypeHeader, "application/json")
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
216 reply = WebBrowserWindow.networkManager().post(req, data)
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
217
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
218 while reply.isRunning():
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
219 QCoreApplication.processEvents(QEventLoop.AllEvents, 200)
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
220 # max. 200 ms processing
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
221
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
222 res = []
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
223 if reply.error() != QNetworkReply.NoError:
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
224 self.networkError.emit(reply.errorString())
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
225 else:
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
226 res = self.__extractData(reply)
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
227
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
228 reply.deleteLater()
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
229 return res
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 def __extractData(self, reply):
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 """
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 Private method to extract the data of a network reply.
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 @param reply reference to the network reply object
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 @type QNetworkReply
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
237 @return extracted data
5832
28f36b9c925f Updated source docu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
238 @rtype list or dict
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 """
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 result = json.loads(str(reply.readAll(), "utf-8"))
5811
5358a3c7995f Done implementing the SafeBrowsingAPIClient class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5809
diff changeset
241 self.__setWaitDuration(result.get("minimumWaitDuration"))
5816
93c74269d59e Changed the SafeBrowsingAPIClient class to work synchroneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5811
diff changeset
242 return result
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 def __setWaitDuration(self, minimumWaitDuration):
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 """
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 Private method to set the minimum wait duration.
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 @param minimumWaitDuration duration to be set
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 @type str
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 """
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 if not self.__fairUse or minimumWaitDuration is None:
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 self.__nextRequestNoSoonerThan = QDateTime()
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 else:
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
254 waitDuration = int(float(minimumWaitDuration.rstrip("s")))
7269
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
255 self.__nextRequestNoSoonerThan = (
5809
5b53c17b7d93 Done implementing the SafeBrowsingUrl class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 QDateTime.currentDateTime().addSecs(waitDuration)
7269
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
257 )
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
258
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
259 def fairUseDelayExpired(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
260 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
261 Public method to check, if the fair use wait period has expired.
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
262
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
263 @return flag indicating expiration
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
264 @rtype bool
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
265 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
266 return (
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
267 self.__fairUse and
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
268 QDateTime.currentDateTime() >= self.__nextRequestNoSoonerThan
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5816
diff changeset
269 ) or not self.__fairUse
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
270
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
271 def getFairUseDelayExpirationDateTime(self):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
272 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
273 Public method to get the date and time the fair use delay will expire.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
274
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
275 @return fair use delay expiration date and time
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
276 @rtype QDateTime
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
277 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
278 return self.__nextRequestNoSoonerThan
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
279
6230
77a1b22c8a1f Web Browser NG, Safe Browsing: identified places needing a change to support the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
280 #######################################################################
77a1b22c8a1f Web Browser NG, Safe Browsing: identified places needing a change to support the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
281 ## Methods below implement the 'Lookup API (v4)'
77a1b22c8a1f Web Browser NG, Safe Browsing: identified places needing a change to support the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
282 #######################################################################
77a1b22c8a1f Web Browser NG, Safe Browsing: identified places needing a change to support the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
283
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
284 def lookupUrl(self, url, platforms):
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
285 """
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
286 Public method to send an URL to Google for checking.
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
287
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
288 @param url URL to be checked
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
289 @type QUrl
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
290 @param platforms list of platform types to check against
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
291 @type list of str
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
292 @return tuple containing the list of threat list info objects and
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
293 an error message
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
294 @rtype tuple of (list of ThreatList, str)
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
295 """
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
296 error = ""
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
297
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
298 # sanitize the URL by removing user info and query data
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
299 url = url.adjusted(
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
300 QUrl.RemoveUserInfo | QUrl.RemoveQuery | QUrl.RemoveFragment
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
301 )
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
302 urlStr = url.toString()
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
303
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
304 # check the local cache first
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
305 if urlStr in self.__lookupApiCache:
7269
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
306 if (
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
307 self.__lookupApiCache[urlStr]["validUntil"] >
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
308 QDateTime.currentDateTime()
0c63ea7f94bd Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
309 ):
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
310 # cached entry is still valid
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
311 return self.__lookupApiCache[urlStr]["threatInfo"], error
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
312 else:
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
313 del self.__lookupApiCache[urlStr]
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
314
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
315 # no valid entry found, ask the safe browsing server
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
316 requestBody = {
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
317 "client": {
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
318 "clientId": self.ClientId,
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
319 "clientVersion": self.ClientVersion,
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
320 },
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
321 "threatInfo": {
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
322 "threatTypes": SafeBrowsingAPIClient.definedThreatTypes(),
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
323 "platformTypes": platforms,
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
324 "threatEntryTypes":
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
325 SafeBrowsingAPIClient.definedThreatEntryTypes(),
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
326 "threatEntries": [
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
327 {"url": urlStr},
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
328 ],
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
329 },
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
330 }
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
331
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
332 data = QByteArray(json.dumps(requestBody).encode("utf-8"))
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
333 url = QUrl(self.GsbUrlTemplate.format("threatMatches:find",
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
334 self.__apiKey))
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
335 req = QNetworkRequest(url)
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
336 req.setHeader(QNetworkRequest.ContentTypeHeader, "application/json")
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
337 reply = WebBrowserWindow.networkManager().post(req, data)
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
338
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
339 while reply.isRunning():
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
340 QCoreApplication.processEvents(QEventLoop.AllEvents, 200)
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
341 # max. 200 ms processing
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
342
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
343 threats = []
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
344 if reply.error() != QNetworkReply.NoError:
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
345 error = reply.errorString()
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
346 self.networkError.emit(error)
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
347 else:
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
348 res = json.loads(str(reply.readAll(), "utf-8"))
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
349 if res and "matches" in res:
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
350 cacheDuration = 0
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
351 for match in res["matches"]:
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
352 threatInfo = ThreatList(
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
353 match["threatType"],
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
354 match["platformType"],
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
355 match["threatEntryType"],
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
356 )
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
357 threats.append(threatInfo)
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
358 if "cacheDuration" in match:
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
359 cacheDurationSec = int(
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
360 match["cacheDuration"].strip().rstrip("s")
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
361 .split(".")[0])
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
362 if cacheDurationSec > cacheDuration:
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
363 cacheDuration = cacheDurationSec
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
364 if cacheDuration > 0 and bool(threats):
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
365 validUntil = QDateTime.currentDateTime().addSecs(
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
366 cacheDuration)
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
367 self.__lookupApiCache[urlStr] = {
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
368 "validUntil": validUntil,
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
369 "threatInfo": threats
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
370 }
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
371
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
372 reply.deleteLater()
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
373 return threats, error
6230
77a1b22c8a1f Web Browser NG, Safe Browsing: identified places needing a change to support the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
374
77a1b22c8a1f Web Browser NG, Safe Browsing: identified places needing a change to support the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
375 #######################################################################
77a1b22c8a1f Web Browser NG, Safe Browsing: identified places needing a change to support the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
376 ## Methods below implement global (class wide) functionality
77a1b22c8a1f Web Browser NG, Safe Browsing: identified places needing a change to support the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
377 #######################################################################
77a1b22c8a1f Web Browser NG, Safe Browsing: identified places needing a change to support the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
378
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
379 @classmethod
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
380 def getThreatMessage(cls, threatType):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
381 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
382 Class method to get a warning message for the given threat type.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
383
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
384 @param threatType threat type to get the message for
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
385 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
386 @return threat message
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
387 @rtype str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
388 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
389 threatType = threatType.lower()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
390 if threatType == "malware":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
391 msg = QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
392 "SafeBrowsingAPI",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
393 "<h3>Malware Warning</h3>"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
394 "<p>The web site you are about to visit may try to install"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
395 " harmful programs on your computer in order to steal or"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
396 " destroy your data.</p>")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
397 elif threatType == "social_engineering":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
398 msg = QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
399 "SafeBrowsingAPI",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
400 "<h3>Phishing Warning</h3>"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
401 "<p>The web site you are about to visit may try to trick you"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
402 " into doing something dangerous online, such as revealing"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
403 " passwords or personal information, usually through a fake"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
404 " website.</p>")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
405 elif threatType == "unwanted_software":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
406 msg = QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
407 "SafeBrowsingAPI",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
408 "<h3>Unwanted Software Warning</h3>"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
409 "<p>The software you are about to download may negatively"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
410 " affect your browsing or computing experience.</p>")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
411 elif threatType == "potentially_harmful_application":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
412 msg = QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
413 "SafeBrowsingAPI",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
414 "<h3>Potentially Harmful Application</h3>"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
415 "<p>The web site you are about to visit may try to trick you"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
416 " into installing applications, that may negatively affect"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
417 " your browsing experience.</p>")
5831
536d97e3f1a1 Updated translations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5829
diff changeset
418 elif threatType == "malicious_binary":
536d97e3f1a1 Updated translations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5829
diff changeset
419 msg = QCoreApplication.translate(
536d97e3f1a1 Updated translations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5829
diff changeset
420 "SafeBrowsingAPI",
536d97e3f1a1 Updated translations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5829
diff changeset
421 "<h3>Malicious Binary Warning</h3>"
536d97e3f1a1 Updated translations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5829
diff changeset
422 "<p>The software you are about to download may be harmful"
536d97e3f1a1 Updated translations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5829
diff changeset
423 " to your computer.</p>")
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
424 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
425 # unknow threat
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
426 msg = QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
427 "SafeBrowsingAPI",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
428 "<h3>Unknown Threat Warning</h3>"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
429 "<p>The web site you are about to visit was found in the Safe"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
430 " Browsing Database but was not classified yet.</p>")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
431
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
432 return msg
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
433
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
434 @classmethod
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
435 def getThreatType(cls, threatType):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
436 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
437 Class method to get a display string for a given threat type.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
438
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
439 @param threatType threat type to get display string for
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
440 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
441 @return display string
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
442 @rtype str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
443 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
444 threatType = threatType.lower()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
445 if threatType == "malware":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
446 displayString = QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
447 "SafeBrowsingAPI", "Malware")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
448 elif threatType == "social_engineering":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
449 displayString = QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
450 "SafeBrowsingAPI", "Phishing")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
451 elif threatType == "unwanted_software":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
452 displayString = QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
453 "SafeBrowsingAPI", "Unwanted Software")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
454 elif threatType == "potentially_harmful_application":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
455 displayString = QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
456 "SafeBrowsingAPI", "Harmful Application")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
457 elif threatType == "malcious_binary":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
458 displayString = QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
459 "SafeBrowsingAPI", "Malicious Binary")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
460 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
461 displayString = QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
462 "SafeBrowsingAPI", "Unknown Threat")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
463
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
464 return displayString
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
465
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
466 @classmethod
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
467 def definedThreatTypes(cls):
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
468 """
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
469 Class method to get all threat types defined in API v4.
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
470
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
471 @return list of defined threat types
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
472 @rtype list of str
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
473 """
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
474 return [
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
475 "THREAT_TYPE_UNSPECIFIED", "MALWARE", "SOCIAL_ENGINEERING",
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
476 "UNWANTED_SOFTWARE", "POTENTIALLY_HARMFUL_APPLICATION",
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
477 ]
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
478
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
479 @classmethod
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
480 def getThreatEntryString(cls, threatEntry):
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
481 """
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
482 Class method to get the threat entry string.
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
483
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
484 @param threatEntry threat entry type as defined in the v4 API
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
485 @type str
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
486 @return threat entry string
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
487 @rtype str
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
488 """
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
489 if threatEntry == "URL":
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
490 return "URL"
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
491 elif threatEntry == "EXECUTABLE":
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
492 return QCoreApplication.translate(
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
493 "SafeBrowsingAPI", "executable program")
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
494 else:
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
495 return QCoreApplication.translate(
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
496 "SafeBrowsingAPI", "unknown type")
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
497
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
498 @classmethod
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
499 def definedThreatEntryTypes(cls):
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
500 """
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
501 Class method to get all threat entry types defined in API v4.
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
502
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
503 @return list of all defined threat entry types
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
504 @rtype list of str
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
505 """
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
506 return [
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
507 "THREAT_ENTRY_TYPE_UNSPECIFIED", "URL", "EXECUTABLE",
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
508 ]
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
509
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
510 @classmethod
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
511 def getPlatformString(cls, platformType):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
512 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
513 Class method to get the platform string for a given platform type.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
514
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
515 @param platformType platform type as defined in the v4 API
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
516 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
517 @return platform string
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
518 @rtype str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
519 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
520 platformStrings = {
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
521 "WINDOWS": "Windows",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
522 "LINUX": "Linux",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
523 "ANDROID": "Android",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
524 "OSX": "macOS",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
525 "IOS": "iOS",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
526 "CHROME": "Chrome OS",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
527 }
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
528 if platformType in platformStrings:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
529 return platformStrings[platformType]
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
530
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
531 if platformType == "ANY_PLATFORM":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
532 return QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
533 "SafeBrowsingAPI", "any defined platform")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
534 elif platformType == "ALL_PLATFORMS":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
535 return QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
536 "SafeBrowsingAPI", "all defined platforms")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
537 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
538 return QCoreApplication.translate(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
539 "SafeBrowsingAPI", "unknown platform")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
540
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
541 @classmethod
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
542 def getPlatformTypes(cls, platform):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
543 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
544 Class method to get the platform types for a given platform.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
545
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
546 @param platform platform string
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
547 @type str (one of 'linux', 'windows', 'macos')
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
548 @return list of platform types as defined in the v4 API for the
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
549 given platform
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
550 @rtype list of str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
551 @exception ValueError raised to indicate an invalid platform string
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
552 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
553 platform = platform.lower()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
554
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
555 platformTypes = ["ANY_PLATFORM", "ALL_PLATFORMS"]
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
556 if platform == "linux":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
557 platformTypes.append("LINUX")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
558 elif platform == "windows":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
559 platformTypes.append("WINDOWS")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
560 elif platform == "macos":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
561 platformTypes.append("OSX")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
562 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
563 raise ValueError("Unsupported platform")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
564
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
565 return platformTypes
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
566
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
567 @classmethod
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
568 def definedPlatformTypes(cls):
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
569 """
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
570 Class method to get all platform types defined in API v4.
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
571
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
572 @return list of all defined platform types
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
573 @rtype list of str
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
574 """
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
575 return [
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
576 "PLATFORM_TYPE_UNSPECIFIED", "WINDOWS", "LINUX", "ANDROID", "OSX",
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
577 "IOS", "ANY_PLATFORM", "ALL_PLATFORMS", "CHROME",
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
578 ]

eric ide

mercurial