command_line_args.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (c) 2018 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/app/command_line_args.h"
  5. #include <algorithm>
  6. #include <locale>
  7. #include "sandbox/policy/switches.h"
  8. #include "shell/common/options_switches.h"
  9. namespace {
  10. #if BUILDFLAG(IS_WIN)
  11. constexpr auto DashDash = base::CommandLine::StringPieceType{L"--"};
  12. #else
  13. constexpr auto DashDash = base::CommandLine::StringPieceType{"--"};
  14. #endif
  15. // we say it's a URL arg if it starts with a URI scheme that:
  16. // 1. starts with an alpha, and
  17. // 2. contains no spaces, and
  18. // 3. is longer than one char (to ensure it's not a Windows drive path)
  19. bool IsUrlArg(const base::CommandLine::StringPieceType arg) {
  20. const auto scheme_end = arg.find(':');
  21. if (scheme_end == base::CommandLine::StringPieceType::npos)
  22. return false;
  23. const auto& c_locale = std::locale::classic();
  24. const auto isspace = [&](auto ch) { return std::isspace(ch, c_locale); };
  25. const auto scheme = arg.substr(0U, scheme_end);
  26. return std::size(scheme) > 1U && std::isalpha(scheme.front(), c_locale) &&
  27. std::ranges::none_of(scheme, isspace);
  28. }
  29. } // namespace
  30. namespace electron {
  31. // Check for CVE-2018-1000006 issues. Return true iff argv looks safe.
  32. // Sample exploit: 'exodus://aaaaaaaaa" --gpu-launcher="cmd" --aaaaa='
  33. // Prevent it by returning false if any arg except '--' follows a URL arg.
  34. // More info at https://www.electronjs.org/blog/protocol-handler-fix
  35. bool CheckCommandLineArguments(const base::CommandLine::StringVector& argv) {
  36. bool block_args = false;
  37. for (const auto& arg : argv) {
  38. if (arg == DashDash)
  39. break;
  40. if (block_args)
  41. return false;
  42. if (IsUrlArg(arg))
  43. block_args = true;
  44. }
  45. return true;
  46. }
  47. bool IsSandboxEnabled(base::CommandLine* command_line) {
  48. return command_line->HasSwitch(switches::kEnableSandbox) ||
  49. !command_line->HasSwitch(sandbox::policy::switches::kNoSandbox);
  50. }
  51. } // namespace electron