2

is there a way in selenium(and python, if it does matter) to find out which js set/access certain cookies?

Some of functionality i need is provided by OpenWPM. How do they do it?

Stefan Weiss
  • 461
  • 1
  • 6
  • 20
  • possible duplicate of https://stackoverflow.com/questions/15058462/how-to-save-and-load-cookies-using-python-selenium-webdriver – Prany Jun 08 '18 at 07:47
  • 1
    the questions have nothing similar. i know how to set and get cookies in selenium. i want to know which js set certain cookie – Stefan Weiss Jun 08 '18 at 07:51
  • I'm not very familiar with Selenium, but if it is possible to inject arbitrary JS into the page via it, then you add listeners for when the cookies are changed. – xrisk Jun 08 '18 at 07:56
  • @Rishav Link to example or more detailed answer would be great! – Stefan Weiss Jun 08 '18 at 07:57
  • Use [this](https://stackoverflow.com/q/31354352/4759361) to inject some JS that adds a [cookie listener](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/cookies/onChanged). – xrisk Jun 08 '18 at 08:03
  • @StefanWeiss Could you provide some logs or example on how OpenWPN saves javascript which set up cookies, please? I checked the repo and I even installed it on my PC but I can't see JS in the produced sqlite DB. – gbajson Jun 08 '18 at 08:20
  • @gbajson i found openWPN while i googled for my question. i did not install it – Stefan Weiss Jun 08 '18 at 08:27

1 Answers1

0

From what I have learned they (OpenWPM) check JS sources for the specific commands for storing cookies:

Have a look at test/test_extension.py:

DOCUMENT_COOKIE_WRITE = (
    JS_COOKIE_TEST_URL,
    u'7',
    u'9',
    u'set_cookie',
    u'',
    u'set_cookie@' + JS_COOKIE_TEST_URL + ':7:9'
    '\nonload@' + JS_COOKIE_TEST_URL + ':1:1',
    u'window.document.cookie', u'set',
    u'test_cookie=Test-0123456789; '
    'expires=Tue, 31 Dec 2030 00:00:00 UTC; path=/')

[...]

    def test_document_cookie_instrumentation(self):
        db = self.visit(utilities.BASE_TEST_URL + "/js_cookie.html")
        rows = db_utils.get_javascript_entries(db, all_columns=True)
        captured_cookie_calls = set()
        for row in rows:
            item = (row['script_url'], row['script_line'], row['script_col'],
                    row['func_name'], row['script_loc_eval'],
                    row['call_stack'], row['symbol'], row['operation'],
                    row['value'])
            captured_cookie_calls.add(item)
        assert captured_cookie_calls == DOCUMENT_COOKIE_READ_WRITE
gbajson
  • 1,531
  • 13
  • 32