ORA-01036: illegal variable name/number when running query through nodejs

user557657

I am running this simple query 'ALTER USER hr IDENTIFIED BY secret' using oracledb nodejs package. Not sure why I am getting illegal variable name error. Can I do something like this "ALTER USER :user IDENTIFIED BY :password";if yes then what will be correct syntax for me

function get(req, res, next) {
    oracledb.getConnection(
        config.database,
        function(err, connection){
            if (err) {
                return next(err);
            }

            connection.execute(
                'ALTER USER :user IDENTIFIED BY :password'+

            {
                user: req.body.user
            },
            {
                password: req.body.password
            },

                {
                    outFormat: oracledb.OBJECT
                }


          });
        }

Thanks for the help

Dan McGhan

As Matthew pointed out, you'll have to use string concatenation and protect against SQL injection.

To some degree, I question the need to do this. I know you're already aware of this post: https://jsao.io/2015/06/authentication-with-node-js-jwts-and-oracle-database/

Because that solution uses a table to store user credentials, rather than depending on database users, the values can be safely bound in without worrying about SQL injection. Why not use that approach instead?

Using database users with dynamic SQL requires that single quotes (') not be allowed in passwords. Frankly, I hate those kinds of restrictions. Password rules should exist to increase security (what must be included), not ensure that code can be executed safely (what's not allowed). That would not be an issue with a custom table.

However, just so you have one, here's an example solution that's based on promises that shows how to use dbms_assert to sanitize the values coming in:

const oracledb = require('oracledb');
const config = require('./dbConfig.js');

function get(req, res, next) {
  let conn;
  const user = req.body.user;
  const password = req.body.password; // Do not change case

  oracledb.getConnection(config)
    .then((c) => {
      conn = c;

      return conn.execute(
       `declare

          -- Use dbms_assert to sanitize values coming in to avoid SQL injection.
          l_user      varchar2(30) := dbms_assert.simple_sql_name(:user);
          l_password  varchar2(30) := dbms_assert.enquote_literal(:password);
          l_statement varchar2(100);

        begin

          -- Replace single quotes added by enquote_literal to left and right sides with double quotes
          l_password := '"' || substr(substr(l_password, 2, length(l_password)), 1, length(l_password) - 2) || '"';

          l_statement := 'alter user ' || l_user || ' identified by ' || l_password;

          execute immediate l_statement;

        end;`,
        {
          user: user,
          password: password
        }
      );
    })
    .then(result => {
      console.log('Password changed');

      // write to res
    })
    .catch(err => {
      console.log('Error changing password', err);

      next(err);
    })
    .then(() => {
      if (conn) { // conn assignment worked, need to close
        return conn.close();
      }
    })
    .catch(err => {
      console.log('Error during close', err);
    });
}

// Simulate run of 'get' function
get(
  {
    body: {
      user: 'movie_budget',
      password: 'N0rm@l-P@sswOrd!' // This value will throw an error: '\'\; drop table users;'
    }
  }, 
  {},
  function() {}
);

Finally, combining database logic with controller logic may lead to code that's difficult to maintain. Have a look at this recording for some tips on organizing things a little better: https://www.youtube.com/watch?v=hQgw2WmyuFM

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dapper and Oracle parametrized query - ORA-01036: illegal variable name/number

From Dev

Oracle ORA-01036 illegal variable name/number for no obvious reason

From Dev

cx_Oracle CREATE TABLE AS returns ORA-01036: illegal variable name/number

From Dev

ORA-01036: illegal variable name/number - oci_bind_by_name

From Dev

Python script to move data from a SQL server db to an oracle db keeps giving 'ORA-01036: illegal variable name/number'

From Dev

"Illegal variable name" error when running "docker stop $(docker ps -a -q)"

From Dev

Javascript ILLEGAL when generated through PHP

From Dev

"ORA-00937: not a single-group group function" when running my query

From Dev

"ORA-00937: not a single-group group function" when running my query

From Dev

Getting ORA-00933: SQL command not properly ended, when running the following query?

From Dev

How to express a BETWEEN statement in SQL when running query from a variable

From Dev

502 Bad Gateway when connecting to Nodejs app running express through Nginx

From Dev

Running through multiple functions in nodejs / expressjs

From Dev

ORA-01036 after rewriting an Oracle insert command

From Dev

SQL Syntax Error when running through PHP but runs fine as an SQL Query

From Dev

Running HIve query through impala.dbapi fails when embedding "Add Jar"

From Dev

(standard_in) illegal character when looping through a for loop

From Dev

MongoDB - running parallel queries with nodejs driver and close the db connection when the last query completes

From Dev

Illegal instruction error when running openMP in gfortran mac

From Dev

Custom Cursor Adapter Error When Running, Illegal Argument Exception

From Dev

Illegal instruction error when running openMP in gfortran mac

From Dev

Running my Oracle DB query through Jenkins

From Dev

Running hive query through the script by using crontab

From Dev

Nodejs - How to kill a running SQLite query

From Dev

Running Emacs through the value of the bash variable

From Dev

Illegal XML Characters When Declaring XML Variable In SQL

From Dev

ORA-01017 when connecting through jdbc thin driver

From Dev

ORA-01017 when connecting through jdbc thin driver

From Dev

PL/SQL Error ORA-01722 when looping through cursor

Related Related

  1. 1

    Dapper and Oracle parametrized query - ORA-01036: illegal variable name/number

  2. 2

    Oracle ORA-01036 illegal variable name/number for no obvious reason

  3. 3

    cx_Oracle CREATE TABLE AS returns ORA-01036: illegal variable name/number

  4. 4

    ORA-01036: illegal variable name/number - oci_bind_by_name

  5. 5

    Python script to move data from a SQL server db to an oracle db keeps giving 'ORA-01036: illegal variable name/number'

  6. 6

    "Illegal variable name" error when running "docker stop $(docker ps -a -q)"

  7. 7

    Javascript ILLEGAL when generated through PHP

  8. 8

    "ORA-00937: not a single-group group function" when running my query

  9. 9

    "ORA-00937: not a single-group group function" when running my query

  10. 10

    Getting ORA-00933: SQL command not properly ended, when running the following query?

  11. 11

    How to express a BETWEEN statement in SQL when running query from a variable

  12. 12

    502 Bad Gateway when connecting to Nodejs app running express through Nginx

  13. 13

    Running through multiple functions in nodejs / expressjs

  14. 14

    ORA-01036 after rewriting an Oracle insert command

  15. 15

    SQL Syntax Error when running through PHP but runs fine as an SQL Query

  16. 16

    Running HIve query through impala.dbapi fails when embedding "Add Jar"

  17. 17

    (standard_in) illegal character when looping through a for loop

  18. 18

    MongoDB - running parallel queries with nodejs driver and close the db connection when the last query completes

  19. 19

    Illegal instruction error when running openMP in gfortran mac

  20. 20

    Custom Cursor Adapter Error When Running, Illegal Argument Exception

  21. 21

    Illegal instruction error when running openMP in gfortran mac

  22. 22

    Running my Oracle DB query through Jenkins

  23. 23

    Running hive query through the script by using crontab

  24. 24

    Nodejs - How to kill a running SQLite query

  25. 25

    Running Emacs through the value of the bash variable

  26. 26

    Illegal XML Characters When Declaring XML Variable In SQL

  27. 27

    ORA-01017 when connecting through jdbc thin driver

  28. 28

    ORA-01017 when connecting through jdbc thin driver

  29. 29

    PL/SQL Error ORA-01722 when looping through cursor

HotTag

Archive