23
23
unresolver ,
24
24
)
25
25
from readthedocs .core .utils .extend import SettingsOverrideObject
26
+ from readthedocs .core .utils .requests import is_suspicious_request
26
27
from readthedocs .projects .constants import OLD_LANGUAGES_CODE_MAPPING , PRIVATE
27
28
from readthedocs .projects .models import Domain , Feature , HTMLFile
28
29
from readthedocs .projects .templatetags .projects_tags import sort_version_aware
@@ -497,13 +498,14 @@ def get(self, request, proxito_path):
497
498
# and we don't want to issue infinite redirects.
498
499
pass
499
500
500
- # Register 404 pages into our database for user's analytics
501
- self ._register_broken_link (
502
- project = project ,
503
- version = version ,
504
- path = filename ,
505
- full_path = proxito_path ,
506
- )
501
+ # Register 404 pages into our database for user's analytics.
502
+ if not unresolved_domain .is_from_external_domain :
503
+ self ._register_broken_link (
504
+ project = project ,
505
+ version = version ,
506
+ filename = filename ,
507
+ path = proxito_path ,
508
+ )
507
509
508
510
response = self ._get_custom_404_page (
509
511
request = request ,
@@ -521,33 +523,26 @@ def get(self, request, proxito_path):
521
523
path_not_found = proxito_path ,
522
524
)
523
525
524
- def _register_broken_link (self , project , version , path , full_path ):
526
+ def _register_broken_link (self , project , version , filename , path ):
525
527
try :
526
528
if not project .has_feature (Feature .RECORD_404_PAGE_VIEWS ):
527
529
return
528
530
529
- # This header is set from Cloudflare,
530
- # it goes from 0 to 100, 0 being low risk,
531
- # and values above 10 are bots/spammers.
532
- # https://developers.cloudflare.com/ruleset-engine/rules-language/fields/#dynamic-fields.
533
- threat_score = int (self .request .headers .get ("X-Cloudflare-Threat-Score" , 0 ))
534
- if threat_score > 10 :
531
+ if is_suspicious_request (self .request ):
535
532
log .info (
536
- "Suspicious threat score, not recording 404." ,
537
- threat_score = threat_score ,
533
+ "Suspicious request, not recording 404." ,
538
534
)
539
535
return
540
536
541
- # If the path isn't attached to a version
542
- # it should be the same as the full_path,
537
+ # If we don't have a version, the filename is the path,
543
538
# otherwise it would be empty.
544
539
if not version :
545
- path = full_path
540
+ filename = path
546
541
PageView .objects .register_page_view (
547
542
project = project ,
548
543
version = version ,
544
+ filename = filename ,
549
545
path = path ,
550
- full_path = full_path ,
551
546
status = 404 ,
552
547
)
553
548
except Exception :
@@ -556,7 +551,7 @@ def _register_broken_link(self, project, version, path, full_path):
556
551
log .exception (
557
552
"Error while recording the broken link" ,
558
553
project_slug = project .slug ,
559
- full_path = full_path ,
554
+ path = path ,
560
555
)
561
556
562
557
def _get_custom_404_page (self , request , project , version = None ):
0 commit comments