CVE-2015-10026 (Python, published 2023-01-07T13:15Z)
Description: value: A vulnerability was found in tiredtyrant flairbot. It has been declared as critical. This vulnerability affects unknown code of the file flair.py. The manipulation leads to sql injection. The patch is identified as 5e112b68c6faad1d4699d02c1ebbb7daf48ef8fb. It is recommended to apply a patch to fix this issue. VDB-217618 is the identifier assigned to this vulnerability.
Severity: nan (CVSS v3 base score: 9.8)
Weakness: CWE-89 \u2014 Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
The product constructs all or part of an SQL command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended SQL command when it is sent to a downstream component. Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data.
Repository: https://github.com/tiredtyrant/flairbot
Files changed:
flair.py
Commit message: Alteracoes nas queries para evitar sql injection

Signed-off-by: tiredtyrant <tiredtyrant@gmail.com>


Vulnerable code:
        query = 'SELECT id FROM paises WHERE nome == "%s"' % (msg)
        cursor.execute(query)
        query = 'SELECT estados.id FROM municipios JOIN estados ON municipios.estados_id == estados.id WHERE municipios.nome == "%s" AND estados.uf == "%s";' % (cidade, estado)
        cursor.execute(query)
        for msg in r.get_unread(limit=None):
            try:
                print 'AUTHOR: %s - SUBJECT: %s - BODY: %s' % (msg.author, msg.subject, msg.body)
            except UnicodeEncodeError:
                print 'AUTHOR: %s - unprintable chars' % (msg.author)
            sub = r.get_subreddit('brasil')
            if msg.subject == 'flair':
                if dbLookup(msg.body):
                    estado = 'world' if len(msg.body.split(',')) < 2 else msg.body.split(',')[1].strip()
                    sub.set_flair(msg.author,msg.body,estado)
                    r.send_message(msg.author,'flair','Flair configurado.')
                    print('flair ok')
                else:
                    r.send_message(msg.author,'flair','Configuração de flair falhou.')
                    print('flair fail')
            if msg.subject == 'remover flair':
                sub.set_flair(msg.author,'','')
                r.send_message(msg.author,'flair','Flair removido.')
                print('remove flair ok')
            msg.mark_as_read()
    

Fixed code:
        query = 'SELECT id FROM paises WHERE nome == ?;'
        cursor.execute(query,(msg,))
        query = 'SELECT estados.id FROM municipios JOIN estados ON municipios.estados_id == estados.id WHERE municipios.nome == ? AND estados.uf == ?;'
        cursor.execute(query,(cidade,estado))
        try:
            for msg in r.get_unread(limit=None):
                try:
                    print 'AUTHOR: %s - SUBJECT: %s - BODY: %s' % (msg.author, msg.subject, msg.body)
                except UnicodeEncodeError:
                    print 'AUTHOR: %s - unprintable chars' % (msg.author)
                sub = r.get_subreddit('brasil')
                if msg.subject == 'flair':
                    if dbLookup(msg.body):
                        estado = 'world' if len(msg.body.split(',')) < 2 else msg.body.split(',')[1].strip()
                        sub.set_flair(msg.author,msg.body,estado)
                        r.send_message(msg.author,'flair','Flair configurado.')
                        print('flair ok')
                    else:
                        r.send_message(msg.author,'flair','Configuração de flair falhou.')
                        print('flair fail')
                if msg.subject == 'remover flair':
                    sub.set_flair(msg.author,'','')
                    r.send_message(msg.author,'flair','Flair removido.')
                    print('remove flair ok')
                msg.mark_as_read()
        except:
            pass