1

現在、オンラインデータベースに保存されているコードをロードする必要があるプロジェクト内の関数に取り組んでいます。コードは適度に信頼されている必要がありますが、完全に信頼されているわけではありません。

現在、実行しているコードが変更されていないことを確認するために、個別の非自明なテーブルのフィールドのmd5合計を使用していますが、コードをpysandboxでサンドボックス化することもできます(より良い提案も受け入れます)。この問題は、データベースから情報を取得するためにdjangoモデルを使用する必要がある場合に発生します。これは、モデルに触れようとする場合を除いて、pysandboxが常に使用するためです。

保護された実行者:

for hash in HashedChecksum.objects.all():
    if code_md5hash == hash.data:
        sandbox = Sandbox(SandboxConfig('stdout'))
        config.allowModule(contacts.models, Suppliers)
        namespace = {'query_dict': query_dict}
        sandbox.execute(code, locals=namespace)
        print namespace
    else:
        assert False

実行するコード:

print "code in sandbox"
try:
    import contacts.models
    print "hey"
    print contacts.models.Supplier.objects.all()  #line that excepts
except:
    print "Excepted"
print "Ended Execution"

例外出力の例:

try:
    import jmsdirectory.contacts.models
    print jmsdirectory.contacts.models.Supplier.objects.all()
except Exception as e:
    print e

例外出力:

5.5
SafeModule 'jmsdirectory' has no attribute 'contacts'
5.6
{'e': AttributeError("SafeModule 'jmsdirectory' has no attribute 'contacts'",), 'company': u'D5202F00-A8C7-11E1-B68A-00219B15530E', 'jmsdirectory': <SafeModule 'jmsdirectory' from 'C:\\Joinerysoft\\Development\\joinerysoft-directory\\jmsdirectory\\..\\jmsdirectory\\__init__.pyc'>, 'contact': u'D53D7B00-A8C7-11E1-A34B-00219B15530E', 'address': u'D53368E1-A8C7-11E1-AF74-00219B15530E', 'type': u'stays'}
run_code: 6
[08/Jun/2012 15:53:01] "POST /transfer/server/ HTTP/1.0" 200 129
4

0 に答える 0