Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/inspector/dom_storage_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ protocol::DispatchResponse DOMStorageAgent::getDOMStorageItems(
std::optional<StorageMap> storage_map_fallback;
if (storage_map->empty()) {
auto web_storage_obj = getWebStorage(is_local_storage);
if (web_storage_obj) {
storage_map_fallback = web_storage_obj.value()->GetAll();
storage_map = &storage_map_fallback.value();
if (!web_storage_obj) {
return protocol::DispatchResponse::ServerError(
"Could not read DOM storage items");
}
storage_map_fallback = web_storage_obj.value()->GetAll();
storage_map = &storage_map_fallback.value();
}

auto result =
Expand Down
17 changes: 13 additions & 4 deletions src/inspector/storage_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ void StorageAgent::Wire(protocol::UberDispatcher* dispatcher) {
DispatchResponse StorageAgent::getStorageKey(
std::optional<protocol::String> frameId, protocol::String* storageKey) {
auto local_storage_file = env_->options()->localstorage_file;
*storageKey = node::url::FromFilePath(to_absolute_path(local_storage_file));
auto absolute_path = to_absolute_path(local_storage_file);
if (!absolute_path) {
return protocol::DispatchResponse::ServerError(
"Could not resolve the storage key path");
}
*storageKey = node::url::FromFilePath(*absolute_path);
return protocol::DispatchResponse::Success();
}

std::string StorageAgent::to_absolute_path(const std::filesystem::path& input) {
std::filesystem::path abs =
std::filesystem::weakly_canonical(std::filesystem::absolute(input));
std::optional<std::string> StorageAgent::to_absolute_path(
const std::filesystem::path& input) {
std::error_code error;
std::filesystem::path abs = std::filesystem::absolute(input, error);
if (error) return std::nullopt;
abs = std::filesystem::weakly_canonical(abs, error);
if (error) return std::nullopt;
return abs.generic_string();
}

Expand Down
6 changes: 5 additions & 1 deletion src/inspector/storage_agent.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef SRC_INSPECTOR_STORAGE_AGENT_H_
#define SRC_INSPECTOR_STORAGE_AGENT_H_

#include <filesystem>
#include <optional>
#include <string>
#include "env.h"
#include "node/inspector/protocol/Storage.h"

Expand All @@ -22,7 +25,8 @@ class StorageAgent : public protocol::Storage::Backend {
StorageAgent& operator=(const StorageAgent&) = delete;

private:
std::string to_absolute_path(const std::filesystem::path& input);
std::optional<std::string> to_absolute_path(
const std::filesystem::path& input);
std::unique_ptr<protocol::Storage::Frontend> frontend_;
Environment* env_;
};
Expand Down
58 changes: 58 additions & 0 deletions test/fixtures/test-inspector-dom-storage-unavailable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import '../common/index.mjs';
import assert from 'assert';
import { Session } from 'node:inspector/promises';

// getDOMStorageItems only looks at isLocalStorage, so the storage key is not
// needed to address a store. Storage.getStorageKey is deliberately not used
// here: without --localstorage-file it has no path to resolve, and what it
// does then differs between platforms.
const storageKey = '';

// Without --localstorage-file, globalThis.localStorage is undefined, so the
// agent cannot read the store. Reading its items must report an error instead
// of answering successfully with an empty list, which is indistinguishable
// from a store that exists and happens to be empty.
{
const session = new Session();
await session.connect();
await session.post('DOMStorage.enable');

await assert.rejects(
session.post('DOMStorage.getDOMStorageItems', {
storageId: {
isLocalStorage: true,
securityOrigin: '',
storageKey,
},
}),
{
code: 'ERR_INSPECTOR_COMMAND',
message: /Could not read DOM storage items/,
},
);

session.disconnect();
}

// sessionStorage is always backed by an in-memory store, so it stays readable
// and answers with an empty list until items are added.
{
const session = new Session();
await session.connect();
await session.post('DOMStorage.enable');

const storageId = { isLocalStorage: false, securityOrigin: '', storageKey };

const empty = await session.post('DOMStorage.getDOMStorageItems', {
storageId,
});
assert.deepStrictEqual(empty.entries, []);

sessionStorage.setItem('key', 'value');
const result = await session.post('DOMStorage.getDOMStorageItems', {
storageId,
});
assert.deepStrictEqual(result.entries, [['key', 'value']]);

session.disconnect();
}
6 changes: 6 additions & 0 deletions test/parallel/test-inspector-dom-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ spawnSyncAndExitWithoutError(process.execPath, [
'--localstorage-file=./localstorage.db',
fixtures.path('test-inspector-dom-storage.mjs'),
], { cwd: tmpdir.path });

spawnSyncAndExitWithoutError(process.execPath, [
'--inspect=0',
'--experimental-storage-inspection',
fixtures.path('test-inspector-dom-storage-unavailable.mjs'),
], { cwd: tmpdir.path });
Loading