Electron 是否将来自 mainWindow 的 cookie 存储在内存中或硬盘驱动器上的某个地方?如果是硬盘那么在哪里?

Does electron store cookies from the mainWindow in memory or on the hard drive somewhere? If the hard drive then where?

在处理 Electron 应用程序时,我意识到我存储在 cookie 中的身份验证令牌并未存储在其缓存文件中的 'Cookie' 文件中,缓存文件驻留在此处指定的路径中 post How to clear the cache data in Electron(atom shell)?.

由于我不知道 Electron 或 Chromium 的工作原理,我希望有人能帮我回答以下问题:

  1. Electron 将 "mainWindow"* 中的 cookie 存储在内存中还是硬盘文件中?如果它确实将其存储在硬盘驱动器上的哪个位置和哪个文件中? (我需要此信息来评估安全问题)
  2. 如果知道使用 Chromium 的 electron 存储的 cookie 是否以某种方式加密也很好。

*我所说的 mainWindow 是指当我通过电子代码在 window 中打开我的网络应用程序,然后允许用户登录时。我需要知道登录后存储的 cookie 在哪里在,被存储。

如果有人知道这方面的任何信息,请告诉我。

Does Electron store the cookies from the "mainWindow"* in the memory or in a file on the hard drive? If it does store it on the hard drive where and in which file exactly? (I need this info to evaluate a security issue)

在 Electron 中,Cookies 按会话存储。

假设您正在使用 BrowserWindowWebContents 会话来设置 cookie,如下所示:

const { BrowserWindow } = require('electron')

let win = new BrowserWindow({ width: 800, height: 600 })
win.loadURL('http://github.com')

const ses = win.webContents.session
const cookie = { url: 'http://www.github.com', name: 'dummy_name', value: 'dummy' }
await ses.cookies.set(cookie);

您的 cookie 应该在您的用户数据路径下可用,您可以通过 app.getPath('userData') API.

访问该路径

请注意,子目录取决于您使用的会话。例如,如果您使用 session.fromPartition('persist:your-part-name) API,则需要导航到 Partitions/your-part-name 文件夹。

It would also be nice to know if the cookie stored by electron which uses Chromium is encrypted in some way.

Chromium 中的 Cookie 确实是加密的 (see changelist)。 Electron 使用 Chromium 的实现。

我正在加载外部网页,下面的配置对我有用。默认情况下,该网页配置为使用“会话 cookie”,这就是我将其更改为有效期为 2 周的“持久性 cookie”的原因:

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
const util = require('util')

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 700,
    height: 500,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      partition: 'persist:infragistics'
    },
    icon: __dirname + '/assets/favicon.ico',
    show:false
  })



  let cookies = mainWindow.webContents.session.cookies;
  cookies.on('changed', function(event, cookie, cause, removed) {
    if (cookie.session && !removed) {
      let url = util.format('%s://%s%s', (!cookie.httpOnly && cookie.secure) ? 'https' : 'http', cookie.domain, cookie.path);
      console.log('url', url);
      cookies.set({
        url: url,
        name: cookie.name,
        value: cookie.value,
        domain: cookie.domain,
        path: cookie.path,
        secure: cookie.secure,
        httpOnly: cookie.httpOnly,
        expirationDate: new Date().setDate(new Date().getDate() + 14)
      }, function(err) {
        if (err) {
          log.error('Error trying to persist cookie', err, cookie);
        }
      });
    }
  });

注意:请务必确保您已设置 "partition" webPreferences property as well

A String that sets the session used by the page. If partition starts with persist:, the page will use a persistent session available to all pages in the app with the same partition. if there is no persist: prefix, the page will use an in-memory session

您可以检查您的 Chrome 申请部分是否设置了 cookie:

Origin source.