Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't put code outside of wrapper with new build system #354

Closed
Aradiv opened this issue Mar 5, 2020 · 16 comments · Fixed by #356
Closed

can't put code outside of wrapper with new build system #354

Aradiv opened this issue Mar 5, 2020 · 16 comments · Fixed by #356

Comments

@Aradiv
Copy link

Aradiv commented Mar 5, 2020

i need to place some code outside of wrapper() since i have various things running in GM sandbox that don't want to expose.

Functions that need to be accessible are placed inside wrapper everything else is outside wrapper

with the old build system i just placed the code before the @@PLUGINSTART@@ and could control where my code is running.

@johnd0e
Copy link
Contributor

johnd0e commented Mar 5, 2020

@Aradiv old system was not possible to use for own plugins at all

@Aradiv
Copy link
Author

Aradiv commented Mar 5, 2020

@johnd0e i used the old system for own plugins all the time

@johnd0e
Copy link
Contributor

johnd0e commented Mar 5, 2020

Ok, but I need sample of such code.

@Aradiv
Copy link
Author

Aradiv commented Mar 5, 2020

example script that will alert "newToken" every second refresh

redacted.user.js.txt

@johnd0e
Copy link
Contributor

johnd0e commented Mar 5, 2020

redacted.user.js.txt

Still, I do not see here secure way to operate data.
Yes,, you avoid saving it to localStorage. But now you have to expose some unsecure function to retrieve that data.

@Aradiv
Copy link
Author

Aradiv commented Mar 5, 2020

redacted.user.js.txt

Still, I do not see here secure way to operate data.
Yes,, you avoid saving it to localStorage. But now you have to expose some unsecure function to retrieve that data.

the token never needs to be retrieved outside the sandbox since all querys with the token are made inside of it. So unless you expose the getToken() function you can't retrieve the token outside of the plugin sandbox.

  • you cant call getToken
  • if you overwrite awesomeFunction you can't retrieve the token anymore

@johnd0e
Copy link
Contributor

johnd0e commented Mar 5, 2020

Let's begin from start: how do you put value into sandbox initially?
Is it just hardcoded like in redacted.user.js?

Then it is not more secure than if you just hardcodes it in some local variable.

@Aradiv
Copy link
Author

Aradiv commented Mar 5, 2020

you could just window.prompt it

@johnd0e
Copy link
Contributor

johnd0e commented Mar 5, 2020

@Aradiv OK, that makes sense.

But still most applications I can ever imagine would require data exposition, in one or another way.
E.g. earlier you suggested usage for tile layers apikeys.
But after you initialize L.TileLayer - everyone can see it's properties, even private ones.

Well, you can implement own leaflet class, that will keep apikey hidden. But again, in some point you should use it in web request, which can hijacked by anyone (on your machine).

Because of such their nature api keys are designed not to keep top-secret data.

@Aradiv
Copy link
Author

Aradiv commented Mar 5, 2020

a lot of the map providers provide the ability to create short lived read only limited access tokens when you have a long live one. so exposing the short lived one is okay (sometimes you can even ip bind it) but you should never expose the long lived ones.

@Aradiv
Copy link
Author

Aradiv commented Mar 5, 2020

and if you do your requests from inside the sandbox it is still not visible to any other plugin

@johnd0e
Copy link
Contributor

johnd0e commented Mar 5, 2020

@Aradiv Really? I would like to see real samples if you have them (or when you will have, in the future).

In general, I agree that there can be some limited application for GM sandbox. But it's not for wide use.

You see my related PR, do if you want — feel free to test it, fix it, and improve it.

@Aradiv
Copy link
Author

Aradiv commented Mar 5, 2020

yes this is only usefull for things that interact with third party services and maybe some operation critic information that you want to have specially protected.

@johnd0e
Copy link
Contributor

johnd0e commented Mar 7, 2020

This discussion made me think that may be we can greatly simplify our wrapper code.
Following sample does not use script injection (continued in #358):

// ==UserScript==
// @name            IITC plugin: [redacted] tiles
// @version         0.2.1
// @namespace       redacted
// @match           https://intel.ingress.com/*
// @grant           GM.getValue
// @grant           GM.setValue
// @grant           GM.deleteValue
// ==/UserScript==

window = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window;
// ensure plugin framework is there, even if iitc is not yet loaded
if(typeof window.plugin !== 'function') window.plugin = function() {};

const key = 'plugin-[redacted]-token';
function Token (action, token) {
    return GM[action + 'Value'](key, token);
}
function setup () {
    Token('get').then(token => {
        if(token === undefined){
            Token('set', "newToken");
        } else {
            Token('delete');
            alert(token);
      }
    });
};

var info = {};
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) info.script = { version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description };
var plugin_info = info;

setup.info = plugin_info; //add the script info data to the function as a property
if(!window.bootPlugins) window.bootPlugins = [];
window.bootPlugins.push(setup);
// if IITC has already booted, immediately run the 'setup' function
if(window.iitcLoaded && typeof setup === 'function') setup();

Update: this code does not actually work for GM

@johnd0e johnd0e linked a pull request Mar 7, 2020 that will close this issue
10 tasks
@Aradiv
Copy link
Author

Aradiv commented Mar 7, 2020

@johnd0e IMHO The window = unsafeWindow
is not really a good idea
Since you can't put code only in sandbox anymore.

Put except this it looks okay

@johnd0e
Copy link
Contributor

johnd0e commented Mar 7, 2020

Since you can't put code only in sandbox anymore.

Right. But we can fix that with #356

@johnd0e johnd0e removed a link to a pull request Mar 10, 2020
10 tasks
@johnd0e johnd0e mentioned this issue Mar 10, 2020
10 tasks
johnd0e pushed a commit that referenced this issue Apr 24, 2020
build_plugin.py: some code can be placed outside of wrapper

Close #354

To leave some code out of wrapper use special marker (on separate line):

```js
'this_is_unwrapped';
/*wrapped-from-here*/
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants