GithubHelp home page GithubHelp logo

Comments (3)

deadoverflow avatar deadoverflow commented on May 26, 2024

yeah based on the function you provided there is a code execution bug in the function read().
The eval function is a problem here because if you call read("value=='test' and print(7*7)") you would see 49 in the shell as well as some weird ass error but the code was executed!

from tinydb import TinyDB, Query

db = TinyDB('tests.json')
db.insert({
    'value': 'test'
})

def read(s:str):
    """ query jobid and return struct if exists, else it return an empty list """
    global db
    return db.search(eval(f"Query().{s}"))

print(read("value=='test' and print(7*7)"))

Here is the code that i have used and this is my console after i ran this:

image

Better way to write this read function is to add sanitization and filters so that it detects any malicious data. For example this is the example of safer read function:

from tinydb import TinyDB, Query

def read(s:str):
    global db
    obj = s.split('==')
    key = obj[0]
    value = obj[1]
    if '(' in value and ')' in value or '(' in key and ')' in key:
        return []
    else:
        return eval(f'db.search(Query().{key} == {value})')

This function is restricting any use of functions in the eval which is good enough i guess. Code still can be executed but there is nothing to worry about since no functions can be called. I hope this helps with you understand why the eval is bad and that your function had a deadly bug!

from tinydb.

fenchu avatar fenchu commented on May 26, 2024

Thanks I will use that, but I was hoping I could just write
db.search(Query()."jobid==6830997") without the eval like json.loads()

Maybe a future feature request :-)

Thanks again, solution works as a dream, we keep 10K backlog links here now.

from tinydb.

deadoverflow avatar deadoverflow commented on May 26, 2024

no problem fenchu, i am glad i was able to solve your problem!

Best regards, deadoverflow

from tinydb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.