File size: 4,007 Bytes
8247f19 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | # Patch-overlapping Python context before the fix
## `xhu.py` — `function:sanitized_join` (lines 42-46)
```python
def sanitized_join(path: str, root: pathlib.Path) -> pathlib.Path:
result = (root / path).absolute()
if not str(result).startswith(str(root) + "/"):
raise ValueError("resulting path is outside root")
return result
```
## `xhu.py` — `function:get_paths` (lines 49-53)
```python
def get_paths(base_path: pathlib.Path):
data_file = pathlib.Path(str(base_path) + ".data")
metadata_file = pathlib.Path(str(base_path) + ".meta")
return data_file, metadata_file
```
## `xhu.py` — `function:get_info` (lines 61-71)
```python
def get_info(path: str, root: pathlib.Path) -> typing.Tuple[
pathlib.Path,
dict]:
dest_path = sanitized_join(
path,
pathlib.Path(app.config["DATA_ROOT"]),
)
data_file, metadata_file = get_paths(dest_path)
return data_file, load_metadata(metadata_file)
```
## `xhu.py` — `function:put_file` (lines 104-170)
```python
def put_file(path):
try:
dest_path = sanitized_join(
path,
pathlib.Path(app.config["DATA_ROOT"]),
)
except ValueError:
return flask.Response(
"Not Found",
404,
mimetype="text/plain",
)
verification_key = flask.request.args.get("v", "")
length = int(flask.request.headers.get("Content-Length", 0))
hmac_input = "{} {}".format(path, length).encode("utf-8")
key = app.config["SECRET_KEY"]
mac = hmac.new(key, hmac_input, hashlib.sha256)
digest = mac.hexdigest()
if not hmac.compare_digest(digest, verification_key):
return flask.Response(
"Invalid verification key",
403,
mimetype="text/plain",
)
content_type = flask.request.headers.get(
"Content-Type",
"application/octet-stream",
)
dest_path.parent.mkdir(parents=True, exist_ok=True, mode=0o770)
data_file, metadata_file = get_paths(dest_path)
try:
with write_file(data_file) as fout:
stream_file(flask.request.stream, fout, length)
with metadata_file.open("x") as f:
json.dump(
{
"headers": {"Content-Type": content_type},
},
f,
)
except EOFError:
return flask.Response(
"Bad Request",
400,
mimetype="text/plain",
)
except OSError as exc:
if exc.errno == errno.EEXIST:
return flask.Response(
"Conflict",
409,
mimetype="text/plain",
)
raise
return flask.Response(
"Created",
201,
mimetype="text/plain",
)
```
## `xhu.py` — `function:head_file` (lines 189-211)
```python
def head_file(path):
try:
data_file, metadata = get_info(
path,
pathlib.Path(app.config["DATA_ROOT"])
)
stat = data_file.stat()
except (OSError, ValueError):
return flask.Response(
"Not Found",
404,
mimetype="text/plain",
)
response = flask.Response()
response.headers["Content-Length"] = str(stat.st_size)
generate_headers(
response.headers,
metadata["headers"],
)
return response
```
## `xhu.py` — `function:get_file` (lines 214-235)
```python
def get_file(path):
try:
data_file, metadata = get_info(
path,
pathlib.Path(app.config["DATA_ROOT"])
)
except (OSError, ValueError):
return flask.Response(
"Not Found",
404,
mimetype="text/plain",
)
response = flask.make_response(flask.send_file(
str(data_file),
))
generate_headers(
response.headers,
metadata["headers"],
)
return response
```
|