mirror of
				https://github.com/psycopg/psycopg2.git
				synced 2025-11-04 01:37:31 +03:00 
			
		
		
		
	Fixed refcount handling in encrypt_password
Added tests to check bad types, which discovered the above problem: on type error we would have decref'd on exit something that was only borrowed (because we wouldn't have performed matching increfs).
This commit is contained in:
		
							parent
							
								
									abca14d601
								
							
						
					
					
						commit
						9cf658ec6e
					
				| 
						 | 
					@ -418,16 +418,20 @@ psyco_encrypt_password(PyObject *self, PyObject *args, PyObject *kwargs)
 | 
				
			||||||
    PyObject *password = NULL, *user = NULL;
 | 
					    PyObject *password = NULL, *user = NULL;
 | 
				
			||||||
    PyObject *scope = Py_None, *algorithm = Py_None;
 | 
					    PyObject *scope = Py_None, *algorithm = Py_None;
 | 
				
			||||||
    PyObject *res = NULL;
 | 
					    PyObject *res = NULL;
 | 
				
			||||||
 | 
					    connectionObject *conn = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    static char *kwlist[] = {"password", "user", "scope", "algorithm", NULL};
 | 
					    static char *kwlist[] = {"password", "user", "scope", "algorithm", NULL};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    connectionObject *conn = NULL;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|OO", kwlist,
 | 
					    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|OO", kwlist,
 | 
				
			||||||
            &password, &user, &scope, &algorithm)) {
 | 
					            &password, &user, &scope, &algorithm)) {
 | 
				
			||||||
        return NULL;
 | 
					        return NULL;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* for ensure_bytes */
 | 
				
			||||||
 | 
					    Py_INCREF(user);
 | 
				
			||||||
 | 
					    Py_INCREF(password);
 | 
				
			||||||
 | 
					    Py_INCREF(algorithm);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (scope != Py_None) {
 | 
					    if (scope != Py_None) {
 | 
				
			||||||
        if (PyObject_TypeCheck(scope, &cursorType)) {
 | 
					        if (PyObject_TypeCheck(scope, &cursorType)) {
 | 
				
			||||||
            conn = ((cursorObject*)scope)->conn;
 | 
					            conn = ((cursorObject*)scope)->conn;
 | 
				
			||||||
| 
						 | 
					@ -437,16 +441,11 @@ psyco_encrypt_password(PyObject *self, PyObject *args, PyObject *kwargs)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else {
 | 
					        else {
 | 
				
			||||||
            PyErr_SetString(PyExc_TypeError,
 | 
					            PyErr_SetString(PyExc_TypeError,
 | 
				
			||||||
                    "the scope must be a connection or a cursor");
 | 
					                "the scope must be a connection or a cursor");
 | 
				
			||||||
            goto exit;
 | 
					            goto exit;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* for ensure_bytes */
 | 
					 | 
				
			||||||
    Py_INCREF(user);
 | 
					 | 
				
			||||||
    Py_INCREF(password);
 | 
					 | 
				
			||||||
    Py_INCREF(algorithm);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (!(user = psycopg_ensure_bytes(user))) { goto exit; }
 | 
					    if (!(user = psycopg_ensure_bytes(user))) { goto exit; }
 | 
				
			||||||
    if (!(password = psycopg_ensure_bytes(password))) { goto exit; }
 | 
					    if (!(password = psycopg_ensure_bytes(password))) { goto exit; }
 | 
				
			||||||
    if (algorithm != Py_None) {
 | 
					    if (algorithm != Py_None) {
 | 
				
			||||||
| 
						 | 
					@ -473,7 +472,7 @@ psyco_encrypt_password(PyObject *self, PyObject *args, PyObject *kwargs)
 | 
				
			||||||
            goto exit;
 | 
					            goto exit;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /* TODO: algo = will block: forbid on async/green conn? */
 | 
					        /* TODO: algo = None will block: forbid on async/green conn? */
 | 
				
			||||||
        encrypted = PQencryptPasswordConn(conn->pgconn,
 | 
					        encrypted = PQencryptPasswordConn(conn->pgconn,
 | 
				
			||||||
            Bytes_AS_STRING(password), Bytes_AS_STRING(user),
 | 
					            Bytes_AS_STRING(password), Bytes_AS_STRING(user),
 | 
				
			||||||
            algorithm != Py_None ? Bytes_AS_STRING(algorithm) : NULL);
 | 
					            algorithm != Py_None ? Bytes_AS_STRING(algorithm) : NULL);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1482,6 +1482,17 @@ class TestEncryptPassword(ConnectingTestCase):
 | 
				
			||||||
                password='psycopg2', user='ashesh',
 | 
					                password='psycopg2', user='ashesh',
 | 
				
			||||||
                scope=self.conn, algorithm='scram-sha-256')
 | 
					                scope=self.conn, algorithm='scram-sha-256')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_bad_types(self):
 | 
				
			||||||
 | 
					        self.assertRaises(TypeError, ext.encrypt_password)
 | 
				
			||||||
 | 
					        self.assertRaises(TypeError, ext.encrypt_password,
 | 
				
			||||||
 | 
					            'password', 42, self.conn, 'md5')
 | 
				
			||||||
 | 
					        self.assertRaises(TypeError, ext.encrypt_password,
 | 
				
			||||||
 | 
					            42, 'user', self.conn, 'md5')
 | 
				
			||||||
 | 
					        self.assertRaises(TypeError, ext.encrypt_password,
 | 
				
			||||||
 | 
					            42, 'user', 'wat', 'abc')
 | 
				
			||||||
 | 
					        self.assertRaises(TypeError, ext.encrypt_password,
 | 
				
			||||||
 | 
					            'password', 'user', 'wat', 42)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class AutocommitTests(ConnectingTestCase):
 | 
					class AutocommitTests(ConnectingTestCase):
 | 
				
			||||||
    def test_closed(self):
 | 
					    def test_closed(self):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user