Skip to content

Commit

Permalink
Fix code scanning alert no. 10: Incomplete URL substring sanitization
Browse files Browse the repository at this point in the history
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Oct 17, 2024
1 parent ef042e1 commit d30570c
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { setMenuItems } from "./menu";
import updateApp from "./update";
const isDevelopment = process.env.NODE_ENV !== "production";

const allowedDomains = ["aliyun.com", "qianwen.aliyun.com"];

const DEFAULT_USER_AGENT = ""; // Empty string to use the Electron default
/** @type {BrowserWindow} */
let mainWindow = null;
Expand Down Expand Up @@ -191,10 +193,7 @@ async function createWindow() {
newCookie.domain = cookie.domain;
}
// Handle the session cookie for QianWen
if (
cookie.domain.startsWith(".aliyun.com") ||
cookie.domain.startsWith("qianwen.aliyun.com")
) {
if (isAllowedDomain(cookie.domain)) {
newCookie.expirationDate = setCookieExpireDate(7);
}
await win.webContents.session.cookies.set(newCookie);
Expand All @@ -205,6 +204,22 @@ async function createWindow() {
},
);

function isAllowedDomain(domain) {
try {
const parsedHost = new URL(
`https://${domain.startsWith(".") ? domain.substring(1) : domain}`,
).host;
return allowedDomains.some(
(allowedDomain) =>
parsedHost === allowedDomain ||
parsedHost.endsWith(`.${allowedDomain}`),
);
} catch (error) {
console.error("Error parsing domain in isAllowedDomain:", domain, error);
return false;
}
}

// Modify the Referer header for each request and special patch for some sites.
win.webContents.session.webRequest.onBeforeSendHeaders(
(details, callback) => {
Expand Down

0 comments on commit d30570c

Please sign in to comment.