File tree Expand file tree Collapse file tree 2 files changed +20
-11
lines changed Expand file tree Collapse file tree 2 files changed +20
-11
lines changed Original file line number Diff line number Diff line change 25
25
26
26
27
27
def assert_path_is_inside_docroot (path ):
28
+ """Assert that the given path is inside the DOCROOT directory."""
29
+ assert_path_is_inside_expected_path (path , Path (settings .DOCROOT ).absolute ())
30
+
31
+
32
+ def assert_path_is_inside_expected_path (path , expected_path ):
28
33
"""
29
- Assert that the given path is inside the DOCROOT directory.
34
+ Assert that the given path is inside the expected path directory.
30
35
31
36
Symlinks are resolved before checking, a SuspiciousFileOperation exception
32
- will be raised if the path is outside the DOCROOT .
37
+ will be raised if the path is outside the expected path .
33
38
34
39
.. warning::
35
40
36
41
This operation isn't safe to TocTou (Time-of-check to Time-of-use) attacks.
37
42
Users shouldn't be able to change files while this operation is done.
38
43
"""
39
44
resolved_path = path .absolute ().resolve ()
40
- docroot = Path (settings .DOCROOT ).absolute ()
41
- if not path .is_relative_to (docroot ):
45
+ if not path .is_relative_to (expected_path ):
42
46
log .error (
43
- "Suspicious operation outside the docroot directory." ,
47
+ "Suspicious operation outside the expected path directory." ,
44
48
path_resolved = str (resolved_path ),
49
+ expected_path = str (expected_path ),
45
50
)
46
51
raise SuspiciousFileOperation (path )
47
52
Original file line number Diff line number Diff line change 10
10
import socket
11
11
import subprocess
12
12
from dataclasses import dataclass , field
13
+ from pathlib import Path
13
14
14
15
import structlog
15
16
from celery import Task
41
42
from readthedocs .builds .utils import memcache_lock
42
43
from readthedocs .config .config import BuildConfigV2
43
44
from readthedocs .config .exceptions import ConfigError
45
+ from readthedocs .core .utils .filesystem import assert_path_is_inside_expected_path
44
46
from readthedocs .doc_builder .director import BuildDirector
45
47
from readthedocs .doc_builder .environments import (
46
48
DockerBuildEnvironment ,
@@ -630,13 +632,15 @@ def get_valid_artifact_types(self):
630
632
631
633
# Rename file as "<project_slug>-<version_slug>.<artifact_type>",
632
634
# which is the filename that Proxito serves for offline formats.
633
- filename = list_dir [0 ]
634
- _ , extension = filename .rsplit ("." )
635
+ filename = os .path .join (artifact_directory , list_dir [0 ])
636
+ _ , extension = filename .rsplit ("." , maxsplit = 1 )
637
+ output = os .path .join (
638
+ self .data .project .checkout_path (self .data .version .slug ),
639
+ "_readthedocs/" ,
640
+ )
641
+ assert_path_is_inside_expected_path (Path (filename ), Path (output ))
635
642
shutil .move (
636
- os .path .join (
637
- artifact_directory ,
638
- list_dir [0 ],
639
- ),
643
+ filename ,
640
644
os .path .join (
641
645
artifact_directory ,
642
646
f"{ self .data .project .slug } .{ extension } " ,
You can’t perform that action at this time.
0 commit comments