Skip to content

Instantly share code, notes, and snippets.

@Shalelol
Last active January 24, 2020 07:03
Show Gist options
  • Save Shalelol/be2a1d1024c4858e8356047677e40b64 to your computer and use it in GitHub Desktop.
Save Shalelol/be2a1d1024c4858e8356047677e40b64 to your computer and use it in GitHub Desktop.
A javascript version of the pseudo code in Chromium's SameSite=None: Known Incompatible Clients https://www.chromium.org/updates/same-site/incompatible-clients
// Copyright 2019 Google LLC.
// SPDX-License-Identifier: Apache-2.0
// Don’t send `SameSite=None` to known incompatible clients.
function shouldSendSameSiteNone() {
return !isSameSiteNoneIncompatible ();
}
// Classes of browsers known to be incompatible.
function isSameSiteNoneIncompatible(useragent) {
return hasWebKitSameSiteBug(useragent)
|| dropsUnrecognizedSameSiteCookies(useragent);
}
function hasWebKitSameSiteBug(useragent) {
if (isIosVersion(useragent)) return true;
if (!isMacosxVersion(10, 14, useragent)) return false;
return isSafari(useragent)
|| isMacEmbeddedBrowser(useragent);
}
function dropsUnrecognizedSameSiteCookies(useragent) {
if (isUcBrowser(useragent))
return !isUcBrowserVersionAtLeast(12, 13, 2, useragent);
return isChromiumBased(useragent) &&
isChromiumVersionAtLeast(51, useragent) &&
!isChromiumVersionAtLeast(67, useragent);
}
// Regex parsing of User-Agent string. (See note above!)
function isIosVersion(major, useragent) {
var regex = /\(iP.+; CPU .*OS (\d+)[_\d]*.*\) AppleWebKit\//;
return useragent.matches(regex)[0] == major;
}
function isMacosxVersion(major, minor, useragent) {
var regex = /\(Macintosh;.*Mac OS X (\d+)_(\d+)[_\d]*.*\) AppleWebKit\//;
var matches = useragent.matches(regex);
return matches[0] == major && matches[1] == minor;
}
function isSafari(useragent) {
var regex = /Version\/.* Safari\//
return useragent.test(regex) && !isChromiumBased(useragent);
}
function isMacEmbeddedBrowser(useragent) {
var regex = /^Mozilla\/[\.\d]+ \(Macintosh;.*Mac OS X [_\d]+\) AppleWebKit\/[\.\d]+ \(KHTML, like Gecko\)$/
return useragent.test(regex);
}
function isChromiumBased (useragent) {
var regex = /Chrom(e|ium)/;
return useragent.test(regex);
}
function isChromiumVersionAtLeast(major, useragent) {
var regex = /Chrom[^ \/]+\/(\d+)[\.\d]* /;
var version = +useragent.matches(regex)[0];
return version >= major;
}
function isUcBrowser (useragent) {
var regex = /UCBrowser\//;
return useragent.test(regex);
}
function isUcBrowserVersionAtLeast (major, minor, build, useragent) {
var regex = /UCBrowser\/(\d+)\.(\d+)\.(\d+)[\.\d]*/;
var matches = useragent.match(regex);
// // Extract digits from three capturing groups.
var majorVersion = +matches[0];
var minorVersion = +matches[0];
var buildVersion = +matches[0];
if (majorVersion !== major)
return majorVersion > major;
if (minorVersion != minor)
return minorVersion > minor;
return buildVersion >= build;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment