biblioteq icon indicating copy to clipboard operation
biblioteq copied to clipboard

Guard SQL queries.

Open textbrowser opened this issue 2 years ago • 32 comments

Difficult to correct because BQ is very mature. The goal is to prevent SQL queries from polluting the program. As an example, SELECT title FROM book, may result in a dead process if title contains many characters. With PostgreSQL, this is simpler to prevent because of permissions. Not impossible. With SQLite, this is easy. Replace a functional database file with an infected one.

One solution: SELECT SUBSTR(title, 1, 250) FROM book.

https://www.sqlite.org/limits.html#max_length

textbrowser avatar Mar 14 '22 14:03 textbrowser

I can't help you with sql but I don't think any author or publisher would choose a title of two hundred and fifty characters or more. There is no room on the cover and it is not attractive for a potential buyer. This text is 250 characters long :-)

meteos77 avatar Mar 17 '22 08:03 meteos77

on my book base the maximum found is 69 characters title : "QUE CROIS-TU QU'IL ARRIVA ? L'HISTOIRE DE MOUMINE, MUMLA ET PETITE MU"

SELECT title,LENGTH(title),book.myoid, book.type FROM book ORDER BY LENGTH(title) DESC or SELECT title,MAX(LENGTH(title)),book.myoid, book.type FROM book

meteos77 avatar Mar 17 '22 08:03 meteos77

title of the longest book in French isbn : 9782917084472 (sudoc) title : "My musician mother, died of a malignant disease at midnight, Tuesday to Wednesday, in the middle of May of the year 1997 at the Memorial Nursing Home in Manhattan"

result 146 characters if you have a minute look at the cover of the book ...

meteos77 avatar Mar 17 '22 12:03 meteos77

Consider:

sqlite> update book set keyword = (select keyword || keyword || keyword || keyword || keyword from book where myoid = 1) where myoid = 1; sqlite> select length(keyword) from book; 18408000

So 18 million characters. BQ is dead. :)

textbrowser avatar Mar 17 '22 13:03 textbrowser

This is a malicious attack that's impossible to prevent unless the query retrieves a reasonably-limited string.

textbrowser avatar Mar 17 '22 13:03 textbrowser

The query itself must be of the form SUBSTR(keyword, 1, N).

textbrowser avatar Mar 17 '22 13:03 textbrowser

sqlite> select length(keyword) from book; 575250000

Functional adaptation: "SUBSTR(keyword, 1, 2048) AS keyword".

textbrowser avatar Mar 17 '22 13:03 textbrowser

Do you think software in the open and proprietary world protects against this? Most likely not.

textbrowser avatar Mar 17 '22 13:03 textbrowser

With thousands and thousands of queries, yikes. Queries and sub-queries (WHERE keyword IN (...)), yikes again.

textbrowser avatar Mar 17 '22 13:03 textbrowser

Diagnostic tools die too.

textbrowser avatar Mar 17 '22 13:03 textbrowser

good luck then :-)

meteos77 avatar Mar 17 '22 13:03 meteos77

That is why this is a ticket for an expert and diligent person. It'll serve as a warning to future people. :)

textbrowser avatar Mar 17 '22 13:03 textbrowser

thank you for this reassuring information to make these malicious queries you have to use the BiblioteQ interface with the "Custom Database Query" feature ?

meteos77 avatar Mar 17 '22 13:03 meteos77

No.

cat /dev/urandom | od -h > output Copy some text. Then use sqlite3 tool to update a table entry. Open BQ, open the database, open the particular book and die.

textbrowser avatar Mar 17 '22 13:03 textbrowser

GIT dies too if you attempt to compare the database files.

textbrowser avatar Mar 17 '22 13:03 textbrowser

I'm glad I'm not networked at the library one less thing to worry about

meteos77 avatar Mar 17 '22 13:03 meteos77

I give some ideas, probably farfetched, but it's a fascinating subject :-) make an "imprint of the database" when leaving BiblioteQ and check it when opening BiblioteQ

meteos77 avatar Mar 17 '22 13:03 meteos77

Your SQLite file can be easily replaced.

textbrowser avatar Mar 17 '22 13:03 textbrowser

You have to monitor its size, its permissions, its dates.

textbrowser avatar Mar 17 '22 13:03 textbrowser

You mean a digest / fingerprint? That's possible with SQLite. Kind of nearly impossible with PQ.

textbrowser avatar Mar 17 '22 13:03 textbrowser

sure but with your example, if someone changes the file by sqlite3 and affects only one book and as long as we don't open the book nothing happens, what backup to take?

meteos77 avatar Mar 17 '22 13:03 meteos77

exemple : md5

meteos77 avatar Mar 17 '22 13:03 meteos77

It depends. They may not open that book. But what if they search for books whose keywords include some pattern? SQLite will search a text that has 500,000,000,000 characters.

textbrowser avatar Mar 17 '22 13:03 textbrowser

SQLITE version: at the beginning of your ticket, your link referred to the limit of sqlite, are not it effective?

meteos77 avatar Mar 17 '22 13:03 meteos77

Do you trust the operating system? Do you trust yourself remembering the fingerprint? Where does trust begin? The optimal solution (assuming that the executable is not changed) is to prevent the software from reading too much data.

So maybe 500 million characters. Instead of 500 billion.

"You can't comment at this time."

GH is dead.

textbrowser avatar Mar 17 '22 13:03 textbrowser

I trust you :-)

meteos77 avatar Mar 17 '22 14:03 meteos77

I trust you :-)

I don't trust myself. :P

textbrowser avatar Mar 17 '22 14:03 textbrowser

if it's lost then let's pretend it doesn't exist :-)

meteos77 avatar Mar 17 '22 14:03 meteos77

Of course, the queries have to be protected from themselves. There are probably join queries which return data that is fine but the joins are critically impacted because of sizes. It's a messy world.

textbrowser avatar Mar 17 '22 14:03 textbrowser

I don't know if this has anything to do with it but in my current software, I have a repair function for the database a document title has for example 1 million characters in its field in a control process BiblioteQ detects this anomaly and keeps only the first 250 characters

meteos77 avatar Mar 17 '22 14:03 meteos77