NavigatorID.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  3. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ByteString.h>
  8. #include <LibWeb/HTML/NavigatorID.h>
  9. #include <LibWeb/Loader/ResourceLoader.h>
  10. #include <LibWeb/Loader/UserAgent.h>
  11. namespace Web::HTML {
  12. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-appversion
  13. String NavigatorIDMixin::app_version() const
  14. {
  15. auto navigator_compatibility_mode = ResourceLoader::the().navigator_compatibility_mode();
  16. // Must return the appropriate string that starts with "5.0 (", as follows:
  17. // Let trail be the substring of default `User-Agent` value that follows the "Mozilla/" prefix.
  18. auto user_agent_string = ResourceLoader::the().user_agent();
  19. auto trail = MUST(user_agent_string.substring_from_byte_offset(strlen("Mozilla/"), user_agent_string.bytes().size() - strlen("Mozilla/")));
  20. // If the navigator compatibility mode is Chrome or WebKit
  21. if (navigator_compatibility_mode == NavigatorCompatibilityMode::Chrome || navigator_compatibility_mode == NavigatorCompatibilityMode::WebKit) {
  22. dbgln("Call to NavigatorIDMixin::app_version, using Chrome or WebKit compatibility mode");
  23. // Return trail.
  24. return trail;
  25. }
  26. // If the navigator compatibility mode is Gecko
  27. if (navigator_compatibility_mode == NavigatorCompatibilityMode::Gecko) {
  28. dbgln("Call to NavigatorIDMixin::app_version, using Gecko compatibility mode");
  29. // If trail starts with "5.0 (Windows", then return "5.0 (Windows)".
  30. if (trail.starts_with_bytes("5.0 (Windows"sv, CaseSensitivity::CaseSensitive))
  31. return "5.0 (Windows)"_string;
  32. // Otherwise, return the prefix of trail up to but not including the first U+003B (;), concatenated with the
  33. // character U+0029 RIGHT PARENTHESIS. For example, "5.0 (Macintosh)", "5.0 (Android 10)", or "5.0 (X11)".
  34. if (auto index = trail.find_byte_offset(';'); index.has_value()) {
  35. StringBuilder output;
  36. output.append(MUST(trail.substring_from_byte_offset(0, *index)));
  37. output.append(')');
  38. return MUST(output.to_string());
  39. }
  40. return trail;
  41. }
  42. VERIFY_NOT_REACHED();
  43. }
  44. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-platform
  45. String NavigatorIDMixin::platform() const
  46. {
  47. // Must return a string representing the platform on which the browser is executing (e.g. "MacIntel", "Win32",
  48. // "Linux x86_64", "Linux armv81") or, for privacy and compatibility, a string that is commonly returned on another
  49. // platform.
  50. // FIXME: Use some portion of the user agent string to make spoofing work 100%
  51. return ResourceLoader::the().platform();
  52. }
  53. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-productsub
  54. String NavigatorIDMixin::product_sub() const
  55. {
  56. auto navigator_compatibility_mode = ResourceLoader::the().navigator_compatibility_mode();
  57. // Must return the appropriate string from the following list:
  58. // If the navigator compatibility mode is Chrome or WebKit
  59. if (navigator_compatibility_mode == NavigatorCompatibilityMode::Chrome || navigator_compatibility_mode == NavigatorCompatibilityMode::WebKit) {
  60. dbgln("Call to NavigatorIDMixin::product_sub, using Chrome or WebKit compatibility mode");
  61. // The string "20030107".
  62. return "20030107"_string;
  63. }
  64. // If the navigator compatibility mode is Gecko
  65. if (navigator_compatibility_mode == NavigatorCompatibilityMode::Gecko) {
  66. dbgln("Call to NavigatorIDMixin::product_sub, using Gecko compatibility mode");
  67. // The string "20100101".
  68. return "20100101"_string;
  69. }
  70. VERIFY_NOT_REACHED();
  71. }
  72. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-useragent
  73. String NavigatorIDMixin::user_agent() const
  74. {
  75. // Must return the default `User-Agent` value.
  76. return ResourceLoader::the().user_agent();
  77. }
  78. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-vendor
  79. String NavigatorIDMixin::vendor() const
  80. {
  81. auto navigator_compatibility_mode = ResourceLoader::the().navigator_compatibility_mode();
  82. // Must return the appropriate string from the following list:
  83. // If the navigator compatibility mode is Chrome
  84. if (navigator_compatibility_mode == NavigatorCompatibilityMode::Chrome) {
  85. dbgln("Call to NavigatorIDMixin::vendor, using Chrome compatibility mode");
  86. // The string "Google Inc.".
  87. return "Google Inc."_string;
  88. }
  89. // If the navigator compatibility mode is Gecko
  90. if (navigator_compatibility_mode == NavigatorCompatibilityMode::Gecko) {
  91. dbgln("Call to NavigatorIDMixin::vendor, using Gecko compatibility mode");
  92. // The empty string.
  93. return ""_string;
  94. }
  95. // If the navigator compatibility mode is WebKit
  96. if (navigator_compatibility_mode == NavigatorCompatibilityMode::WebKit) {
  97. dbgln("Call to NavigatorIDMixin::vendor, using WebKit compatibility mode");
  98. // The string "Apple Computer, Inc.".
  99. return "Apple Computer, Inc."_string;
  100. }
  101. VERIFY_NOT_REACHED();
  102. }
  103. }