WebBrowser/SafeBrowsing/SafeBrowsingAPIClient.py

branch
safe_browsing
changeset 5829
d3448873ced3
parent 5820
b610cb5b501a
child 5831
536d97e3f1a1
equal deleted inserted replaced
5821:6c7766cde4c1 5829:d3448873ced3
172 requestBody["threatInfo"]["threatEntries"].append( 172 requestBody["threatInfo"]["threatEntries"].append(
173 {"hash": base64.b64encode(prefix).decode("ascii")}) 173 {"hash": base64.b64encode(prefix).decode("ascii")})
174 174
175 for (threatType, platformType, threatEntryType), currentState in \ 175 for (threatType, platformType, threatEntryType), currentState in \
176 clientState.items(): 176 clientState.items():
177 requestBody["clientStates"].append(clientState) 177 requestBody["clientStates"].append(currentState)
178 if threatType not in requestBody["threatInfo"]["threatTypes"]: 178 if threatType not in requestBody["threatInfo"]["threatTypes"]:
179 requestBody["threatInfo"]["threatTypes"].append(threatType) 179 requestBody["threatInfo"]["threatTypes"].append(threatType)
180 if platformType not in \ 180 if platformType not in \
181 requestBody["threatInfo"]["platformTypes"]: 181 requestBody["threatInfo"]["platformTypes"]:
182 requestBody["threatInfo"]["platformTypes"].append( 182 requestBody["threatInfo"]["platformTypes"].append(
227 @type str 227 @type str
228 """ 228 """
229 if not self.__fairUse or minimumWaitDuration is None: 229 if not self.__fairUse or minimumWaitDuration is None:
230 self.__nextRequestNoSoonerThan = QDateTime() 230 self.__nextRequestNoSoonerThan = QDateTime()
231 else: 231 else:
232 waitDuration = int(minimumWaitDuration.rstrip("s")) 232 waitDuration = int(float(minimumWaitDuration.rstrip("s")))
233 self.__nextRequestNoSoonerThan = \ 233 self.__nextRequestNoSoonerThan = \
234 QDateTime.currentDateTime().addSecs(waitDuration) 234 QDateTime.currentDateTime().addSecs(waitDuration)
235 235
236 def fairUseDelayExpired(self): 236 def fairUseDelayExpired(self):
237 """ 237 """
242 """ 242 """
243 return ( 243 return (
244 self.__fairUse and 244 self.__fairUse and
245 QDateTime.currentDateTime() >= self.__nextRequestNoSoonerThan 245 QDateTime.currentDateTime() >= self.__nextRequestNoSoonerThan
246 ) or not self.__fairUse 246 ) or not self.__fairUse
247
248 def getFairUseDelayExpirationDateTime(self):
249 """
250 Public method to get the date and time the fair use delay will expire.
251
252 @return fair use delay expiration date and time
253 @rtype QDateTime
254 """
255 return self.__nextRequestNoSoonerThan
256
257 @classmethod
258 def getThreatMessage(cls, threatType):
259 """
260 Class method to get a warning message for the given threat type.
261
262 @param threatType threat type to get the message for
263 @type str
264 @return threat message
265 @rtype str
266 """
267 threatType = threatType.lower()
268 if threatType == "malware":
269 msg = QCoreApplication.translate(
270 "SafeBrowsingAPI",
271 "<h3>Malware Warning</h3>"
272 "<p>The web site you are about to visit may try to install"
273 " harmful programs on your computer in order to steal or"
274 " destroy your data.</p>")
275 elif threatType == "social_engineering":
276 msg = QCoreApplication.translate(
277 "SafeBrowsingAPI",
278 "<h3>Phishing Warning</h3>"
279 "<p>The web site you are about to visit may try to trick you"
280 " into doing something dangerous online, such as revealing"
281 " passwords or personal information, usually through a fake"
282 " website.</p>")
283 elif threatType == "unwanted_software":
284 msg = QCoreApplication.translate(
285 "SafeBrowsingAPI",
286 "<h3>Unwanted Software Warning</h3>"
287 "<p>The software you are about to download may negatively"
288 " affect your browsing or computing experience.</p>")
289 elif threatType == "potentially_harmful_application":
290 msg = QCoreApplication.translate(
291 "SafeBrowsingAPI",
292 "<h3>Potentially Harmful Application</h3>"
293 "<p>The web site you are about to visit may try to trick you"
294 " into installing applications, that may negatively affect"
295 " your browsing experience.</p>")
296 else:
297 # unknow threat
298 msg = QCoreApplication.translate(
299 "SafeBrowsingAPI",
300 "<h3>Unknown Threat Warning</h3>"
301 "<p>The web site you are about to visit was found in the Safe"
302 " Browsing Database but was not classified yet.</p>")
303
304 return msg
305
306 @classmethod
307 def getThreatType(cls, threatType):
308 """
309 Class method to get a display string for a given threat type.
310
311 @param threatType threat type to get display string for
312 @type str
313 @return display string
314 @rtype str
315 """
316 threatType = threatType.lower()
317 if threatType == "malware":
318 displayString = QCoreApplication.translate(
319 "SafeBrowsingAPI", "Malware")
320 elif threatType == "social_engineering":
321 displayString = QCoreApplication.translate(
322 "SafeBrowsingAPI", "Phishing")
323 elif threatType == "unwanted_software":
324 displayString = QCoreApplication.translate(
325 "SafeBrowsingAPI", "Unwanted Software")
326 elif threatType == "potentially_harmful_application":
327 displayString = QCoreApplication.translate(
328 "SafeBrowsingAPI", "Harmful Application")
329 elif threatType == "malcious_binary":
330 displayString = QCoreApplication.translate(
331 "SafeBrowsingAPI", "Malicious Binary")
332 else:
333 displayString = QCoreApplication.translate(
334 "SafeBrowsingAPI", "Unknown Threat")
335
336 return displayString
337
338 @classmethod
339 def getPlatformString(cls, platformType):
340 """
341 Class method to get the platform string for a given platform type.
342
343 @param platformType platform type as defined in the v4 API
344 @type str
345 @return platform string
346 @rtype str
347 """
348 platformStrings = {
349 "WINDOWS": "Windows",
350 "LINUX": "Linux",
351 "ANDROID": "Android",
352 "OSX": "macOS",
353 "IOS": "iOS",
354 "CHROME": "Chrome OS",
355 }
356 if platformType in platformStrings:
357 return platformStrings[platformType]
358
359 if platformType == "ANY_PLATFORM":
360 return QCoreApplication.translate(
361 "SafeBrowsingAPI", "any defined platform")
362 elif platformType == "ALL_PLATFORMS":
363 return QCoreApplication.translate(
364 "SafeBrowsingAPI", "all defined platforms")
365 else:
366 return QCoreApplication.translate(
367 "SafeBrowsingAPI", "unknown platform")
368
369 @classmethod
370 def getThreatEntryString(cls, threatEntry):
371 """
372 Class method to get the threat entry string.
373
374 @param threatEntry threat entry type as defined in the v4 API
375 @type str
376 @return threat entry string
377 @rtype str
378 """
379 if threatEntry == "URL":
380 return "URL"
381 elif threatEntry == "EXECUTABLE":
382 return QCoreApplication.translate(
383 "SafeBrowsingAPI", "executable program")
384 else:
385 return QCoreApplication.translate(
386 "SafeBrowsingAPI", "unknown type")
387
388 @classmethod
389 def getPlatformTypes(cls, platform):
390 """
391 Class method to get the platform types for a given platform.
392
393 @param platform platform string
394 @type str (one of 'linux', 'windows', 'macos')
395 @return list of platform types as defined in the v4 API for the
396 given platform
397 @rtype list of str
398 @exception ValueError raised to indicate an invalid platform string
399 """
400 platform = platform.lower()
401
402 platformTypes = ["ANY_PLATFORM", "ALL_PLATFORMS"]
403 if platform == "linux":
404 platformTypes.append("LINUX")
405 elif platform == "windows":
406 platformTypes.append("WINDOWS")
407 elif platform == "macos":
408 platformTypes.append("OSX")
409 else:
410 raise ValueError("Unsupported platform")
411
412 return platformTypes

eric ide

mercurial