Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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: 8 additions & 0 deletions bin/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ function getProgram() {
"--cookies-file <file>",
"specifies the file name to store the persistent Cookies"
)
.option(
"--local-storage <storage>",
'ability to set a local storage, key-value pairs (e.g. "bar=foo;domain=url")'
)
.option(
"--session-storage <storage>",
'ability to set a session storage, key-value pairs (e.g. "bar=foo;domain=url")'
)
.option(
"--ignore-ssl-errors",
"ignores SSL errors, such as expired or self-signed certificate errors"
Expand Down
82 changes: 82 additions & 0 deletions extensions/pageStorage/pageStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Support for session-/localStorage injection.
*/
"use strict";

module.exports = function (phantomas) {
const SESSION_STORAGE = "session-storage",
LOCAL_STORAGE = "local-storage";

phantomas.on("init", async (page) => {
let sessionStorage = phantomas.getParam(SESSION_STORAGE, false);
let localStorage = phantomas.getParam(LOCAL_STORAGE, false);

// Mapping given "storage-string" to json object if string has been given
if (typeof sessionStorage == "string" || sessionStorage instanceof String) {
sessionStorage = parseStorage(sessionStorage);
}

if (typeof localStorage == "string" || localStorage instanceof String) {
localStorage = parseStorage(localStorage);
}

if (sessionStorage) {
phantomas.log(
"Injecting sessionStorage: %j",
JSON.stringify(sessionStorage)
);
await injectStorage(page, sessionStorage, SESSION_STORAGE);
}
if (localStorage) {
phantomas.log("Injecting localStorag: %j", JSON.stringify(localStorage));
await injectStorage(page, localStorage, LOCAL_STORAGE);
}
});

function parseStorage(storageString) {
// --sessionStorage='bar=foo;domain=url'
// --localStorage='bar=fooLocal;domain=urlLocal'
var storageMap = {};
storageString.split(";").forEach(function (singleEntry) {
var entryKeyValue = singleEntry.split("=");
storageMap[entryKeyValue[0]] = entryKeyValue[1];
});
return storageMap;
}

/**
* Inject the given storage into the specified page storage.
* Either localStorage or sessionStorage
*
* @param {Page} page in which page the storage should be injected
* @param {Object} storage the JSON object consisting of the storage keys and values
* @param {string} storageType either localStorage or sessionStorage
*/
async function injectStorage(page, storage, storageType) {
if (!page || !storage || !storageType) {
return;
}

/* istanbul ignore next */
await page.evaluateOnNewDocument(
(storage, storageType, SESSION_STORAGE, LOCAL_STORAGE) => {
const keys = Object.keys(storage);
const values = Object.values(storage);
if (storageType === SESSION_STORAGE) {
for (let i = 0; i < keys.length; i++) {
sessionStorage.setItem(keys[i], values[i]);
}
}
if (storageType === LOCAL_STORAGE) {
for (let i = 0; i < keys.length; i++) {
localStorage.setItem(keys[i], values[i]);
}
}
},
storage,
storageType,
SESSION_STORAGE,
LOCAL_STORAGE
);
}
};
13 changes: 13 additions & 0 deletions test/integration-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,19 @@
localStorageEntries: 2
offenders:
localStorageEntries: ["foo", "test"]

# local/session storage injection
- url: "/local-storage-injection.html"
options:
"local-storage": "bar=foo"
metrics:
local-storage: '{"bar":"foo"}'
- url: "/session-storage-injection.html"
options:
"session-storage": "bar=foo"
metrics:
session-storage: '{"bar":"foo"}'

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

# JS errors
- url: "/js-errors.html"
metrics:
Expand Down
9 changes: 9 additions & 0 deletions test/webroot/local-storage-injection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<body>
<script>
console.log('Got local-storage: ' + JSON.stringify(window.localStorage));
// for unit tests
(function(phantomas) {
phantomas.setMetric('local-storage', JSON.stringify(window.localStorage));
})(window.__phantomas);
</script>
</body>
9 changes: 9 additions & 0 deletions test/webroot/session-storage-injection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<body>
<script>
console.log('Got session-storage: ' + JSON.stringify(sessionStorage));
// for unit tests
(function(phantomas) {
phantomas.setMetric('session-storage', JSON.stringify(sessionStorage));
})(window.__phantomas);
</script>
</body>