Skip to content
Closed
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
19 changes: 18 additions & 1 deletion packages/frontend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import App from "./views/App.vue";

import { ScanLauncher } from "@/components/launcher";
import { useLauncher } from "@/stores/launcher";
import { getSelectedRequestFromDOM } from "@/utils/utils";

export const init = (sdk: FrontendSDK) => {
const app = createApp(App);
Expand Down Expand Up @@ -80,6 +81,16 @@ export const init = (sdk: FrontendSDK) => {
sdk.window.showToast("No requests selected", { variant: "warning" });
return;
}
} else if (context.type === "BaseContext") {
const request = getSelectedRequestFromDOM();
if (request) {
requests.push(request);
} else {
sdk.window.showToast("No request editor active or selected", {
variant: "warning",
});
return;
}
} else {
sdk.window.showToast("No requests selected", { variant: "warning" });
return;
Expand All @@ -104,10 +115,11 @@ export const init = (sdk: FrontendSDK) => {

const launcherStore = useLauncher();
launcherStore.restart();
launcherStore.form.targets = requests.map((request) => ({
const targets = requests.map((request) => ({
...request,
method: "GET",
}));
launcherStore.form.targets = targets;

const dialog = sdk.window.showDialog(
{
Expand Down Expand Up @@ -135,10 +147,15 @@ export const init = (sdk: FrontendSDK) => {
if (context.type === "RequestContext") {
return context.request.type === "RequestFull";
}
if (context.type === "BaseContext") {
return true;
}
return false;
},
});

sdk.shortcuts.register("run-active-scanner", ["Control", "Shift", "S"]);

sdk.menu.registerItem({
type: "RequestRow",
commandId: "run-active-scanner",
Expand Down
86 changes: 86 additions & 0 deletions packages/frontend/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
export function getSelectedRequestFromDOM() {
const requestEditor = document.querySelector(
"[data-language='http-request']"
) as HTMLElement;

if (!requestEditor) {

Check failure on line 6 in packages/frontend/src/utils/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected object value in conditional. The condition is always true
return null;
}

const rawRequest = requestEditor.innerText;
const lines = rawRequest.split("\n");
const firstLine = lines[0]?.trim();

if (!firstLine) {

Check failure on line 14 in packages/frontend/src/utils/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected nullable string value in conditional. Please handle the nullish/empty cases explicitly
return null;
}

const parts = firstLine.split(" ");
const pathAndQuery = parts[1] || "/";

Check failure on line 19 in packages/frontend/src/utils/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected nullable string value in conditional. Please handle the nullish/empty cases explicitly
const [path] = pathAndQuery.includes("?")
? pathAndQuery.split("?")
: [pathAndQuery, ""];

const hostLine = lines.find((line) => line.toLowerCase().startsWith("host:"));
const hostFromRequest = hostLine

Check failure on line 25 in packages/frontend/src/utils/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected nullable string value in conditional. Please handle the nullish/empty cases explicitly
? hostLine.split(":").slice(1).join(":").trim()
: null;

let requestId: string | undefined;

if (hostFromRequest && path) {

Check failure on line 31 in packages/frontend/src/utils/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected nullable string value in conditional. Please handle the nullish/empty cases explicitly

Check failure on line 31 in packages/frontend/src/utils/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected nullable string value in conditional. Please handle the nullish/empty cases explicitly
const allRows = Array.from(document.querySelectorAll(".c-item-row"));

for (const row of allRows) {
const hostCell = row
.querySelector("[data-column-id='REQ_HOST']")
?.textContent?.trim();
const pathCell = row
.querySelector("[data-column-id='REQ_PATH']")
?.textContent?.trim();
const rowId = row.getAttribute("data-row-id");

if (hostCell?.includes(hostFromRequest) && pathCell === path && rowId) {

Check failure on line 43 in packages/frontend/src/utils/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected nullable string value in conditional. Please handle the nullish/empty cases explicitly

Check failure on line 43 in packages/frontend/src/utils/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected nullable boolean value in conditional. Please handle the nullish case explicitly
requestId = rowId;
break;
}
}
}

let url: string | null = null;

Check failure on line 50 in packages/frontend/src/utils/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Don't use `null` as a type. Use 'undefined' instead of 'null'

if (hostLine) {

Check failure on line 52 in packages/frontend/src/utils/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected nullable string value in conditional. Please handle the nullish/empty cases explicitly
const host = hostLine.split(":").slice(1).join(":").trim();
url = `https://${host}`;
} else {
return null;
}

try {
const urlObj = new URL(url);
const [finalPath, query = ""] = pathAndQuery.includes("?")
? pathAndQuery.split("?")
: [pathAndQuery, ""];

const hostMatch = rawRequest.match(/Host:\s*(.+?)(?::(\d+))?\r?\n/i);
const host = hostMatch?.[1]?.trim() || urlObj.hostname;
const port = hostMatch?.[2]
? parseInt(hostMatch[2])
: urlObj.port
? parseInt(urlObj.port)
: urlObj.protocol === "https:"
? 443
: 80;

return {
id: requestId || Date.now().toString(),
host,
port,
path: finalPath || "/",
query,
};
} catch {
return null;
}
}