janusgraph - store a password encrypted using bcrypt - node.js

I am using janusgraph with cassandra as storage backend. I am using a node package called as bcrypt to encrypt a password before saving it. The data type for that property in janusgraph is String. This is the password hash string which got generated - $2a$10$JSR6FClewTOHGxwpt/F0AePRzGnKvV2L9gj4TL1dA9fQERLWrig7u
This is the error I am getting while trying to save it in the db:
"message": "startup failed:\nScript88.groovy: 1: illegal string body character after dollar sign;\n solution: either escape a literal dollar sign \"\\$5\" or bracket the value expression \"${5}\" # line 1, column 228.\n elf_reg_ind\",\"2\",\"self_reg_pw\",\"$2a$10$J\n ^\n\n1 error\n",
"Exception-Class": "org.codehaus.groovy.control.MultipleCompilationErrorsException"
Please let me know if you need any other info.

The query you are passing to the server gets compiled with Groovy, and Groovy is attempting to resolve the $ as an identifier. You have a literal $ in your hash, so you need to put a \ in front of each $ to escape it. For example:
{ "gremlin":
"g.V(1234).property('hash', '\$2a\$10\$JSR6FClewTOHGxwpt/F0AePRzGnKvV2L9gj4TL1dA9fQERLWrig7u')"
}

Not a groovy guru myself, but I realised this evaluation is only attempted when using double quotes so I managed to solve this issue by ensuring that I use single quotes.
I'm using PHP so the process is to first json_encode then interchange double quotes for single quotes taking care of any already escaped quotes (if any) in between.

Related

Insert values with single quotes in a Postgres table column [duplicate]

I have a table test(id,name).
I need to insert values like: user's log, 'my user', customer's.
insert into test values (1,'user's log');
insert into test values (2,''my users'');
insert into test values (3,'customer's');
I am getting an error if I run any of the above statements.
If there is any method to do this correctly please share. I don't want any prepared statements.
Is it possible using sql escaping mechanism?
String literals
Escaping single quotes ' by doubling them up → '' is the standard way and works of course:
'user's log' -- incorrect syntax (unbalanced quote)
'user''s log'
Plain single quotes (ASCII / UTF-8 code 39), mind you, not backticks `, which have no special purpose in Postgres (unlike certain other RDBMS) and not double-quotes ", used for identifiers.
In old versions or if you still run with standard_conforming_strings = off or, generally, if you prepend your string with E to declare Posix escape string syntax, you can also escape with the backslash \:
E'user\'s log'
Backslash itself is escaped with another backslash. But that's generally not preferable.
If you have to deal with many single quotes or multiple layers of escaping, you can avoid quoting hell in PostgreSQL with dollar-quoted strings:
'escape '' with '''''
$$escape ' with ''$$
To further avoid confusion among dollar-quotes, add a unique token to each pair:
$token$escape ' with ''$token$
Which can be nested any number of levels:
$token2$Inner string: $token1$escape ' with ''$token1$ is nested$token2$
Pay attention if the $ character should have special meaning in your client software. You may have to escape it in addition. This is not the case with standard PostgreSQL clients like psql or pgAdmin.
That is all very useful for writing PL/pgSQL functions or ad-hoc SQL commands. It cannot alleviate the need to use prepared statements or some other method to safeguard against SQL injection in your application when user input is possible, though. #Craig's answer has more on that. More details:
SQL injection in Postgres functions vs prepared queries
Values inside Postgres
When dealing with values inside the database, there are a couple of useful functions to quote strings properly:
quote_literal() or quote_nullable() - the latter outputs the unquoted string NULL for null input.
There is also quote_ident() to double-quote strings where needed to get valid SQL identifiers.
format() with the format specifier %L is equivalent to quote_nullable().
Like: format('%L', string_var)
concat() or concat_ws() are typically no good for this purpose as those do not escape nested single quotes and backslashes.
According to PostgreSQL documentation (4.1.2.1. String Constants):
To include a single-quote character within a string constant, write
two adjacent single quotes, e.g. 'Dianne''s horse'.
See also the standard_conforming_strings parameter, which controls whether escaping with backslashes works.
This is so many worlds of bad, because your question implies that you probably have gaping SQL injection holes in your application.
You should be using parameterized statements. For Java, use PreparedStatement with placeholders. You say you don't want to use parameterised statements, but you don't explain why, and frankly it has to be a very good reason not to use them because they're the simplest, safest way to fix the problem you are trying to solve.
See Preventing SQL Injection in Java. Don't be Bobby's next victim.
There is no public function in PgJDBC for string quoting and escaping. That's partly because it might make it seem like a good idea.
There are built-in quoting functions quote_literal and quote_ident in PostgreSQL, but they are for PL/PgSQL functions that use EXECUTE. These days quote_literal is mostly obsoleted by EXECUTE ... USING, which is the parameterised version, because it's safer and easier. You cannot use them for the purpose you explain here, because they're server-side functions.
Imagine what happens if you get the value ');DROP SCHEMA public;-- from a malicious user. You'd produce:
insert into test values (1,'');DROP SCHEMA public;--');
which breaks down to two statements and a comment that gets ignored:
insert into test values (1,'');
DROP SCHEMA public;
--');
Whoops, there goes your database.
In postgresql if you want to insert values with ' in it then for this you have to give extra '
insert into test values (1,'user''s log');
insert into test values (2,'''my users''');
insert into test values (3,'customer''s');
you can use the postrgesql chr(int) function:
insert into test values (2,'|| chr(39)||'my users'||chr(39)||');
When I used Python to insert values into PostgreSQL, I also met the question: column "xxx" does not exist.
The I find the reason in wiki.postgresql:
PostgreSQL uses only single quotes for this (i.e. WHERE name = 'John'). Double quotes are used to quote system identifiers; field names, table names, etc. (i.e. WHERE "last name" = 'Smith').
MySQL uses ` (accent mark or backtick) to quote system identifiers, which is decidedly non-standard.
It means PostgreSQL can use only single quote for field names, table names, etc. So you can not use single quote in value.
My situation is: I want to insert values "the difference of it’s adj for sb and it's adj of sb" into PostgreSQL.
How I figure out this problem:
I replace ' with ’, and I replace " with '. Because PostgreSQL value does not support double quote.
So I think you can use following codes to insert values:
insert into test values (1,'user’s log');
insert into test values (2,'my users');
insert into test values (3,'customer’s');
If you need to get the work done inside Pg:
to_json(value)
https://www.postgresql.org/docs/9.3/static/functions-json.html#FUNCTIONS-JSON-TABLE
You must have to add an extra single quotes -> ' and make doubling quote them up like below examples -> ' ' is the standard way and works of course:
Wrong way: 'user's log'
Right way: 'user''s log'
problem:
insert into test values (1,'user's log');
insert into test values (2,''my users'');
insert into test values (3,'customer's');
Solutions:
insert into test values (1,'user''s log');
insert into test values (2,'''my users''');
insert into test values (3,'customer''s');

JSONFormat.print() method encoding special characters and also adding extra slash

I need to convert a protobuf message to JSON string in java. For this I am using the below API as recommended by the docs (https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/util/JsonFormat.Printer.html)
String jsonString = JsonFormat.printer().includingDefaultValueFields().print(protobufMessage);
This is working fine for a simple string, however, when my string contains special characters like &, single quote etc. the gson.toJson() method inside JsonFormat is converting special characters to octal format. For example "A&BC" is converted to "A\u0026BC". Also, the resultant string has an extra backslash appended.
So finally "A&BC" is converted to the string "A\\u0026BC".
If it were "A\u0026BC" then I could have converted to a byte array and formed a string with it. But because of the additional backslash I am not able to do so.
Currently I am using protobuf version 3.7.1 and I tried to upgrade and check if any latest API is available, but it did not help. I searched online but did not find any references (a similar issue was reported for JSONFormat.printToString but this API is removed in a later version. https://github.com/carlomedas/protobuf-java-format/issues/16). Can someone please help here if you have come across this issue.
I think the problem might be that you're using that string to pass along, and it's getting parsed a 2nd time. If you use the printer, it will convert "A&BC" to "A\u0026BC". Then when Jackson parses that, it will append the 2nd backslash. To avoid this, you can use #JsonRawValue annotation to avoid being parsed with the 2nd backslash.

SyntaxError in Jupypter notebook for msticpy QueryProvider

I'm making use of QueryProvider in msticpy.data.data_provider to run a Kusto query statement in Jupyter notebook.
The purpose of the query is to extract a specific part of a string that is typically session (other text), where I want to extract the (other text) - hence the extract function in Line 5.
As the content of the (other text) varies, I used the \w+ in the regex.
I can't execute the query successfully as it keeps complaining of syntax error. I have tried to escape the characters but it seems to have no effect because the same error appear. Would anyone have an idea what is the issue? Or point me to any resources?
Screenshot of current code and error returned
you need to escape the backslash (see: https://learn.microsoft.com/en-us/azure/kusto/query/scalar-data-types/string#string-literals)
regardless, you'd be better off using the parse operator
print s = "session abc"
| extend session = extract(#"session (\w+)", 1, s)
print s = "session abc"
| parse s with "session " session

Couchdb database name

I don't fully get the restrictions for database names on couchdb, when I try to create a name like !abcdef/user-context-81a5c7e396 I get the error
Create database failed: Name: '!abcdef/user-context-81a5c7e396'. Only
lowercase characters (a-z), digits (0-9), and any of the characters _,
$, (, ), +, -, and / are allowed. Must begin with a letter.
and pretty much it is because of the exclamation mark at the beginning, but I had no problem creating the database !abcdef/_users, so is something special about the _users?
I could remove the exclamation mark but this is from a project with many references to that name.
I'm using couchdb 2.2.0 and this behavior happened using curl and Fauxton
You are able to create the !abcdef/_users database due to a bug. So the fact that !abcdef/user-context-81a5c7e396 is failing is actually correct.
The bug has now been fixed, so this behavior should be corrected in CouchDB 2.3.0.

How to put triple qoutes around existing string variable?

Let's say I have this variable:
st='MI'
and I want to convert it to:
st=''' 'MI' '''
to use it in a SQL command.
What's the best way to accomplish this?
Thanks in advance!
Tripleor single quoting are just ways of typing strings into source code files.
Once your program is running, your string is already a string, and there is no need to make any cnversion to use it as a parameter to a SQL driver function call.
What you may want i to have a string with an SQL statement that itself contains various (single or double) quote characters. If that is typed in your Python source code file, you can type the triple-quote straight. If you are getting these SQL statements from elsewhere, they are already strings, as I said above.
Now, there are a few instances in which you have a string in a running Python program, or a Python interactive session, that you would like printed, so that you can paste it directly in source code. For these cases you can try the "unicode_escape" codec (and recode it to text so that it does not double your backslashes:
In [56]: print("\n".encode("unicode_escape").decode("utf-8"))
\n

Resources