Closed Bug 476825 Opened 15 years ago Closed 15 years ago

TwitterFox extension evaluates JSON data in an unsafe way

Categories

(addons.mozilla.org :: Security, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: jwkbugzilla, Unassigned)

References

()

Details

Looking at the method onLoad() in the file components/nsTwitterNotifier.js, I see the following handling for JSON data received from twitter.com:

    case 200:
    case 304:
    default:
      var resp = null;
      if (!req.responseText.match(/^\s*$/)) {
        req.responseText = req.responseText.replace(/Couldn\'t find Status with ID=\d+,/, '');
        try {
          var resp = eval('(' + req.responseText + ')');
        }
        catch (e) {
          this.reportError("An error occurred while requesting " + req.__url);
          this.log("Response text: " + e.message);
          this.handleError(req, "Can't parse JSON. Twitter server responded with an error.");
        }
      }

At least all connections use HTTPS. Still, a malicious response from twitter.com (e.g. if the server is hacked) can take over user's browser. Similar JSON handling can also be found in the method rateLimitExceeded(). For
reference: https://developer.mozilla.org/en/JSON#Using_JSON
Developer has been emailed
Thanks, I'll taking care of it and release fixed version ASAP.
Is there any safe way to decode JSON in Firefox 2.x? Or should I drop to support Fx2?
Thanks for the quick response Kazuho. This may help:

https://developer.mozilla.org/En/Components.utils.evalInSandbox
You can also use the JSON parser script from http://json.org/js.html
I just want to make sure that this is correct to fix the issue.

I changed the code from:

    var resp = eval('(' + req.responseText + ')');

to:

    var s = Components.utils.Sandbox("https://twitter.com/");
    var resp = Components.utils.evalInSandbox(req.responseText, s);
This approach is a possibility but it won't prevent code execution, only move it to the sandbox. As a result, you will have to take quite some precautions when working with the resp variable, as outlined in the evalInSandbox documentation. I think using the code from JSON.jsm (correct link is http://mxr.mozilla.org/mozilla/source/js/src/xpconnect/loader/JSON.jsm, I changed it in the documentation as well) would be a better solution.
Thanks, these changes look good. There is another eval() call there but from what I can tell it is only used for data exchanged between different extension components.
This appears fixed
Status: NEW → RESOLVED
Closed: 15 years ago
Component: Administration → Add-on Security
Resolution: --- → FIXED
I think that this should be public by now.
Flags: needinfo?(jorge)
Group: client-services-security
Flags: needinfo?(jorge)
You need to log in before you can comment on or make changes to this bug.