From b97b4dfa126f4c6d501ce6dea4ab3ba3e0189c3e Mon Sep 17 00:00:00 2001 From: aryangupta701 Date: Fri, 21 Jul 2023 00:16:18 +0530 Subject: [PATCH] added functionality to input script name Signed-off-by: aryangupta701 --- source/Background/index.ts | 6 ++--- source/ContentScript/index.ts | 6 ++++- source/Popup/index.tsx | 33 ++++++++++++++++++++++-- source/Popup/styles.scss | 28 +++++++++++++++++++- source/types/zestScript/ZestScript.ts | 32 ++++++++++++++++++++--- source/types/zestScript/ZestStatement.ts | 19 ++++++++++++++ test/ContentScript/unitTests.test.ts | 6 ++--- views/popup.html | 6 +++++ 8 files changed, 123 insertions(+), 13 deletions(-) diff --git a/source/Background/index.ts b/source/Background/index.ts index 0f48f28..f0de936 100644 --- a/source/Background/index.ts +++ b/source/Background/index.ts @@ -157,11 +157,11 @@ function sendZestScriptToZAP( }); } -function handleMessage( +async function handleMessage( request: MessageEvent, zapurl: string, zapkey: string -): boolean | ZestScriptMessage { +): Promise { if (request.type === 'zapDetails') { console.log('ZAP Service worker updating the ZAP details'); Browser.storage.sync.set({ @@ -241,7 +241,7 @@ async function onMessageHandler( zapurl: 'http://zap/', zapkey: 'not set', }); - const msg = handleMessage(message, items.zapurl, items.zapkey); + const msg = await handleMessage(message, items.zapurl, items.zapkey); if (!(typeof msg === 'boolean')) { val = msg; } diff --git a/source/ContentScript/index.ts b/source/ContentScript/index.ts index 6cf2fda..86decb3 100644 --- a/source/ContentScript/index.ts +++ b/source/ContentScript/index.ts @@ -256,7 +256,11 @@ Browser.runtime.onMessage.addListener( recordUserInteractions(); } else if (message.type === 'zapStopRecording') { stopRecordingUserInteractions(); - Browser.runtime.sendMessage({type: 'stopRecording'}); + Browser.storage.sync.get({zapclosewindowhandle: false}).then((items) => { + if (items.zapclosewindowhandle) { + Browser.runtime.sendMessage({type: 'stopRecording'}); + } + }); } } ); diff --git a/source/Popup/index.tsx b/source/Popup/index.tsx index 98bcde8..3acf228 100644 --- a/source/Popup/index.tsx +++ b/source/Popup/index.tsx @@ -25,11 +25,11 @@ let recordingActive = false; const RECORD = i18n.t('Record'); const STOP = i18n.t('Stop'); -function sendMessageToContentScript(message: string): void { +function sendMessageToContentScript(message: string, data = ''): void { Browser.tabs.query({active: true, currentWindow: true}).then((tabs) => { const activeTab = tabs[0]; if (activeTab?.id) { - Browser.tabs.sendMessage(activeTab.id, {type: message}); + Browser.tabs.sendMessage(activeTab.id, {type: message, data}); } }); } @@ -40,6 +40,8 @@ function restoreState(): void { Browser.storage.sync .get({ zaprecordingactive: false, + zapscriptname: 'recordedScript', + zapclosewindowhandle: false, }) .then((items) => { recordingActive = items.zaprecordingactive; @@ -49,6 +51,14 @@ function restoreState(): void { ) as HTMLButtonElement; recordButton.textContent = STOP; } + const scriptNameInput = document.getElementById( + 'script-name-input' + ) as HTMLInputElement; + scriptNameInput.value = items.zapscriptname; + const closeWindowHandle = document.getElementById( + 'window-close-input' + ) as HTMLInputElement; + closeWindowHandle.checked = items.zapclosewindowhandle; }); } @@ -126,9 +136,26 @@ function handleSaveScript(): void { }); } +function handleScriptNameChange(e: Event): void { + const {value} = e.target as HTMLInputElement; + Browser.storage.sync.set({ + zapscriptname: value, + }); + sendMessageToContentScript('updateTitle', value); +} + +function handleWindowHandleClose(e: Event): void { + const {checked} = e.target as HTMLInputElement; + Browser.storage.sync.set({ + zapclosewindowhandle: checked, + }); +} + const recordButton = document.getElementById('record-btn'); const configureButton = document.getElementById('configure-btn'); const saveScript = document.getElementById('save-script'); +const scriptNameInput = document.getElementById('script-name-input'); +const windowHandleCloseInput = document.getElementById('window-close-input'); document.addEventListener('DOMContentLoaded', restoreState); document.addEventListener('load', restoreState); @@ -136,3 +163,5 @@ document.addEventListener('load', restoreState); recordButton?.addEventListener('click', toggleRecording); configureButton?.addEventListener('click', openOptionsPage); saveScript?.addEventListener('click', handleSaveScript); +scriptNameInput?.addEventListener('input', handleScriptNameChange); +windowHandleCloseInput?.addEventListener('click', handleWindowHandleClose); diff --git a/source/Popup/styles.scss b/source/Popup/styles.scss index 0855ac3..aa93ee6 100644 --- a/source/Popup/styles.scss +++ b/source/Popup/styles.scss @@ -24,7 +24,8 @@ body { display: flex; justify-content: center; align-items: center; - height: 100vh; + height: 80vh; + width: 120vh; background-color: #f2f2f2; } @@ -60,4 +61,29 @@ body { .button:hover { background-color: #0056b3; +} + +#script-name-label , #window-close-label { + display: inline-block; /* Add this line */ + margin-bottom: 5px; + font-size: 16px; + font-weight: 700; +} + +#script-name-input { + display: inline-block; + padding: 2px; + border: 1px solid #737272; + border-radius: 3px; + font-size: 16px; + line-height: 16px; + height: 20px; + width: 50vh; +} + +#window-close-input { + display: inline-block; + padding: 2px; + border: 1px solid #737272; + border-radius: 3px; } \ No newline at end of file diff --git a/source/types/zestScript/ZestScript.ts b/source/types/zestScript/ZestScript.ts index a4488c3..f769dfe 100644 --- a/source/types/zestScript/ZestScript.ts +++ b/source/types/zestScript/ZestScript.ts @@ -1,3 +1,24 @@ +/* + * Zed Attack Proxy (ZAP) and its related source files. + * + * ZAP is an HTTP/HTTPS proxy for assessing web application security. + * + * Copyright 2023 The ZAP Development Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import Browser from 'webextension-polyfill'; + interface ZestScriptMessage { script: string; title: string; @@ -45,7 +66,7 @@ class ZestScript { about: 'This is a Zest script. For more details about Zest visit https://github.com/zaproxy/zest/', zestVersion: '0.3', - title: 'recordedScript', + title: this.title, description: '', prefix: '', type: 'StandAlone', @@ -68,8 +89,13 @@ class ZestScript { ); } - getZestScript(): ZestScriptMessage { - return {script: this.toJSON(), title: this.title}; + getZestScript(): Promise { + return new Promise((resolve) => { + Browser.storage.sync.get({zapscriptname: this.title}).then((items) => { + this.title = items.zapscriptname; + resolve({script: this.toJSON(), title: this.title}); + }); + }); } } diff --git a/source/types/zestScript/ZestStatement.ts b/source/types/zestScript/ZestStatement.ts index 1f3d7dc..21b8e63 100644 --- a/source/types/zestScript/ZestStatement.ts +++ b/source/types/zestScript/ZestStatement.ts @@ -1,3 +1,22 @@ +/* + * Zed Attack Proxy (ZAP) and its related source files. + * + * ZAP is an HTTP/HTTPS proxy for assessing web application security. + * + * Copyright 2023 The ZAP Development Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ class ElementLocator { type: string; diff --git a/test/ContentScript/unitTests.test.ts b/test/ContentScript/unitTests.test.ts index 1bae28d..dfa1e4f 100644 --- a/test/ContentScript/unitTests.test.ts +++ b/test/ContentScript/unitTests.test.ts @@ -282,7 +282,7 @@ test('Should Disable The Extension', async () => { }); test('should generate valid script', () => { - const script = new ZestScript(); + const script = new ZestScript('recordedScript'); const expectedOutcome = `{ "about": "This is a Zest script. For more details about Zest visit https://github.com/zaproxy/zest/", "zestVersion": "0.3", @@ -329,7 +329,7 @@ test('should generate valid send keys statement', () => { }); test('should add zest statement to zest script', () => { - const script = new ZestScript(); + const script = new ZestScript('recordedScript'); const elementLocator = new ElementLocator('id', 'test'); const zestStatementElementClick = new ZestStatementElementClick( elementLocator @@ -367,7 +367,7 @@ test('should add zest statement to zest script', () => { }); test('should reset zest script', () => { - const script = new ZestScript(); + const script = new ZestScript('recordedScript'); const elementLocator = new ElementLocator('id', 'test'); const zestStatementElementClick = new ZestStatementElementClick( elementLocator diff --git a/views/popup.html b/views/popup.html index bc913fe..b49b031 100644 --- a/views/popup.html +++ b/views/popup.html @@ -10,6 +10,12 @@

OWASP ZAP

+
+ + + + +