Пример #1
0
    def should_include(self, tag_type, tag_config):
        cookie_name = Tag.get_cookie_name(tag_type)
        cookie = self._cookies.get(cookie_name, None)

        if tag_config == "required":
            return True
        elif tag_config == "initial":
            if not cookie or cookie == "unset" or cookie == "true":
                return True
        else:
            if cookie == "true":
                return True
Пример #2
0
    def post(self, tag_type, tag_config):
        cookie_name = Tag.get_cookie_name(tag_type)
        cookie = self._cookies.get(cookie_name, None)

        if tag_config == "required":
            # Include required lazy tags
            # Include required cookie
            if self._consent is None:
                self._tags.append((Tag.LAZY_LOAD, tag_type))
            if cookie != "true":
                self.cookies[cookie_name] = "true"

        elif self._consent is None:
            if tag_config == "initial":
                if cookie == "unset":
                    # Include initial lazy tags
                    # Include initial instant tags
                    self._tags.append((Tag.LAZY_LOAD, tag_type))
                    self._tags.append((Tag.INSTANT_LOAD, tag_type))
                elif cookie == "true":
                    # Include initial lazy tags
                    self._tags.append((Tag.LAZY_LOAD, tag_type))
            else:
                if cookie == "true":
                    # Include generic lazy tags
                    self._tags.append((Tag.LAZY_LOAD, tag_type))

        elif self._consent is True:
            if tag_config == "initial":
                if cookie == "false":
                    # Include initial lazy tags
                    # Include initial instant tags
                    # Include initial cookie
                    self._tags.append((Tag.LAZY_LOAD, tag_type))
                    self._tags.append((Tag.INSTANT_LOAD, tag_type))
                self.cookies[cookie_name] = "true"
            else:
                if cookie == "true":
                    pass
                else:
                    # Include generic lazy tags
                    # Include generic instant tags
                    # Include generic cookie
                    self._tags.append((Tag.LAZY_LOAD, tag_type))
                    self._tags.append((Tag.INSTANT_LOAD, tag_type))
                    self.cookies[cookie_name] = "true"

        elif self._consent is False:
            self.cookies[cookie_name] = "false"
Пример #3
0
    def get(self, tag_type, tag_config):
        cookie_name = Tag.get_cookie_name(tag_type)
        cookie = self._cookies.get(cookie_name, None)

        if tag_config == "required":
            # Include required instant tags
            # Include required cookie
            self._tags.append((Tag.INSTANT_LOAD, tag_type))
            self.cookies[cookie_name] = "true"
        elif tag_config == "initial":
            if not cookie or cookie == "unset":
                # Include initial cookie
                self.cookies[cookie_name] = "unset"
            elif cookie == "true":
                # Include initial instant tags
                self._tags.append((Tag.INSTANT_LOAD, tag_type))
                self.cookies[cookie_name] = "true"
        else:
            if cookie == "true":
                # Include generic instant tags
                self._tags.append((Tag.INSTANT_LOAD, tag_type))
                self.cookies[cookie_name] = "true"
Пример #4
0
def scan_cookies(request):  # pragma: no cover
    def chop_microseconds(delta):
        return delta - timedelta(microseconds=delta.microseconds)

    try:
        options = webdriver.ChromeOptions()
        options.add_argument("headless")

        browser = webdriver.Chrome(options=options)
        browser.implicitly_wait(30)
        browser.get(request.site.root_page.full_url)
        browser.delete_all_cookies()
        for tag in Tag.get_types():
            browser.add_cookie(
                {
                    "name": Tag.get_cookie_name(tag),
                    "value": "true",
                    "path": "",
                    "secure": False,
                }
            )

        browser.get(request.site.root_page.full_url)
        now = datetime.utcnow()

        created = 0
        updated = 0

        for cookie in browser.get_cookies():
            expiry = datetime.fromtimestamp(cookie.get("expiry", now))

            obj, created = CookieDeclaration.objects.update_or_create(
                name=cookie.get("name"),
                domain=cookie.get("domain"),
                defaults={
                    "security": CookieDeclaration.INSECURE_COOKIE
                    if cookie.get("httpOnly")
                    else CookieDeclaration.SECURE_COOKIE,
                    "purpose": _("Unknown"),
                    "duration": chop_microseconds(expiry - now),
                },
            )

            if created:
                created = created + 1
            else:
                updated = updated + 1

        browser.quit()

        messages.success(
            request, _("Created %d declaration(s) and updated %d." % (created, updated))
        )
    except NotADirectoryError:
        messages.warning(
            request,
            mark_safe(
                _(
                    "Could not instantiate WebDriver session. Please ensure "
                    "<a href='http://chromedriver.chromium.org/' target='_blank' rel='noopener'>ChromeDriver</a> "
                    "is installed and available in your path."
                )
            ),
        )
    except Exception as e:
        messages.error(request, e)
Пример #5
0
 def cookie_state(self):
     return {
         tag_type:
         self.cookies.get(Tag.get_cookie_name(tag_type), "false") != "false"
         for tag_type in Tag.get_types()
     }