【问题标题】:How to hide library source code in Google way?如何以 Google 方式隐藏库源代码?
【发布时间】:2012-08-08 13:11:50
【问题描述】:

例如,我有一个library,我想保护源代码不被查看。想到的第一个方法是为私有函数创建公共包装器,如下所示

function executeMyCoolFunction(param1, param2, param3) {
  return executeMyCoolFunction_(param1, param2, param3);
}

通过这种方式,只有代码的公共部分可见。这很好,但所有 Google 服务功能看起来都像 function abs() {/* */}。我很好奇,有没有像谷歌那样隐藏库源代码的方法?

Edit 00: 不要通过使用另一个库来“隐藏”库代码,即具有已知项目密钥的 LibA 使用具有未知项目密钥的 LibB。 LibB 的公共函数代码可以获取甚至执行。代码是

function exploreLib_(lib, libName) {
  if (libName == null) {
    for (var name in this) {
      if (this[name] == lib) {
        libName = name;
      }
    }
  }
  var res = [];
  for (var entity in lib) {
    var obj = lib[entity];
    var code;
    if (obj["toSource"] != null) {
      code = obj.toSource();
    }
    else if (obj["toString"] != null) {
      code = obj.toString();
    }
    else {
      var nextLibCode = exploreLib_(obj, libName + "." + entity);
      res = res.concat(nextLibCode);
    }
    if (code != null) {
      res.push({ libraryName: libName, functionCode: code });
    }
  }
  return res;
}

function explorerLibPublicFunctionsCode() {
  var lstPublicFunctions = exploreLib_(LibA);
  var password = LibA.LibB.getPassword();
}

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    编辑:更改了我的答案以反映异常的堆栈跟踪将包含库项目密钥这一事实。

    在此示例中,MyLibraryB 是 MyLibraryA 包含的库。两者都公开共享以供查看(访问控制),但只有 MyLibraryA 的项目密钥是已知的。攻击者似乎很难看到 MyLibraryB 中的代码:

    //this function is in your MyLibraryA, and you share its project key
    function executeMyCoolFunction(param1, param2, param3) {
      for (var i = 0; i < 1000000; i++) {
        debugger; //forces a breakpoint that the IDE cannot? step over
      }
      //... your code goes here
      //don't share MyLibraryB project key
      MyLibraryB.doSomething(args...); 
    }
    

    但根据@megabyte1024 的 cmets,如果您要在 MyLibraryB.doSomething() 中引发异常,堆栈跟踪将包含 MyLibraryB 的项目密钥。

    【讨论】:

    • 在我的原始帖子中,我写了关于公共库(项目密钥是已知的),开发人员向所有人发布这些库,例如 Notable Script Libraries。如果项目密钥已知,则可以查看公共功能代码。
    • 可以得到MyLibraryB的项目ID,如果制作代码,使用MyLibraryAMyLibraryB中抛出异常。如果异常代码类似于throw new Error('blah-blah-blah'),则捕获的异常将包含完整的堆栈和异常项目ID。
    • 玩了一下库脚本,我找到了一种在不知道其项目 ID 的情况下获取 MyLibraryB 的公共函数代码的方法。这很简单。
    • 彼得,It appears it would be very difficult for an attacker to see the code in MyLibraryB 的说法不正确。正如我在之前的评论中所写,很容易获得 MyLibraryB 代码。请参阅我的问题的Edit 00 段落。
    • 我同意,因此代码后面的短语...“但根据@metabyte 的 cmets...”。
    【解决方案2】:

    我不知道谷歌是做什么的,但你可以做这样的事情(未经测试!只是一个想法):

    函数声明:

    var myApp = {
      foo: function { /**/ },
      bar: function { /**/ }
    };
    

    然后,在另一个地方,一个匿名函数写入 foo() 和 bar():

    (function(a) {
      a['\u0066\u006F\u006F'] = function(){
        // here code for foo
      };
      a['\u0062\u0061\u0072'] = function(){
        // here code for bar
      };
    })(myApp);
    

    您可以打包或缩小以进一步混淆。

    【讨论】:

    • 不走运。我尝试了以下代码 - (function(inst) { inst.test3 = function() { return 3; } inst['test4'] = function() { return 4; } }(this)); function test3() { /**/ } function test4() { /**/ },但无论如何,“真正的”函数代码在调用代码中都是可见的。
    • 您可以混淆,但您的代码将始终可见。浏览器需要看到它。
    • 浏览器不需要查看任何 Google Apps 脚本(HTML 模板中的 JavaScript 代码除外),因为 GAS 是在 Google 服务器而不是客户端(浏览器)上执行的。 Google 隐藏了自己的脚本,并且有一个使用私有函数的解决方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    相关资源
    最近更新 更多