Skip to content

Commit

Permalink
added functionality to input script name
Browse files Browse the repository at this point in the history
Signed-off-by: aryangupta701 <garyan447@gmail.com>
  • Loading branch information
aryangupta701 committed Jul 21, 2023
1 parent 276c1c7 commit b97b4df
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 13 deletions.
6 changes: 3 additions & 3 deletions source/Background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ function sendZestScriptToZAP(
});
}

function handleMessage(
async function handleMessage(
request: MessageEvent,
zapurl: string,
zapkey: string
): boolean | ZestScriptMessage {
): Promise<boolean | ZestScriptMessage> {
if (request.type === 'zapDetails') {
console.log('ZAP Service worker updating the ZAP details');
Browser.storage.sync.set({
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion source/ContentScript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
}
});
}
}
);
Expand Down
33 changes: 31 additions & 2 deletions source/Popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
});
}
Expand All @@ -40,6 +40,8 @@ function restoreState(): void {
Browser.storage.sync
.get({
zaprecordingactive: false,
zapscriptname: 'recordedScript',
zapclosewindowhandle: false,
})
.then((items) => {
recordingActive = items.zaprecordingactive;
Expand All @@ -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;
});
}

Expand Down Expand Up @@ -126,13 +136,32 @@ 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);

recordButton?.addEventListener('click', toggleRecording);
configureButton?.addEventListener('click', openOptionsPage);
saveScript?.addEventListener('click', handleSaveScript);
scriptNameInput?.addEventListener('input', handleScriptNameChange);
windowHandleCloseInput?.addEventListener('click', handleWindowHandleClose);
28 changes: 27 additions & 1 deletion source/Popup/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
height: 80vh;
width: 120vh;
background-color: #f2f2f2;
}

Expand Down Expand Up @@ -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;
}
32 changes: 29 additions & 3 deletions source/types/zestScript/ZestScript.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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',
Expand All @@ -68,8 +89,13 @@ class ZestScript {
);
}

getZestScript(): ZestScriptMessage {
return {script: this.toJSON(), title: this.title};
getZestScript(): Promise<ZestScriptMessage> {
return new Promise((resolve) => {
Browser.storage.sync.get({zapscriptname: this.title}).then((items) => {
this.title = items.zapscriptname;
resolve({script: this.toJSON(), title: this.title});
});
});
}
}

Expand Down
19 changes: 19 additions & 0 deletions source/types/zestScript/ZestStatement.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
6 changes: 3 additions & 3 deletions test/ContentScript/unitTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions views/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<body>
<div class="container">
<h1 class="title">OWASP ZAP</h1>
<div>
<label id="script-name-label" for="script-name">Script Name:</label>
<input type="text" name="script-name" id="script-name-input" />
<label id="window-close-label" for="window-close">Close Window Handle:</label>
<input type="checkbox" name="window-close" id="window-close-input" />
</div>
<div class="content">
<button id="record-btn" class="button">Record</button>
<button id="configure-btn" class="button">Options</button>
Expand Down

0 comments on commit b97b4df

Please sign in to comment.