1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112

ash / webui / demo_mode_app_ui / demo_mode_app_untrusted_ui_unittests.cc [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>

#include "ash/webui/demo_mode_app_ui/demo_mode_app_untrusted_ui.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/strcat.h"
#include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/url_util.h"

namespace ash {
namespace {

const std::string kFileContents = "Test File Contents";

class DemoModeAppUntrustedUITest : public testing::Test {
 protected:
  DemoModeAppUntrustedUITest() = default;
  ~DemoModeAppUntrustedUITest() override = default;

  void SetUp() override {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    base::File file = base::CreateAndOpenTemporaryFileInDir(
        temp_dir_.GetPath(), &content_file_path_);
    base::WriteFile(content_file_path_, kFileContents);

    scheme_registry_ = std::make_unique<url::ScopedSchemeRegistryForTests>();
    url::AddStandardScheme("chrome-untrusted", url::SCHEME_WITH_HOST);
  }

  base::FilePath content_file_path_;
  base::ScopedTempDir temp_dir_;
  std::unique_ptr<url::ScopedSchemeRegistryForTests> scheme_registry_;
  base::test::TaskEnvironment task_environment_;
};

void VerifyDataResponse(std::string expected_response,
                        base::OnceClosure quit_closure,
                        scoped_refptr<base::RefCountedMemory> data_response) {
  EXPECT_EQ(base::as_string_view(*data_response), expected_response);
  std::move(quit_closure).Run();
}

TEST_F(DemoModeAppUntrustedUITest, SourceDataFromComponent) {
  base::RunLoop run_loop;
  DemoModeAppUntrustedUI::SourceDataFromComponent(
      temp_dir_.GetPath(), content_file_path_.BaseName().MaybeAsASCII(),
      base::BindOnce(&VerifyDataResponse, kFileContents,
                     run_loop.QuitClosure()));
  run_loop.Run();
}

TEST_F(DemoModeAppUntrustedUITest, SourceDataFromComponentQueryParam) {
  base::RunLoop run_loop;
  std::string resource_path_with_query_param =
      content_file_path_.BaseName().MaybeAsASCII() + "?testparam=testvalue";

  DemoModeAppUntrustedUI::SourceDataFromComponent(
      temp_dir_.GetPath(), resource_path_with_query_param,
      base::BindOnce(&VerifyDataResponse, kFileContents,
                     run_loop.QuitClosure()));
  run_loop.Run();
}

TEST_F(DemoModeAppUntrustedUITest, SourceDataFromComponentURLFragment) {
  base::RunLoop run_loop;
  std::string resource_path_with_url_fragment =
      content_file_path_.BaseName().MaybeAsASCII() + "#frag";

  DemoModeAppUntrustedUI::SourceDataFromComponent(
      temp_dir_.GetPath(), resource_path_with_url_fragment,
      base::BindOnce(&VerifyDataResponse, kFileContents,
                     run_loop.QuitClosure()));
  run_loop.Run();
}

TEST_F(DemoModeAppUntrustedUITest,
       SourceDataFromComponentQueryParamAndURLFragment) {
  base::RunLoop run_loop;
  std::string resource_path_with_url_fragment =
      content_file_path_.BaseName().MaybeAsASCII() +
      "?testparam=testvalue#frag";

  DemoModeAppUntrustedUI::SourceDataFromComponent(
      temp_dir_.GetPath(), resource_path_with_url_fragment,
      base::BindOnce(&VerifyDataResponse, kFileContents,
                     run_loop.QuitClosure()));
  run_loop.Run();
}

TEST_F(DemoModeAppUntrustedUITest, SourceDataFromComponentParentDirReference) {
  base::RunLoop run_loop;
  // Treat temp_dir_ as the parent of the component directory here, that
  // a malicious ".."-containing path may be trying to access
  base::ScopedTempDir component_dir;
  ASSERT_TRUE(component_dir.CreateUniqueTempDirUnderPath(temp_dir_.GetPath()));
  std::string resource_path_with_parent_ref =
      "../" + content_file_path_.BaseName().MaybeAsASCII();

  DemoModeAppUntrustedUI::SourceDataFromComponent(
      component_dir.GetPath(), resource_path_with_parent_ref,
      base::BindOnce(&VerifyDataResponse, "", run_loop.QuitClosure()));
  run_loop.Run();
}

}  // namespace
}  // namespace ash