Security concerns, communication with database etc.
Posted: Thu Jun 11, 2020 1:50 pm
Hello colleagues,
Note: This post was also published in the German PureBasic Forum in the SpiderBasic section.
I'm not very experienced in programming with SpiderBasic, but I'm still planning to do a "medium-small" project with Spider. At the moment I'm still working with the demo version of Spidebasic to see if I can implement all parts of the planned project.
With some topics for the realization of the project I can't judge if my approach is ok or if it could be done better in another way.
What exactly is it about?
It's about a web application that should be protected by a login and that communicates via several PHP modules via HttpRequest() to interact with a database and the filesystem of the webserver.
So I have a program part that runs on the client browser and will be written in Spiderbasic (or after compiling in JavaScript). Here the whole GUI, its logic and the loading, processing and displaying of data is realized.
The other part of the program is on the server and is called by SpiderBasic via HttpRequest(). The parameters are transferred by POST.
With the following points I do not know yet what approach is the best:
1) the topic login and authorization to use the SpiderBasic application at all.
If I would write the web application in pure PHP/HTML, I would program a login screen and store the user's permissions in a session variable after a successful login. When calling other PHP pages that belong to the application, I would read the session variable with the permission level and react accordingly in the PHP script (either continue the script or abort it with a error message if the permission is not sufficient).
But how do I do that within SpiderBasic?
a) I could implement the login screen directly in the SpiderBasic application, so that I can decide directly in SpiderBasic whether the user is allowed to continue working with the application or is kicked out immediately.
Now SpiderBasic is finally JavaScript and runs on the user's browser. Wouldn't it be possible for the user to analyze and modify the JavaScript code to bypass the login check? That would be a security risk. Unfortunately I am not very familiar with JavaScript.
b) I could implement the login screen in PHP, then directly fill the session variables with the user's authorization and, if the login was successful, forward them to the SpiderBasic application.
Here a similar problem arises nevertheless. The SpiderBasic application is started by calling the corresponding *.html file. The URL of the SpiderBasic application can then be seen either directly in the URL gadget of the browser or by using the browser's developer tools. If you know this URL, you could also call it directly without the login process. Then the login check would be completely bypassed.
b2) I could send a query from the SpiderBasic application to the web server to see if the current user has the appropriate permission (HttpRequest() to a PHP script that handles authentication). But again: JavaScript runs on the browser and I don't know if this call or its return value could be compromised by an attacker.
c) For additional security I could proceed as described under b2) and have the PHP authentication module return a token if the authorization was positive. This token would then have to be sent with all communication with the web server. This means every time the SpiderBasic application sends requests to the web server to acces the file system or database, it sends also this token. Only if the token sent is identical to the token of the successfully logged on user, the request is executed.
Thus, an unauthorized user could see and partially use the GUI of the SpiderBasic application, but everything related to server communication would fail.
Whether all this is then sufficiently secure, I cannot judge.
I would be very happy to hear from experts on this topic.
2) Passing parameters to a Spiderbasic application.
You can pass parameters to a PHP script via POST or GET. You can also pass parameters to a PureBasic program, which you can get with ProgramParameter().
For SpiderBasic I haven't found anything like this yet.
It might be useful with regard to the login topic, if I could tell the SpiderBasic application that the user has sufficient permissions when it is called.
3) Secure database queries
Again I am afraid that the JavaScript code can be changed to do mischief or to get unauthorized access to data.
This is about the secure coding of database queries in Spiderbasic.
The whole thing is realized by calling the HttpRequest() function of a PHP script, which then takes over the actual communication with the database and returns the result of the query to the SpiderBasic application.
Probably such a module has been developed a thousand times (CRUD module), but unfortunately I'm not further into the topic.
a) From a logical common sense point of view, I would say that it is highly dangerous to keep the entire SQL query inside the SpiderBasic application and send it to the PHP module. For example "Select id_user From users Where name = "Smith" and PasswordHash = "blahblah". An attacker could certainly manipulate the query to cause damage to the database.
b) One could abstract the functionality.
The PHP module would get the following parameters:
- Command (a flag indicating whether a select, delete, update or insert should be performed)
- Table (Indicator which specifies which table is mined - The indicator must not be the same as the table name!)
- Values (Which data fields should be returned)
- Clause (restrictions, i.e. the where clause of the SQL statement)
The whole thing is probably just as insecure as a), except that an attacker have to think about it a little longer.
c) You could put the entire function and the entire SQL into the PHP module and program all the commands or queries that the SpiderBasic program needs.
Then you would only send the following to the PHP module:
- Command (specifies what is required)
- Values[] (the values to be entered in the table during an Update or Insert Command)
The PHP module would then unfortunately have to contain a lot of redundant code, because a separate function would have to be written for each command.
For example:
Command = "CHECKUSER"
Value1 = "Smith"
Value2 = "blahblah"
-> Check if the user with the name "Smith" and the password hash "blahblah" exists in the table user and has the permission to use the application. Returns 0/1 if so.
Command = "GETPRODUCT"
Value1 = "1309"
-> Return the record of the product with the ID 1309 from the table products. The transfer to Spider then as JSON string.
Command = "UPDATEUSEREMAIL"
Value1 = "3"
Value2 = "meineemail@gmx.de"
-> Update the field Email of the user with ID 3 in the table user with the value 'meineemail@gmx.de'. Returns 0 or 1 depending on the success of the action.
As already mentioned under 1c), for better security I could pass a token parameter with every call of the PHP database module. The token contains a random character sequence or a checksum, which was generated by the authentication module after the successful login.
But as I said, this is all just logical mind and "dangerous semi-knowing"
If anyone here has a clue about this stuff, then I am really grateful for links or hints.
Oh, there are two more things I can think of:
4) In the "CreateApp" dialog, in the "Web" tab below, there is the "Post processing:" section.
Is this something like an IDE tool which is started after compilation? So you could for example specify a batch to transfer the compilation directly to the web server?
5) I notice that when creating a web application, the folder containing the SpiderBasic libraries is always the same size. This means then that Spider does not do it like PureBasic and does not just copy the libs used by the program, but always copies all the libs?
Greetings Kurzer
(Mostly) Translated with http://www.DeepL.com/Translator (free version)
Note: This post was also published in the German PureBasic Forum in the SpiderBasic section.
I'm not very experienced in programming with SpiderBasic, but I'm still planning to do a "medium-small" project with Spider. At the moment I'm still working with the demo version of Spidebasic to see if I can implement all parts of the planned project.
With some topics for the realization of the project I can't judge if my approach is ok or if it could be done better in another way.
What exactly is it about?
It's about a web application that should be protected by a login and that communicates via several PHP modules via HttpRequest() to interact with a database and the filesystem of the webserver.
So I have a program part that runs on the client browser and will be written in Spiderbasic (or after compiling in JavaScript). Here the whole GUI, its logic and the loading, processing and displaying of data is realized.
The other part of the program is on the server and is called by SpiderBasic via HttpRequest(). The parameters are transferred by POST.
With the following points I do not know yet what approach is the best:
1) the topic login and authorization to use the SpiderBasic application at all.
If I would write the web application in pure PHP/HTML, I would program a login screen and store the user's permissions in a session variable after a successful login. When calling other PHP pages that belong to the application, I would read the session variable with the permission level and react accordingly in the PHP script (either continue the script or abort it with a error message if the permission is not sufficient).
But how do I do that within SpiderBasic?
a) I could implement the login screen directly in the SpiderBasic application, so that I can decide directly in SpiderBasic whether the user is allowed to continue working with the application or is kicked out immediately.
Now SpiderBasic is finally JavaScript and runs on the user's browser. Wouldn't it be possible for the user to analyze and modify the JavaScript code to bypass the login check? That would be a security risk. Unfortunately I am not very familiar with JavaScript.

b) I could implement the login screen in PHP, then directly fill the session variables with the user's authorization and, if the login was successful, forward them to the SpiderBasic application.
Here a similar problem arises nevertheless. The SpiderBasic application is started by calling the corresponding *.html file. The URL of the SpiderBasic application can then be seen either directly in the URL gadget of the browser or by using the browser's developer tools. If you know this URL, you could also call it directly without the login process. Then the login check would be completely bypassed.
b2) I could send a query from the SpiderBasic application to the web server to see if the current user has the appropriate permission (HttpRequest() to a PHP script that handles authentication). But again: JavaScript runs on the browser and I don't know if this call or its return value could be compromised by an attacker.
c) For additional security I could proceed as described under b2) and have the PHP authentication module return a token if the authorization was positive. This token would then have to be sent with all communication with the web server. This means every time the SpiderBasic application sends requests to the web server to acces the file system or database, it sends also this token. Only if the token sent is identical to the token of the successfully logged on user, the request is executed.
Thus, an unauthorized user could see and partially use the GUI of the SpiderBasic application, but everything related to server communication would fail.
Whether all this is then sufficiently secure, I cannot judge.
I would be very happy to hear from experts on this topic.

2) Passing parameters to a Spiderbasic application.
You can pass parameters to a PHP script via POST or GET. You can also pass parameters to a PureBasic program, which you can get with ProgramParameter().
For SpiderBasic I haven't found anything like this yet.
It might be useful with regard to the login topic, if I could tell the SpiderBasic application that the user has sufficient permissions when it is called.
3) Secure database queries
Again I am afraid that the JavaScript code can be changed to do mischief or to get unauthorized access to data.
This is about the secure coding of database queries in Spiderbasic.
The whole thing is realized by calling the HttpRequest() function of a PHP script, which then takes over the actual communication with the database and returns the result of the query to the SpiderBasic application.
Probably such a module has been developed a thousand times (CRUD module), but unfortunately I'm not further into the topic.
a) From a logical common sense point of view, I would say that it is highly dangerous to keep the entire SQL query inside the SpiderBasic application and send it to the PHP module. For example "Select id_user From users Where name = "Smith" and PasswordHash = "blahblah". An attacker could certainly manipulate the query to cause damage to the database.
b) One could abstract the functionality.
The PHP module would get the following parameters:
- Command (a flag indicating whether a select, delete, update or insert should be performed)
- Table (Indicator which specifies which table is mined - The indicator must not be the same as the table name!)
- Values (Which data fields should be returned)
- Clause (restrictions, i.e. the where clause of the SQL statement)
The whole thing is probably just as insecure as a), except that an attacker have to think about it a little longer.
c) You could put the entire function and the entire SQL into the PHP module and program all the commands or queries that the SpiderBasic program needs.
Then you would only send the following to the PHP module:
- Command (specifies what is required)
- Values[] (the values to be entered in the table during an Update or Insert Command)
The PHP module would then unfortunately have to contain a lot of redundant code, because a separate function would have to be written for each command.
For example:
Command = "CHECKUSER"
Value1 = "Smith"
Value2 = "blahblah"
-> Check if the user with the name "Smith" and the password hash "blahblah" exists in the table user and has the permission to use the application. Returns 0/1 if so.
Command = "GETPRODUCT"
Value1 = "1309"
-> Return the record of the product with the ID 1309 from the table products. The transfer to Spider then as JSON string.
Command = "UPDATEUSEREMAIL"
Value1 = "3"
Value2 = "meineemail@gmx.de"
-> Update the field Email of the user with ID 3 in the table user with the value 'meineemail@gmx.de'. Returns 0 or 1 depending on the success of the action.
As already mentioned under 1c), for better security I could pass a token parameter with every call of the PHP database module. The token contains a random character sequence or a checksum, which was generated by the authentication module after the successful login.
But as I said, this is all just logical mind and "dangerous semi-knowing"

If anyone here has a clue about this stuff, then I am really grateful for links or hints.
Oh, there are two more things I can think of:
4) In the "CreateApp" dialog, in the "Web" tab below, there is the "Post processing:" section.
Is this something like an IDE tool which is started after compilation? So you could for example specify a batch to transfer the compilation directly to the web server?
5) I notice that when creating a web application, the folder containing the SpiderBasic libraries is always the same size. This means then that Spider does not do it like PureBasic and does not just copy the libs used by the program, but always copies all the libs?
Greetings Kurzer
(Mostly) Translated with http://www.DeepL.com/Translator (free version)