NavigatorID.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // Return trail.
  23. return trail;
  24. }
  25. // If the navigator compatibility mode is Gecko
  26. if (navigator_compatibility_mode == NavigatorCompatibilityMode::Gecko) {
  27. // If trail starts with "5.0 (Windows", then return "5.0 (Windows)".
  28. if (trail.starts_with_bytes("5.0 (Windows"sv, CaseSensitivity::CaseSensitive))
  29. return "5.0 (Windows)"_string;
  30. // Otherwise, return the prefix of trail up to but not including the first U+003B (;), concatenated with the
  31. // character U+0029 RIGHT PARENTHESIS. For example, "5.0 (Macintosh)", "5.0 (Android 10)", or "5.0 (X11)".
  32. if (auto index = trail.find_byte_offset(';'); index.has_value()) {
  33. StringBuilder output;
  34. output.append(MUST(trail.substring_from_byte_offset(0, *index)));
  35. output.append(')');
  36. return MUST(output.to_string());
  37. }
  38. return trail;
  39. }
  40. VERIFY_NOT_REACHED();
  41. }
  42. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-platform
  43. String NavigatorIDMixin::platform() const
  44. {
  45. // Must return a string representing the platform on which the browser is executing (e.g. "MacIntel", "Win32",
  46. // "Linux x86_64", "Linux armv81") or, for privacy and compatibility, a string that is commonly returned on another
  47. // platform.
  48. // FIXME: Use some portion of the user agent string to make spoofing work 100%
  49. return ResourceLoader::the().platform();
  50. }
  51. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-productsub
  52. String NavigatorIDMixin::product_sub() const
  53. {
  54. auto navigator_compatibility_mode = ResourceLoader::the().navigator_compatibility_mode();
  55. // Must return the appropriate string from the following list:
  56. // If the navigator compatibility mode is Chrome or WebKit
  57. if (navigator_compatibility_mode == NavigatorCompatibilityMode::Chrome || navigator_compatibility_mode == NavigatorCompatibilityMode::WebKit) {
  58. // The string "20030107".
  59. return "20030107"_string;
  60. }
  61. // If the navigator compatibility mode is Gecko
  62. if (navigator_compatibility_mode == NavigatorCompatibilityMode::Gecko) {
  63. // The string "20100101".
  64. return "20100101"_string;
  65. }
  66. VERIFY_NOT_REACHED();
  67. }
  68. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-useragent
  69. String NavigatorIDMixin::user_agent() const
  70. {
  71. // Must return the default `User-Agent` value.
  72. return ResourceLoader::the().user_agent();
  73. }
  74. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-vendor
  75. String NavigatorIDMixin::vendor() const
  76. {
  77. auto navigator_compatibility_mode = ResourceLoader::the().navigator_compatibility_mode();
  78. // Must return the appropriate string from the following list:
  79. // If the navigator compatibility mode is Chrome
  80. if (navigator_compatibility_mode == NavigatorCompatibilityMode::Chrome) {
  81. // The string "Google Inc.".
  82. return "Google Inc."_string;
  83. }
  84. // If the navigator compatibility mode is Gecko
  85. if (navigator_compatibility_mode == NavigatorCompatibilityMode::Gecko) {
  86. // The empty string.
  87. return ""_string;
  88. }
  89. // If the navigator compatibility mode is WebKit
  90. if (navigator_compatibility_mode == NavigatorCompatibilityMode::WebKit) {
  91. // The string "Apple Computer, Inc.".
  92. return "Apple Computer, Inc."_string;
  93. }
  94. VERIFY_NOT_REACHED();
  95. }
  96. }