Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/urlscan/pro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

from functools import cached_property
from typing import BinaryIO
from urllib.parse import quote_plus

from urlscan.client import BaseClient
from urlscan.iterator import SearchIterator
from urlscan.pro.visibility import Visibility
from urlscan.types import MaliciousObservableType
from urlscan.utils import _compact

from .brand import Brand
Expand Down Expand Up @@ -276,3 +278,20 @@ def get_user(self) -> dict:

"""
return self.get_json("/api/v1/pro/username")

def lookup_malicious_observable(
self,
type_: MaliciousObservableType,
value: str,
):
"""Look up how often an observable has been seen in malicious scan results and when it was first and last seen.

Returns:
dict: Malicious observable lookup result.

Reference:
https://docs.urlscan.io/apis/urlscan-openapi/malicious/maliciouslookup

"""
path = f"/api/v1/malicious/{type_}/{quote_plus(value)}"
return self.get_json(path)
1 change: 1 addition & 0 deletions src/urlscan/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@
WatchedAttributeType = Literal[
"detections", "tls", "dns", "labels", "page", "meta", "ip"
]
MaliciousObservableType = Literal["url", "domain", "ip", "hostname"]
13 changes: 13 additions & 0 deletions tests/integration/pro/test_pro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest

from urlscan import Pro


@pytest.mark.integration
def test_lookup_malicious_observable(pro: Pro):
type_ = "url"
value = "https://example.com"
result = pro.lookup_malicious_observable(type_="url", value=value)
assert isinstance(result, dict)
assert result["type"] == type_
assert result["observable"] in value
20 changes: 20 additions & 0 deletions tests/unit/pro/test_pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,23 @@ def test_get_user(pro: Pro, httpserver: HTTPServer):

got = pro.get_user()
assert got == data


def test_lookup_malicious_observable(pro: Pro, httpserver: HTTPServer):
type_ = "url"
value = "https://example.com"
data = {
"type": type_,
"value": value,
"count": 5,
"lastSeen": "2024-06-01T12:00:00.000Z",
"firstSeen": "2024-01-01T08:00:00.000Z",
}
httpserver.expect_request(
# pytest-httpserver (werkzeug) normalizes URL path so no need to quote_plus it in the test
f"/api/v1/malicious/{type_}/{value}",
method="GET",
).respond_with_json(data)

got = pro.lookup_malicious_observable(type_, value) # type: ignore[arg-type]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy behaves strangely here (it detects an incompatible type error here, meanwhile Pyright doesn't)

assert got == data