Skip to content

Commit

Permalink
[twitter] implement 'csrf' option (#2676)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Jun 13, 2022
1 parent 08db843 commit 9c8d895
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 13 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2271,6 +2271,19 @@ Description
<https://help.twitter.com/en/using-twitter/twitter-conversations>`__.


extractor.twitter.csrf
----------------------
Type
``string``
Default
``"cookies"``
Description
Controls how to handle Cross Site Request Forgery (CSRF) tokens.

* ``"auto"``: Always auto-generate a token.
* ``"cookies"``: Use token given by the ``ct0`` cookie if present.


extractor.twitter.size
----------------------
Type
Expand Down
7 changes: 5 additions & 2 deletions gallery_dl/extractor/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,11 @@ def __init__(self, extractor):
cookies = extractor.session.cookies
cookiedomain = extractor.cookiedomain

# CSRF
csrf_token = cookies.get("ct0", domain=cookiedomain)
csrf = extractor.config("csrf")
if csrf is None or csrf == "cookies":
csrf_token = cookies.get("ct0", domain=cookiedomain)
else:
csrf_token = None
if not csrf_token:
csrf_token = util.generate_token()
cookies.set("ct0", csrf_token, domain=cookiedomain)
Expand Down

0 comments on commit 9c8d895

Please sign in to comment.