Skip to content

Commit

Permalink
Merge pull request #678 from adobe/fix-20240226
Browse files Browse the repository at this point in the history
Fix 20240226
  • Loading branch information
rofe authored Feb 29, 2024
2 parents 89a7b50 + 9c2268f commit aa43d26
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/extension/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@
"config_invalid_giturl": {
"message": "Invalid repository URL"
},
"config_invalid_host": {
"message": "Invalid hostname"
},
"config_invalid_import": {
"message": "Choose a valid file to import"
},
Expand Down
20 changes: 20 additions & 0 deletions src/extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,26 @@ const internalActions = {
chrome.contextMenus.onClicked.addListener(async ({ menuItemId }, tab) => {
if (!tab.url) return;
internalActions[menuItemId](tab);
await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: async (menuItemIdVal) => {
try {
const mod = await import(chrome.runtime.getURL('rum.js'));
const { default: sampleRUM } = mod;

// Ensure window.hlx and window.hlx.sidekick exists
window.hlx = window.hlx || {};
window.hlx.sidekick = window.hlx.sidekick || { location: window.location };

const checkpoint = `sidekick:context-menu:${menuItemIdVal}`;
sampleRUM(checkpoint, { source: window.location.href });
} catch (e) {
// eslint-disable-next-line no-console
console.log('Unable to collect RUM data', e);
}
},
args: [menuItemId],
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/extension/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ import sampleRUM from './rum.js';
const sp = new URLSearchParams(url.search);
const docPath = sp.get('id') || sp.get('RootFolder');
const dotIndex = docPath?.split('/').pop().indexOf('.');
return [-1, 0].includes(dotIndex); // dot only allowed as first char
return !docPath || [-1, 0].includes(dotIndex); // if doc path, dot can only be first char
}
return false;
}
Expand Down
28 changes: 24 additions & 4 deletions src/extension/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ function isValidGitHubURL(giturl) {
&& Object.keys(getGitHubSettings(giturl)).length === 3;
}

function isValidHost(host) {
const hostPattern = /^$|^([a-zA-Z0-9-._]*\.)*([a-zA-Z0-9-_]+\.)+[a-zA-Z]+$/;
return hostPattern.test(host);
}

function sanitize(str) {
return str.replace(/[$<>"'`=]/g, '-');
}
Expand All @@ -50,7 +55,7 @@ function drawLink(url) {
} catch (e) {
return ''; // not a valid url
}
if (!/^[a-z]+/.test(url.host)) {
if (!isValidHost(url.host)) {
return '';
}
const href = url.toString();
Expand Down Expand Up @@ -209,15 +214,30 @@ function editProject(i) {
}
};
const save = async () => {
const giturl = document.querySelector('#edit-giturl').value;
if (!isValidGitHubURL(giturl)) {
// eslint-disable-next-line no-alert
window.alert(i18n('config_invalid_giturl'));
return;
}
const host = document.querySelector('#edit-host').value;
const previewHost = document.querySelector('#edit-previewHost').value;
const liveHost = document.querySelector('#edit-liveHost').value;
// eslint-disable-next-line max-len
if (!isValidHost(host) || !isValidHost(previewHost) || !isValidHost(liveHost)) {
// eslint-disable-next-line no-alert
window.alert(i18n('config_invalid_host'));
return;
}
const input = {
giturl: document.querySelector('#edit-giturl').value,
mountpoints: [
document.querySelector('#edit-mountpoints').value,
],
project: document.querySelector('#edit-project').value,
previewHost: document.querySelector('#edit-previewHost').value,
liveHost: document.querySelector('#edit-liveHost').value,
host: document.querySelector('#edit-host').value,
previewHost,
liveHost,
host,
devMode: document.querySelector('#edit-devMode').checked,
devOrigin: document.querySelector('#edit-devOrigin').value,
};
Expand Down
23 changes: 23 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,29 @@ describe('Test sidekick', () => {
assert.ok((await test.run()).checkPageResult, 'Did not detect production URL');
}).timeout(IT_DEFAULT_TIMEOUT);

it('Detects admin environment correctly', async () => {
const setup = new Setup('blog');
nock.sidekick(setup, { persist: true });
nock.admin(setup, { persist: true });
const test = new SidekickTest({
browser,
page,
loadModule,
checkPage: async (p) => p.evaluate(() => window.hlx.sidekick.isAdmin()),
});
const urls = [
{ url: 'https://adobe.sharepoint.com/sites/test/Shared%20Documents/Forms/AllItems.aspx', res: true },
{ url: 'https://adobe.sharepoint.com/sites/test/Shared%20Documents/Forms/AllItems.aspx?id=%2Fsites%2Ftest%2FShared%20Documents%2Ffoo', res: true },
{ url: 'https://adobe.sharepoint.com/sites/test/Shared%20Documents/Forms/AllItems.aspx?RootFolder=%2Fsites%2Ftest%2FShared%20Documents%2Ffoo', res: true },
{ url: 'https://adobe.sharepoint.com/sites/test/Shared%20Documents/Forms/AllItems.aspx?id=%2Fsites%2Ftest%2FShared%20Documents%2F.helix', res: true },
{ url: 'https://adobe.sharepoint.com/sites/test/Shared%20Documents/Forms/AllItems.aspx?id=%2Fsites%2Ftest%2FShared%20Documents%2Ffoo%2Ftest.pdf', res: false },
];
for (const { url, res } of urls) {
// eslint-disable-next-line no-await-in-loop
assert.equal((await test.run(url)).checkPageResult, res, 'Did not detect admin URL');
}
}).timeout(IT_DEFAULT_TIMEOUT);

it('Switches to live instead of production without host', async () => {
const setup = new Setup('blog');
nock.sidekick(setup, {
Expand Down

0 comments on commit aa43d26

Please sign in to comment.