sec: ADD exclusive-create to prevent colliisions #23
This commit is contained in:
parent
5727556344
commit
566c432601
1 changed files with 10 additions and 9 deletions
19
main.py
19
main.py
|
|
@ -282,19 +282,20 @@ async def upload_text(request: Request, authorized: bool = Depends(validate_uplo
|
||||||
reason="empty_content")
|
reason="empty_content")
|
||||||
raise HTTPException(status_code=400, detail="Empty content")
|
raise HTTPException(status_code=400, detail="Empty content")
|
||||||
|
|
||||||
random_path = generate_random_path()
|
|
||||||
while (UPLOAD_DIR / random_path).exists():
|
|
||||||
random_path = generate_random_path()
|
|
||||||
|
|
||||||
file_path = UPLOAD_DIR / random_path
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Generate deletion token
|
# Generate deletion token
|
||||||
deletion_token = generate_deletion_token()
|
deletion_token = generate_deletion_token()
|
||||||
|
|
||||||
# Save paste content
|
# Use O_CREAT|O_EXCL (mode 'x') so the kernel rejects collisions atomically
|
||||||
with open(file_path, 'w', encoding='utf-8') as f:
|
while True:
|
||||||
f.write(content)
|
random_path = generate_random_path()
|
||||||
|
file_path = UPLOAD_DIR / random_path
|
||||||
|
try:
|
||||||
|
with open(file_path, 'x', encoding='utf-8') as f:
|
||||||
|
f.write(content)
|
||||||
|
break
|
||||||
|
except FileExistsError:
|
||||||
|
continue
|
||||||
|
|
||||||
# Save metadata with deletion token
|
# Save metadata with deletion token
|
||||||
save_metadata(random_path, deletion_token, client_ip)
|
save_metadata(random_path, deletion_token, client_ip)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue