-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use regexp to check not a brand #72
Conversation
The latest regexp is as follows.
Reference sourcehttps://wicg.github.io/ua-client-hints/#create-arbitrary-brands-section my implementation of itclass BrandVersion(object):
def __init__(self, brand, version):
self.brand = brand
self.version = version
def __str__(self):
return f"BrandVersion(x={self.brand}, y={self.version})"
def __repr__(self):
return f"BrandVersion(x={self.brand}, y={self.version})"
def toJson(self):
return {'brand': self.brand, 'version': self.version}
def get_greased_user_agent_brand_version(permuted_order=[], major_version='114', enable_updated_grease_by_policy=True, is_full_version=False):
if enable_updated_grease_by_policy:
greasey_chars = [" ", "(", ":", "-", ".", "/", ")", ";", "=", "?", "_"]
greased_versions = ["8", "99", "24"]
# https://wicg.github.io/ua-client-hints/#create-arbitrary-brands-section
greasey_brand = f"Not{greasey_chars[int(major_version) % len(greasey_chars)]}A{greasey_chars[(int(major_version) + 1) % len(greasey_chars)]}Brand"
greasey_version = greased_versions[int(major_version) % len(greased_versions)]
# https://github.com/chromium/chromium/blob/02e65feb53029473f796c1bc2bbbf214ea627688/components/embedder_support/user_agent_utils.cc#L406
# https://github.com/chromium/chromium/blob/02e65feb53029473f796c1bc2bbbf214ea627688/components/embedder_support/user_agent_utils_unittest.cc#L993
else:
greasey_version = "99"
greasey_chars = [" ", " ", ";"]
greasey_brand = f'{greasey_chars[permuted_order[0]]}Not{greasey_chars[permuted_order[1]]}A{greasey_chars[permuted_order[2]]}Brand'
if is_full_version:
greasey_version = f'{greasey_version}.0.0.0'
return BrandVersion(greasey_brand, greasey_version)
def generate_brand_version_list(major_version='114', brand='Microsoft Edge', full_version='114.0.5735.199', enable_updated_grease_by_policy=True, is_full_version=False):
if not is_full_version:
full_version = full_version.split('.')[0]
# seed = major_version
npermutations = 6
permutation = int(major_version) % npermutations
orders = [
[0, 1, 2], [0, 2, 1], [1, 0, 2],
[1, 2, 0], [2, 0, 1], [2, 1, 0]
]
order = orders[permutation]
greasey_bv = get_greased_user_agent_brand_version(permuted_order=order, major_version=major_version,
enable_updated_grease_by_policy=enable_updated_grease_by_policy, is_full_version=is_full_version)
chromium_bv = BrandVersion("Chromium", full_version)
greased_brand_version_list = [1, 2, 3]
if brand:
brand_bv = BrandVersion(brand, full_version)
greased_brand_version_list[order[0]] = greasey_bv.toJson()
greased_brand_version_list[order[1]] = chromium_bv.toJson()
greased_brand_version_list[order[2]] = brand_bv.toJson()
else:
greased_brand_version_list[int(major_version) % 2] = greasey_bv
greased_brand_version_list[(int(major_version) + 1) % 2] = chromium_bv
return greased_brand_version_list
def get_user_agent_brand_full_version_list(major_version='114',
full_version='114.0.5735.199', brand='Edge', enable_updated_grease_by_policy=True):
return generate_brand_version_list(major_version=major_version, brand=brand, full_version=full_version, enable_updated_grease_by_policy=enable_updated_grease_by_policy, is_full_version=True)
if __name__ == '__main__':
full_version = '109.0.5735.199'
major_version = full_version.split('.')[0]
brand = 'Microsoft Edge'
print(get_user_agent_brand_full_version_list(major_version=major_version, full_version=full_version, brand=brand))
print(generate_brand_version_list(major_version=major_version, full_version=full_version, brand=brand, is_full_version=False)) |
updated regex match pattern & fixed typo
I'm very sorry, the regex I wrote was not tested and there was an escape character error @kaliiiiiiiiii brands_pattern = r'^Not[ (:-./);=?]A[ (:-./);=?]Brand$' Should be changed to |
@anysoft mhh still get import re
brands_pattern = r'^Not[ (:-./);=?]A[ (:-./);=?]Brand$'
brand = 'Google Chrome'
re.match(brands_pattern, brand) |
` ` is my fault char - Escape character required |
Invalid escape character in github comment brans_pattern = r'^Not[ (:\-./);=?]A[ (:\-./);=?]Brand$' |
import re
brands_pattern = r'^Not[ (:\-./);=?]A[ (:\-./);=?]Brand$'
brand = 'Google Chrome'
print(re.match(brands_pattern, brand))
brand = 'Not.A/Brand'
print(re.match(brands_pattern, brand)) in github comment we need enter double \ 😂 |
@anysoft ohh forgot.. whatever, implemented now with with |
Thank you very much ❤️ |
If you have time to refer to this, just the useragent can automatically complete the brand data in the metadata Some security policies verify ua ch information in server |
Not.A/Brand
Not:A/Brand
Not=A?Brand
Not)A;Brand