Skip to content

Commit ca6d0b8

Browse files
committed
SameSite=Strict as default; closes #189
1 parent 48705a7 commit ca6d0b8

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

copyparty/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,7 @@ def add_stats(ap):
12881288
def add_yolo(ap):
12891289
ap2 = ap.add_argument_group('yolo options')
12901290
ap2.add_argument("--allow-csrf", action="store_true", help="disable csrf protections; let other domains/sites impersonate you through cross-site requests")
1291+
ap2.add_argument("--cookie-lax", action="store_true", help="allow cookies from other domains (if you follow a link from another website into your server, you will arrive logged-in); this reduces protection against CSRF")
12911292
ap2.add_argument("--getmod", action="store_true", help="permit ?move=[...] and ?delete as GET")
12921293
ap2.add_argument("--wo-up-readme", action="store_true", help="allow users with write-only access to upload logues and readmes without adding the _wo_ filename prefix (volflag=wo_up_readme)")
12931294

copyparty/httpcli.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2995,12 +2995,20 @@ def get_pwd_cookie(self, pwd: str) -> tuple[bool, str]:
29952995
# reset both plaintext and tls
29962996
# (only affects active tls cookies when tls)
29972997
for k in ("cppwd", "cppws") if self.is_https else ("cppwd",):
2998-
ck = gencookie(k, pwd, self.args.R, False)
2998+
ck = gencookie(k, pwd, self.args.R, self.args.cookie_lax, False)
29992999
self.out_headerlist.append(("Set-Cookie", ck))
30003000
self.out_headers.pop("Set-Cookie", None) # drop keepalive
30013001
else:
30023002
k = "cppws" if self.is_https else "cppwd"
3003-
ck = gencookie(k, pwd, self.args.R, self.is_https, dur, "; HttpOnly")
3003+
ck = gencookie(
3004+
k,
3005+
pwd,
3006+
self.args.R,
3007+
self.args.cookie_lax,
3008+
self.is_https,
3009+
dur,
3010+
"; HttpOnly",
3011+
)
30043012
self.out_headers["Set-Cookie"] = ck
30053013

30063014
return dur > 0, msg
@@ -5041,7 +5049,7 @@ def tx_mounts(self) -> bool:
50415049
def setck(self) -> bool:
50425050
k, v = self.uparam["setck"].split("=", 1)
50435051
t = 0 if v in ("", "x") else 86400 * 299
5044-
ck = gencookie(k, v, self.args.R, False, t)
5052+
ck = gencookie(k, v, self.args.R, self.args.cookie_lax, False, t)
50455053
self.out_headerlist.append(("Set-Cookie", ck))
50465054
if "cc" in self.ouparam:
50475055
self.redirect("", "?h#cc")
@@ -5053,7 +5061,7 @@ def set_cfg_reset(self) -> bool:
50535061
for k in ALL_COOKIES:
50545062
if k not in self.cookies:
50555063
continue
5056-
cookie = gencookie(k, "x", self.args.R, False)
5064+
cookie = gencookie(k, "x", self.args.R, self.args.cookie_lax, False)
50575065
self.out_headerlist.append(("Set-Cookie", cookie))
50585066

50595067
self.redirect("", "?h#cc")

copyparty/util.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,15 +2037,25 @@ def formatdate(ts: Optional[float] = None) -> str:
20372037
return RFC2822 % (WKDAYS[wd], d, MONTHS[mo - 1], y, h, mi, s)
20382038

20392039

2040-
def gencookie(k: str, v: str, r: str, tls: bool, dur: int = 0, txt: str = "") -> str:
2040+
def gencookie(
2041+
k: str, v: str, r: str, lax: bool, tls: bool, dur: int = 0, txt: str = ""
2042+
) -> str:
20412043
v = v.replace("%", "%25").replace(";", "%3B")
20422044
if dur:
20432045
exp = formatdate(time.time() + dur)
20442046
else:
20452047
exp = "Fri, 15 Aug 1997 01:00:00 GMT"
20462048

2047-
t = "%s=%s; Path=/%s; Expires=%s%s%s; SameSite=Lax"
2048-
return t % (k, v, r, exp, "; Secure" if tls else "", txt)
2049+
t = "%s=%s; Path=/%s; Expires=%s%s%s; SameSite=%s"
2050+
return t % (
2051+
k,
2052+
v,
2053+
r,
2054+
exp,
2055+
"; Secure" if tls else "",
2056+
txt,
2057+
"Lax" if lax else "Strict",
2058+
)
20492059

20502060

20512061
def humansize(sz: float, terse: bool = False) -> str:

tests/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class Cfg(Namespace):
143143
def __init__(self, a=None, v=None, c=None, **ka0):
144144
ka = {}
145145

146-
ex = "chpw daw dav_auth dav_mac dav_rt e2d e2ds e2dsa e2t e2ts e2tsr e2v e2vu e2vp early_ban ed emp exp force_js getmod grid gsel hardlink hardlink_only ih ihead magic nid nih no_acode no_athumb no_bauth no_clone no_cp no_dav no_db_ip no_del no_dirsz no_dupe no_lifetime no_logues no_mv no_pipe no_poll no_readme no_robots no_sb_md no_sb_lg no_scandir no_tail no_tarcmp no_thumb no_vthumb no_zip nrand nsort nw og og_no_head og_s_title ohead q rand re_dirsz rmagic rss smb srch_dbg srch_excl stats uqe vague_403 vc ver wo_up_readme write_uplog xdev xlink xvol zipmaxu zs"
146+
ex = "chpw cookie_lax daw dav_auth dav_mac dav_rt e2d e2ds e2dsa e2t e2ts e2tsr e2v e2vu e2vp early_ban ed emp exp force_js getmod grid gsel hardlink hardlink_only ih ihead magic nid nih no_acode no_athumb no_bauth no_clone no_cp no_dav no_db_ip no_del no_dirsz no_dupe no_lifetime no_logues no_mv no_pipe no_poll no_readme no_robots no_sb_md no_sb_lg no_scandir no_tail no_tarcmp no_thumb no_vthumb no_zip nrand nsort nw og og_no_head og_s_title ohead q rand re_dirsz rmagic rss smb srch_dbg srch_excl stats uqe vague_403 vc ver wo_up_readme write_uplog xdev xlink xvol zipmaxu zs"
147147
ka.update(**{k: False for k in ex.split()})
148148

149149
ex = "dav_inf dedup dotpart dotsrch hook_v no_dhash no_fastboot no_fpool no_htp no_rescan no_sendfile no_ses no_snap no_up_list no_voldump re_dhash see_dots plain_ip"

0 commit comments

Comments
 (0)