0

我正在开发 Firefox 插件,需要安全地存储一些用户数据(没有其他扩展,网站应该可以访问它,并且数据应该只存储到浏览器会话关闭)。

你们中的任何人都可以就我应该使用的存储提出建议吗?

如果您认为将其存储为文件是个好主意,您能否指出如何在飞行中对其进行加密和解密的方向?

4

1 回答 1

0

Your JavaScript code and the JavaScript code from some other extension are the same - telling which one is which reliably will hardly be possible. However, if you can isolate the part of your code that needs to work with the data then you can simply use JavaScript's own mechanisms. In particular, closures can access variables that no other code has access to. An example:

function storeLoginData()
{
  var user = prompt("Please enter your user name");
  var pass = prompt("Please enter your password");
  return function(testUser, testPass)
  {
    return testUser == user && testPass == pass;
  };
}

var verifier = storeLoginData();
if (verifier("foo", "bar"))
  alert("Your login credentials are foo:bar");

Note that code outside the storeLoginData function can only call the closure returned but doesn't have access to its variables. In the example it is restricted to checking whether a user name/password combination is correct. And you can remove even this loophole if you make your isolated code act autonomously, on a timer or something like this:

function storeData(data)
{
  var timer = Components.classes["@mozilla.org/timer;1"]
                        .createInstance(Components.interfaces.nsITimer);
  timer.initWithCallback(function()
  {
    // Do something with data here
  }, 1000, timer.TYPE_ONE_SHOT);

  return function() {};
}

var data = ...;
var dataStorage = storeData(data);
data = null;

Note how the main code passes the data variable to storeData and nulls it out after that - all the references to the variable are now inside the storeData function. The closure returned here is useless and its only goal is to keep a reference to the timer variable - without that the timer will be garbage collected and will never fire. The security of this solution relies on the fact that there is no way to enumerate all timers in the system - this is true for timer objects but not event listeners or observers for example.

Of course, the data stays unencrypted in memory - that's hardly avoidable. Consequently, any binary code will still be able to read it, the protection works only against JavaScript.

于 2012-07-03T15:10:09.603 回答