| 1 | """Which hiring platform a careers page runs on, read off the page itself. |
| 2 | |
| 3 | A large company's careers site is usually one of a few hosted platforms under |
| 4 | the company's own host name. The page names its platform in its markup: the |
| 5 | API host and site number of Oracle Recruiting Cloud, Phenom's ``phApp`` page |
| 6 | object, TalentBrew's asset host, Jibe's asset host, Eightfold's group id, a |
| 7 | link to a Kula board (``careers.kula.ai/<slug>``). The |
| 8 | detector reads that and returns the board to read, so no company is added by |
| 9 | hand. Nothing here makes a request. |
| 10 | |
| 11 | Each board is returned as ``(provider, entry)``, where ``entry`` holds what |
| 12 | the platform's reader needs (``board`` is always the host to call) and has |
| 13 | the shape of a registry entry in ``lib.jobs``. |
| 14 | """ |
| 15 | |
| 16 | from __future__ import annotations |
| 17 | |
| 18 | from collections import OrderedDict |
| 19 | import functools |
| 20 | import ipaddress |
| 21 | import re |
| 22 | import socket |
| 23 | import threading |
| 24 | import time |
| 25 | from html import unescape |
| 26 | from typing import Any, Callable |
| 27 | from urllib.parse import urlparse |
| 28 | |
| 29 | from .. import public_host |
| 30 | |
| 31 | PROVIDER_ORACLE = "oracle" |
| 32 | PROVIDER_JIBE = "jibe" |
| 33 | PROVIDER_PHENOM = "phenom" |
| 34 | PROVIDER_TALENTBREW = "talentbrew" |
| 35 | PROVIDER_EIGHTFOLD = "eightfold" |
| 36 | PROVIDER_KULA = "kula" |
| 37 | KULA_HOST = "careers.kula.ai" |
| 38 | |
| 39 | MAX_HTML_BYTES = 5 * 1024 * 1024 |
| 40 | MAX_HTML_CHARS = 5 * 1024 * 1024 |
| 41 | DNS_CACHE_TTL_SECONDS = 5.0 |
| 42 | DNS_CACHE_MAXSIZE = 256 |
| 43 | |
| 44 | # The order platforms are tried in: a page names one platform's own API |
| 45 | # before another's asset (a Phenom page can link a Workday apply URL, which |
| 46 | # ``lib.jobs`` reads on its own first). |
| 47 | PLATFORMS = ( |
| 48 | PROVIDER_ORACLE, PROVIDER_PHENOM, PROVIDER_EIGHTFOLD, PROVIDER_JIBE, PROVIDER_TALENTBREW, |
| 49 | PROVIDER_KULA, |
| 50 | ) |
| 51 | |
| 52 | _ORACLE_HOST = r"[a-z0-9-]+\.fa(?:\.[a-z0-9-]+)?\.oraclecloud\.com" |
| 53 | _ORACLE_HOST_RE = re.compile(rf"^{_ORACLE_HOST}$", re.I) |
| 54 | _SECOND_LEVEL_PUBLIC_LABELS = frozenset({ |
| 55 | "ac", "co", "com", "edu", "gob", "gov", "mil", "ne", "net", "or", "org", |
| 56 | }) |
| 57 | _MULTI_TENANT_SUFFIXES = frozenset({ |
| 58 | "appspot.com", "azurewebsites.net", "blogspot.com", "cloudfront.net", |
| 59 | "firebaseapp.com", "fly.dev", "github.io", "gitlab.io", "herokuapp.com", |
| 60 | "myshopify.com", "netlify.app", "onrender.com", "pages.dev", |
| 61 | "s3.amazonaws.com", "squarespace.com", "vercel.app", "web.app", |
| 62 | "wixsite.com", "wordpress.com", "workers.dev", |
| 63 | }) |
| 64 | _RESERVED_HOST_SUFFIXES = frozenset({"localhost", "local", "internal", "localdomain", "arpa"}) |
| 65 | _VALID_HOST_RE = re.compile( |
| 66 | r"[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+" |
| 67 | ) |
| 68 | _VALID_TLD_RE = re.compile(r"[a-z]{2,63}|xn--[a-z0-9-]{2,59}") |
| 69 | _UNSAFE_URL_CHAR_RE = re.compile(r"[\s\x00-\x1f\x7f\\]") |
| 70 | _HTML_ATTR_RE = re.compile( |
| 71 | r'([A-Za-z_:][\w:.-]*)(?:\s*=\s*(?:"([^"]*)"|\'([^\']*)\'|([^\s"\'=<>`]+)))?' |
| 72 | ) |
| 73 | _START_TAG_RE = re.compile(r"<[a-zA-Z][a-zA-Z0-9:-]*(?:\s[^<>]*)?>") |
| 74 | _SCRIPT_OPEN_RE = re.compile(r"<script\b([^<>]*)>", re.I) |
| 75 | _SCRIPT_CLOSE_RE = re.compile(r"</script(?:\s[^<>]*)?>", re.I) |
| 76 | _META_TAG_RE = re.compile(r"<meta\b([^<>]*)>", re.I) |
| 77 | _LINK_TAG_RE = re.compile(r"<link\b([^<>]*)>", re.I) |
| 78 | _TITLE_OPEN_RE = re.compile(r"<title\b[^<>]*>", re.I) |
| 79 | _TITLE_CLOSE_RE = re.compile(r"</title(?:\s[^<>]*)?>", re.I) |
| 80 | _ORACLE_SITE_NUMBER_VALUE_RE = re.compile(r"^CX_\d+$") |
| 81 | _ORACLE_SITE_LINK = re.compile( |
| 82 | rf"https?://({_ORACLE_HOST})(?::443)?/hcmUI/CandidateExperience/[A-Za-z-]+/sites/(CX_\d+)", re.I, |
| 83 | ) |
| 84 | _ORACLE_SITE_NUMBER = re.compile(r"siteNumber=(CX_\d+)") |
| 85 | _PHENOM_BASE = re.compile(r'"baseUrl"\s*:\s*"(https://[^"/]+/([a-z]{2,8})/([a-z]{2})/)"') |
| 86 | _EIGHTFOLD_GROUP = re.compile(r'_EF_GROUP_ID\s*=\s*"([A-Za-z0-9.-]+)"') |
| 87 | # A Kula board: the company's slug on Kula's own careers host. Kula's assets |
| 88 | # are on other hosts (``careers-cdn.kula.ai``), so a slug is never ``_next``. |
| 89 | KULA_BOARD_LINK = r"careers\.kula\.ai/([A-Za-z0-9][A-Za-z0-9_-]*)" |
| 90 | _HTTPS_PREFIX_RE = re.compile(r"\s*(https://[^<\s]+)", re.I) |
| 91 | |
| 92 | |
| 93 | def parse_html_attrs(raw: str) -> dict[str, str]: |
| 94 | """Parse attributes from a single matched HTML tag header in linear time.""" |
| 95 | attrs: dict[str, str] = {} |
| 96 | for match in _HTML_ATTR_RE.finditer(raw): |
| 97 | name = match.group(1).lower() |
| 98 | val = match.group(2) if match.group(2) is not None else ( |
| 99 | match.group(3) if match.group(3) is not None else (match.group(4) or "") |
| 100 | ) |
| 101 | if name not in attrs: |
| 102 | attrs[name] = unescape(val) |
| 103 | return attrs |
| 104 | |
| 105 | |
| 106 | def extract_script_bodies(html: str) -> list[str]: |
| 107 | """Extract ``<script>...</script>`` bodies in strictly linear time without |
| 108 | using offsets from a lowercased copy (which shift on chars like U+0130).""" |
| 109 | if not html: |
| 110 | return [] |
| 111 | bounded = html[:MAX_HTML_CHARS] |
| 112 | if not _SCRIPT_CLOSE_RE.search(bounded): |
| 113 | return [] |
| 114 | bodies: list[str] = [] |
| 115 | cursor = 0 |
| 116 | for match in _SCRIPT_OPEN_RE.finditer(bounded): |
| 117 | if match.start() < cursor: |
| 118 | continue |
| 119 | close = _SCRIPT_CLOSE_RE.search(bounded, match.end()) |
| 120 | if close is None: |
| 121 | break |
| 122 | bodies.append(bounded[match.end():close.start()]) |
| 123 | cursor = close.end() |
| 124 | return bodies |
| 125 | |
| 126 | |
| 127 | def extract_jsonld_blocks(html: str) -> list[str]: |
| 128 | """Extract ``<script type="application/ld+json">`` blocks in strictly linear time.""" |
| 129 | if not html: |
| 130 | return [] |
| 131 | bounded = html[:MAX_HTML_CHARS] |
| 132 | if "ld+json" not in bounded.lower() or not _SCRIPT_CLOSE_RE.search(bounded): |
| 133 | return [] |
| 134 | blocks: list[str] = [] |
| 135 | cursor = 0 |
| 136 | for match in _SCRIPT_OPEN_RE.finditer(bounded): |
| 137 | if match.start() < cursor: |
| 138 | continue |
| 139 | attrs_raw = match.group(1) |
| 140 | if "ld+json" not in attrs_raw.lower(): |
| 141 | continue |
| 142 | script_type = parse_html_attrs(attrs_raw).get("type", "").split(";", 1)[0].strip().lower() |
| 143 | if script_type != "application/ld+json": |
| 144 | continue |
| 145 | close = _SCRIPT_CLOSE_RE.search(bounded, match.end()) |
| 146 | if close is None: |
| 147 | break |
| 148 | blocks.append(bounded[match.end():close.start()]) |
| 149 | cursor = close.end() |
| 150 | return blocks |
| 151 | |
| 152 | |
| 153 | def extract_meta_contents(html: str, attr_name: str, attr_value: str) -> list[str]: |
| 154 | """Extract ``content`` values from matching ``<meta>`` tags in strictly linear time.""" |
| 155 | if not html: |
| 156 | return [] |
| 157 | bounded = html[:MAX_HTML_CHARS] |
| 158 | want_name = attr_name.lower() |
| 159 | want_value = attr_value.lower() |
| 160 | lowered = bounded.lower() |
| 161 | if "<meta" not in lowered or want_value not in lowered: |
| 162 | return [] |
| 163 | found: list[str] = [] |
| 164 | for match in _META_TAG_RE.finditer(bounded): |
| 165 | attrs_raw = match.group(1) |
| 166 | if want_value not in attrs_raw.lower(): |
| 167 | continue |
| 168 | attrs = parse_html_attrs(attrs_raw) |
| 169 | if attrs.get(want_name, "").strip().lower() == want_value: |
| 170 | content = attrs.get("content", "").strip() |
| 171 | if content: |
| 172 | found.append(content) |
| 173 | return found |
| 174 | |
| 175 | |
| 176 | def extract_title_texts(html: str) -> list[str]: |
| 177 | """Extract ``<title>...</title>`` text contents in strictly linear time.""" |
| 178 | if not html: |
| 179 | return [] |
| 180 | bounded = html[:MAX_HTML_CHARS] |
| 181 | if not _TITLE_CLOSE_RE.search(bounded): |
| 182 | return [] |
| 183 | titles: list[str] = [] |
| 184 | cursor = 0 |
| 185 | for match in _TITLE_OPEN_RE.finditer(bounded): |
| 186 | if match.start() < cursor: |
| 187 | continue |
| 188 | close = _TITLE_CLOSE_RE.search(bounded, match.end()) |
| 189 | if close is None: |
| 190 | break |
| 191 | raw = unescape(bounded[match.end():close.start()]).strip() |
| 192 | if raw: |
| 193 | titles.append(raw) |
| 194 | cursor = close.end() |
| 195 | return titles |
| 196 | |
| 197 | |
| 198 | def page_host(url: str) -> str: |
| 199 | """The host of a page URL, lowercased and without a port or trailing dot.""" |
| 200 | raw = (url or "").strip() |
| 201 | if not raw or "\\" in raw or any(ch.isspace() or ord(ch) < 0x20 or ord(ch) == 0x7F for ch in raw): |
| 202 | return "" |
| 203 | try: |
| 204 | parsed = urlparse(raw) |
| 205 | _ = parsed.port |
| 206 | except ValueError: |
| 207 | return "" |
| 208 | if parsed.username is not None or parsed.password is not None: |
| 209 | return "" |
| 210 | raw_host = (parsed.hostname or "").lower() |
| 211 | if not raw_host or "\\" in raw_host or ".." in raw_host or raw_host.startswith("."): |
| 212 | return "" |
| 213 | return raw_host.rstrip(".") |
| 214 | |
| 215 | |
| 216 | @functools.lru_cache(maxsize=1024) |
| 217 | def _is_safe_public_host(raw_host: str) -> bool: |
| 218 | if not raw_host or ".." in raw_host or raw_host.startswith("."): |
| 219 | return False |
| 220 | host = raw_host.rstrip(".") |
| 221 | if not host or len(host) > 253 or not _VALID_HOST_RE.fullmatch(host): |
| 222 | return False |
| 223 | if public_host._parse_ip_literal(host) is not None: |
| 224 | return False |
| 225 | labels = host.split(".") |
| 226 | if labels[-1] in _RESERVED_HOST_SUFFIXES or not _VALID_TLD_RE.fullmatch(labels[-1]): |
| 227 | return False |
| 228 | return True |
| 229 | |
| 230 | |
| 231 | def is_safe_public_url(url: str) -> bool: |
| 232 | """Zero-DNS check: http(s) URL with standard port, no userinfo/backslash, |
| 233 | not an IP literal or reserved/internal domain.""" |
| 234 | if not isinstance(url, str) or not url or _UNSAFE_URL_CHAR_RE.search(url): |
| 235 | return False |
| 236 | try: |
| 237 | parsed = urlparse(url) |
| 238 | port = parsed.port |
| 239 | except ValueError: |
| 240 | return False |
| 241 | scheme = parsed.scheme.lower() |
| 242 | if scheme not in ("http", "https"): |
| 243 | return False |
| 244 | if parsed.username is not None or parsed.password is not None: |
| 245 | return False |
| 246 | if port is not None and not ( |
| 247 | (scheme == "https" and port == 443) or (scheme == "http" and port == 80) |
| 248 | ): |
| 249 | return False |
| 250 | if not parsed.netloc.isascii() or "%" in parsed.netloc: |
| 251 | return False |
| 252 | return _is_safe_public_host((parsed.hostname or "").lower()) |
| 253 | |
| 254 | |
| 255 | @functools.lru_cache(maxsize=1024) |
| 256 | def registrable_domain(host: str) -> str: |
| 257 | """Return the registrable site domain (eTLD+1), or '' for IP literals, |
| 258 | malformed hosts, single-label hosts, or bare public suffixes.""" |
| 259 | raw = (host or "").strip().lower() |
| 260 | if not raw or ".." in raw or raw.startswith("."): |
| 261 | return "" |
| 262 | h = raw.rstrip(".") |
| 263 | if not h or len(h) > 253 or not _VALID_HOST_RE.fullmatch(h): |
| 264 | return "" |
| 265 | if public_host._parse_ip_literal(h) is not None: |
| 266 | return "" |
| 267 | parts = h.removeprefix("www.").split(".") |
| 268 | if len(parts) < 2 or parts[-1] in _RESERVED_HOST_SUFFIXES or not _VALID_TLD_RE.fullmatch(parts[-1]): |
| 269 | return "" |
| 270 | if len(parts) >= 3 and ".".join(parts[-3:]) in _MULTI_TENANT_SUFFIXES: |
| 271 | return ".".join(parts[-4:]) if len(parts) >= 4 else "" |
| 272 | tail2 = ".".join(parts[-2:]) |
| 273 | if tail2 in _MULTI_TENANT_SUFFIXES or ( |
| 274 | len(parts[-1]) == 2 and parts[-2] in _SECOND_LEVEL_PUBLIC_LABELS |
| 275 | ): |
| 276 | return ".".join(parts[-3:]) if len(parts) >= 3 else "" |
| 277 | return tail2 |
| 278 | |
| 279 | |
| 280 | def registrable_label(host: str) -> str: |
| 281 | """Return the leftmost label of ``registrable_domain(host)``, or ''.""" |
| 282 | reg = registrable_domain(host) |
| 283 | return reg.split(".", 1)[0] if reg else "" |
| 284 | |
| 285 | |
| 286 | def same_host_or_subdomain(host: str, site_host: str) -> bool: |
| 287 | """Whether ``host`` is ``site_host`` (ignoring ``www.``) or a strict |
| 288 | subdomain of it, rejecting IP literals and public-suffix-only bases.""" |
| 289 | if not registrable_domain(host) or not registrable_domain(site_host): |
| 290 | return False |
| 291 | base = site_host.strip().rstrip(".").lower().removeprefix("www.") |
| 292 | cand = host.strip().rstrip(".").lower().removeprefix("www.") |
| 293 | return bool(base) and (cand == base or cand.endswith("." + base)) |
| 294 | |
| 295 | |
| 296 | def _resolve_all_public(name: str) -> bool: |
| 297 | try: |
| 298 | answers = socket.getaddrinfo(name, None, type=socket.SOCK_STREAM) |
| 299 | except (OSError, UnicodeError): |
| 300 | return False |
| 301 | addresses: set[ipaddress.IPv4Address | ipaddress.IPv6Address] = set() |
| 302 | for answer in answers: |
| 303 | try: |
| 304 | addresses.add(ipaddress.ip_address(answer[4][0].split("%", 1)[0])) |
| 305 | except ValueError: |
| 306 | return False |
| 307 | return bool(addresses) and all(public_host._is_public_ip(address) for address in addresses) |
| 308 | |
| 309 | |
| 310 | class _BoundedPublicResolver: |
| 311 | """Thread-safe bounded-TTL DNS resolver verifying every address is public.""" |
| 312 | |
| 313 | def __init__( |
| 314 | self, |
| 315 | *, |
| 316 | maxsize: int = DNS_CACHE_MAXSIZE, |
| 317 | ttl_seconds: float = DNS_CACHE_TTL_SECONDS, |
| 318 | ) -> None: |
| 319 | self.maxsize = maxsize |
| 320 | self.ttl_seconds = ttl_seconds |
| 321 | self._lock = threading.Lock() |
| 322 | self._cache: OrderedDict[str, tuple[bool, float]] = OrderedDict() |
| 323 | |
| 324 | def cache_clear(self) -> None: |
| 325 | with self._lock: |
| 326 | self._cache.clear() |
| 327 | |
| 328 | def __call__(self, name: str) -> bool: |
| 329 | raw_host = (name or "").strip().lower() |
| 330 | if not raw_host or ".." in raw_host or raw_host.startswith("."): |
| 331 | return False |
| 332 | bare = raw_host.rstrip(".") |
| 333 | if not bare or bare == "localhost" or bare.endswith(".localhost"): |
| 334 | return False |
| 335 | literal = public_host._parse_ip_literal(bare) |
| 336 | if literal is not None: |
| 337 | return public_host._is_public_ip(literal) |
| 338 | dns_host = raw_host |
| 339 | now = time.monotonic() |
| 340 | with self._lock: |
| 341 | cached = self._cache.get(dns_host) |
| 342 | if cached is not None: |
| 343 | verdict, expires_at = cached |
| 344 | if now < expires_at: |
| 345 | self._cache.move_to_end(dns_host) |
| 346 | return verdict |
| 347 | del self._cache[dns_host] |
| 348 | verdict = _resolve_all_public(dns_host) |
| 349 | expires_at = time.monotonic() + self.ttl_seconds |
| 350 | with self._lock: |
| 351 | self._cache[dns_host] = (verdict, expires_at) |
| 352 | self._cache.move_to_end(dns_host) |
| 353 | while len(self._cache) > self.maxsize: |
| 354 | self._cache.popitem(last=False) |
| 355 | return verdict |
| 356 | |
| 357 | |
| 358 | resolves_public = _BoundedPublicResolver() |
| 359 | |
| 360 | |
| 361 | def is_same_site_public_url( |
| 362 | url: str, |
| 363 | site_host: str, |
| 364 | *, |
| 365 | require_https: bool = False, |
| 366 | ) -> bool: |
| 367 | """Zero-DNS check: ``url`` is a safe standard-port http(s) URL on the |
| 368 | site of ``site_host`` (requiring ``https`` when ``require_https`` is set).""" |
| 369 | if not is_safe_public_url(url): |
| 370 | return False |
| 371 | if require_https and urlparse(url).scheme.lower() != "https": |
| 372 | return False |
| 373 | target_host = page_host(url) |
| 374 | return bool(target_host) and same_host_or_subdomain(target_host, site_host) |
| 375 | |
| 376 | |
| 377 | def public_site_url( |
| 378 | url: str, |
| 379 | site_host: str, |
| 380 | *, |
| 381 | resolves: Callable[[str], bool] | None = None, |
| 382 | require_https: bool = False, |
| 383 | ) -> bool: |
| 384 | """Whether ``url`` is a safe standard-port http(s) URL on the site of |
| 385 | ``site_host`` whose host resolves exclusively to public IPs.""" |
| 386 | if not is_same_site_public_url(url, site_host, require_https=require_https): |
| 387 | return False |
| 388 | parsed = urlparse(url) |
| 389 | dns_host = (parsed.hostname or "").lower() |
| 390 | return (resolves or resolves_public)(dns_host or page_host(url)) |
| 391 | |
| 392 | |
| 393 | def public_redirect_guard(url: str) -> Callable[[str], bool]: |
| 394 | """Stateless redirect guard allowing any safe public URL while forbidding |
| 395 | ``https -> http`` downgrades from an ``https://`` origin.""" |
| 396 | if not is_safe_public_url(url): |
| 397 | return lambda _target: False |
| 398 | if urlparse(url).scheme.lower() != "https": |
| 399 | return is_safe_public_url |
| 400 | return lambda target: is_safe_public_url(target) and urlparse(target).scheme.lower() == "https" |
| 401 | |
| 402 | |
| 403 | def same_site_redirect_guard( |
| 404 | url: str, |
| 405 | *, |
| 406 | resolves: Callable[[str], bool] | None = None, |
| 407 | ) -> Callable[[str], bool]: |
| 408 | """Stateless redirect guard allowing only safe standard-port http(s) |
| 409 | same-site URLs, forbidding ``https -> http`` downgrades from an |
| 410 | ``https://`` origin. |
| 411 | |
| 412 | When ``resolves`` is omitted, no live DNS lookup runs inside the guard so |
| 413 | transient DNS errors remain retryable; callers pair this guard with |
| 414 | ``http.*(..., public_only=True)`` which resolves and pins public IPs at |
| 415 | connect time. |
| 416 | """ |
| 417 | if not is_safe_public_url(url): |
| 418 | return lambda _target: False |
| 419 | origin_host = page_host(url) |
| 420 | if not origin_host: |
| 421 | return lambda _target: False |
| 422 | require_https = urlparse(url).scheme.lower() == "https" |
| 423 | if resolves is None: |
| 424 | return lambda target: is_same_site_public_url( |
| 425 | target, origin_host, require_https=require_https, |
| 426 | ) |
| 427 | return lambda target: public_site_url( |
| 428 | target, origin_host, resolves=resolves, require_https=require_https, |
| 429 | ) |
| 430 | |
| 431 | |
| 432 | def detect_platform(html: str, page_url: str) -> tuple[str, dict[str, Any]] | None: |
| 433 | """The platform board a careers page names, or None. |
| 434 | |
| 435 | ``page_url`` is the address that answered with ``html`` (after |
| 436 | redirects): a platform served under the company's own host is called on |
| 437 | that host. |
| 438 | """ |
| 439 | if not html: |
| 440 | return None |
| 441 | bounded = html[:MAX_HTML_CHARS] |
| 442 | host = page_host(page_url) |
| 443 | for provider in PLATFORMS: |
| 444 | entry = _DETECTORS[provider](bounded, host) |
| 445 | if entry: |
| 446 | return provider, {"provider": provider, **entry} |
| 447 | return None |
| 448 | |
| 449 | |
| 450 | def eightfold_career_url(html: str) -> str: |
| 451 | """The Eightfold board a careers landing page points at (Microsoft's |
| 452 | ``careers-eightFoldCareerUrl`` element), or ''.""" |
| 453 | bounded = (html or "")[:MAX_HTML_CHARS] |
| 454 | if "eightfoldcareerurl" not in bounded.lower(): |
| 455 | return "" |
| 456 | for match in _START_TAG_RE.finditer(bounded): |
| 457 | if "eightfoldcareerurl" not in match.group(0).lower(): |
| 458 | continue |
| 459 | url_match = _HTTPS_PREFIX_RE.match(bounded, match.end()) |
| 460 | if url_match: |
| 461 | return unescape(url_match.group(1)) |
| 462 | return "" |
| 463 | |
| 464 | |
| 465 | def _oracle(html: str, host: str) -> dict[str, Any] | None: |
| 466 | lowered = html.lower() |
| 467 | if "data-apibaseurl" in lowered and "data-sitenumber" in lowered: |
| 468 | for match in _START_TAG_RE.finditer(html): |
| 469 | tag_text = match.group(0) |
| 470 | tag_lower = tag_text.lower() |
| 471 | if "data-apibaseurl" not in tag_lower or "data-sitenumber" not in tag_lower: |
| 472 | continue |
| 473 | attrs = parse_html_attrs(tag_text[1:-1]) |
| 474 | raw_api_url = attrs.get("data-apibaseurl", "").strip() |
| 475 | site_number = attrs.get("data-sitenumber", "").strip() |
| 476 | if not raw_api_url or not _ORACLE_SITE_NUMBER_VALUE_RE.fullmatch(site_number): |
| 477 | continue |
| 478 | api_host = page_host(raw_api_url) |
| 479 | if ( |
| 480 | api_host |
| 481 | and is_safe_public_url(raw_api_url) |
| 482 | and (_ORACLE_HOST_RE.fullmatch(api_host) or (host and _same_site(api_host, host))) |
| 483 | ): |
| 484 | return {"board": api_host, "site_number": site_number} |
| 485 | if "oraclecloud" in lowered: |
| 486 | match = _ORACLE_SITE_LINK.search(html) |
| 487 | if match: |
| 488 | return {"board": match.group(1).lower(), "site_number": match.group(2)} |
| 489 | if "sitenumber=" not in lowered: |
| 490 | return None |
| 491 | number = _ORACLE_SITE_NUMBER.search(html) |
| 492 | if not number: |
| 493 | return None |
| 494 | if "oraclecloud" in lowered: |
| 495 | api = re.search(rf"https?://({_ORACLE_HOST})", html, re.I) |
| 496 | if api: |
| 497 | return {"board": api.group(1).lower(), "site_number": number.group(1)} |
| 498 | if ( |
| 499 | host |
| 500 | and is_safe_public_url(f"https://{host}/") |
| 501 | and "/hcmUI/CandidateExperience" in html |
| 502 | and "/hcmRestApi/" in html |
| 503 | ): |
| 504 | # Served under the company's own host (Dell): the API answers there. |
| 505 | return {"board": host, "site_number": number.group(1)} |
| 506 | return None |
| 507 | |
| 508 | |
| 509 | def _phenom(html: str, host: str) -> dict[str, Any] | None: |
| 510 | if not host or "phApp" not in html or "phenom" not in html.lower(): |
| 511 | return None |
| 512 | for match in _PHENOM_BASE.finditer(html): |
| 513 | base, country, lang = match.groups() |
| 514 | if not is_safe_public_url(base): |
| 515 | continue |
| 516 | parsed = urlparse(base) |
| 517 | if parsed.scheme.lower() != "https" or parsed.port not in (None, 443): |
| 518 | continue |
| 519 | board = page_host(base) |
| 520 | if board and _same_site(board, host): |
| 521 | return { |
| 522 | "board": board, |
| 523 | "country": country, |
| 524 | "lang": lang, |
| 525 | "base_url": f"https://{board}/{country}/{lang}/", |
| 526 | } |
| 527 | return None |
| 528 | |
| 529 | |
| 530 | def _eightfold(html: str, host: str) -> dict[str, Any] | None: |
| 531 | lowered = html.lower() |
| 532 | if "eightfold" not in lowered or not host or not is_safe_public_url(f"https://{host}/"): |
| 533 | return None |
| 534 | group = _EIGHTFOLD_GROUP.search(html) |
| 535 | if not group: |
| 536 | return None |
| 537 | # The API generation is found by the reader: ``pcsx`` first, then ``v2``. |
| 538 | return {"board": host, "domain": group.group(1).lower()} |
| 539 | |
| 540 | |
| 541 | def _canonical_href(html: str) -> str: |
| 542 | bounded = html[:MAX_HTML_CHARS] |
| 543 | if "canonical" not in bounded.lower(): |
| 544 | return "" |
| 545 | for match in _LINK_TAG_RE.finditer(bounded): |
| 546 | attrs_raw = match.group(1) |
| 547 | if "canonical" not in attrs_raw.lower(): |
| 548 | continue |
| 549 | attrs = parse_html_attrs(attrs_raw) |
| 550 | if "canonical" in attrs.get("rel", "").lower().split() and attrs.get("href"): |
| 551 | return attrs["href"].strip() |
| 552 | return "" |
| 553 | |
| 554 | |
| 555 | def _jibe(html: str, host: str) -> dict[str, Any] | None: |
| 556 | if not host or not is_safe_public_url(f"https://{host}/") or "jibecdn.com" not in html: |
| 557 | return None |
| 558 | # A role's page is under the site's jobs path (``/careers-home/jobs/95802``). |
| 559 | prefix = "" |
| 560 | canonical = _canonical_href(html) |
| 561 | if canonical: |
| 562 | try: |
| 563 | path = urlparse(canonical).path |
| 564 | except ValueError: |
| 565 | path = "" |
| 566 | if "/jobs" in path: |
| 567 | candidate = path.split("/jobs", 1)[0].rstrip("/") |
| 568 | if ( |
| 569 | candidate |
| 570 | and candidate.startswith("/") |
| 571 | and not candidate.startswith("//") |
| 572 | and "\\" not in candidate |
| 573 | and not any(ch.isspace() or ord(ch) < 0x20 or ord(ch) == 0x7F for ch in candidate) |
| 574 | ): |
| 575 | prefix = candidate |
| 576 | return {"board": host, "jobs_path": f"{prefix}/jobs"} |
| 577 | |
| 578 | |
| 579 | def _talentbrew(html: str, host: str) -> dict[str, Any] | None: |
| 580 | if not host or not is_safe_public_url(f"https://{host}/") or "tbcdn.talentbrew.com" not in html: |
| 581 | return None |
| 582 | if "/search-jobs" not in html: |
| 583 | # A TalentBrew asset on a page that is not the job search itself. |
| 584 | return None |
| 585 | return {"board": host} |
| 586 | |
| 587 | |
| 588 | def kula_entry(slug: str) -> dict[str, Any]: |
| 589 | """The board entry of the Kula board under ``slug``.""" |
| 590 | return {"provider": PROVIDER_KULA, "board": KULA_HOST, "slug": slug} |
| 591 | |
| 592 | |
| 593 | def _kula(html: str, host: str) -> dict[str, Any] | None: |
| 594 | match = re.search(KULA_BOARD_LINK, html) |
| 595 | if not match: |
| 596 | return None |
| 597 | return {"board": KULA_HOST, "slug": match.group(1)} |
| 598 | |
| 599 | |
| 600 | def _same_site(host: str, other: str) -> bool: |
| 601 | """Whether two hosts belong to the same registrable site (``careers.adobe.com`` |
| 602 | and ``www.adobe.com``, without equating distinct tenants under ``*.co.uk`` or |
| 603 | multi-tenant suffixes like ``github.io``).""" |
| 604 | r1 = registrable_domain(host) |
| 605 | return bool(r1) and r1 == registrable_domain(other) |
| 606 | |
| 607 | |
| 608 | _DETECTORS = { |
| 609 | PROVIDER_ORACLE: _oracle, |
| 610 | PROVIDER_PHENOM: _phenom, |
| 611 | PROVIDER_EIGHTFOLD: _eightfold, |
| 612 | PROVIDER_JIBE: _jibe, |
| 613 | PROVIDER_TALENTBREW: _talentbrew, |
| 614 | PROVIDER_KULA: _kula, |
| 615 | } |
| 616 |