BJS Security
BJS is very powerful and flexible. But with this. But simply make the code vulnerable.
Please read this article carefully
Especially if you work with payments, user balances, sell goods through a bot

Any user can execute any command

Vulnerable command /setBalance:
1
// admin can add 100$ to users's balance by it telegramid
2
​
3
tgID = params
4
​
5
let res = Libs.ResourcesLib.anotherUserRes("money", tgID);
6
res.add(100)
7
Bot.sendMessage("Added 100$ for user");
Copied!
Any user can run /setBalance [telegramid]
So need check execute this command only for admin:
1.Add this command to group​
add admin to group "admin":
1
// create any temporary command with this code
2
// run it for admin
3
​
4
// destroy command after for security
5
// also you can protect this command with password
6
​
7
User.addToGroup("admin")
Copied!
2. Or you can check admin in BJS
first you need get ADMIN_TELEGRAM_ID
1
Bot.sendMessage(user.telegramid)
Copied!
security command:
1
if(user.telegramid!=ADMIN_TELEGRAM_ID){
2
return // exit from BJS
3
}
4
​
5
// ONLY admin can add 100$ to users's balance by it telegramid
6
​
7
tgID = params
8
​
9
let res = Libs.ResourcesLib.anotherUserRes("money", tgID);
10
res.add(100)
11
Bot.sendMessage("Added 100$ for user");
Copied!
​

Any user can execute any "SECRET" command

For example, you have command /payment (have "Wait for answer") with execute other "secret" command /setBalance :
1
// command /payment
2
​
3
// user provide oneTime password. If password is valid - add bonus 100$
4
​
5
var oneTimePassword = User.getProperty("oneTimePassword");
6
​
7
if(!oneTimePassword){
8
return // we have not oneTime password now
9
}
10
​
11
if(oneTimePassword=="already taked"){
12
// if taked already - exit
13
return
14
}
15
​
16
if(oneTimePassword!=message){
17
// user do not know oneTime password
18
Bot.sendMessager("Error. Password is wrong")
19
}
20
​
21
if(oneTimePassword==message){
22
// user know oneTime password!
23
// make it "already taked"
24
User.setProperty("oneTimePassword", "already taked", "string")
25
// run "secret" command
26
Bot.runCommand("/setBalance");
27
Bot.sendMessage("Thank you for payment!");
28
}
Copied!
"Secret" command /setBalance
1
let res = Libs.ResourcesLib.userRes("money", tgID);
2
res.add(100)
3
Bot.sendMessage("Added 100$ for you");
Copied!
So user must:
    run /payment command
    type secret one time password
    after it - "secret" command "/setBalance" will be runned
Vulnerability: hacker can run /setBalance only and get bonus immediately
Need to checking that command /setBalance was runned only by command /payment
one of the methods - pass secret on run command as params:
command /payment
1
...
2
// part of code for /payment
3
​
4
if(oneTimePassword==message){
5
...
6
var secret = "GJHURFVJLHF" // use own secret. You can store it in property
7
Bot.runCommand("/setBalance");
8
Bot.sendMessage("Thank you for payment!");
9
}
Copied!
Do not use "GJHURFVJLHF" secret!
It is not secret world already: hacker can read this doc too!
command /setBalance
1
if(params=="GJHURFVJLHF"){
2
let res = Libs.ResourcesLib.userRes("money", tgID);
3
res.add(100)
4
Bot.sendMessage("Added 100$ for you");
5
}else{
6
Bot.sendMessage("You are hacker!")
7
}
Copied!
​

User can run secret command on group chat

It can be accidentally or deliberately provoked by a hacker.
If you have a secret command with a secret result, do not run it in a group chat:
1
// send this link only in PM - secure reason
2
if(chat.chat_type!="private"){
3
return
4
}
Copied!
​

Recommendations

Do not share your bot token, BB API Key

Bot token and BB API Key - are is very vulnerability data. Do not share theys anywhere!
​

Do not share your BB Bot ID

This can sometimes be unsafe.
​

Do not use default command names "/onIncome", "/onTransaction" for important commands

Hacker can brute force such command names and try to execute it
​

Remove /test command

If you have any /test command with non security BJS - remove it.
Hacker can execute /test too
​

Use completed_commands_count variable

Anybody can run any command. But it is possible make secured sub command.
For example command /admin
1
// make admin access here
2
// ...
3
​
4
Bot.runCommand("/secure")
Copied!
command /secure
1
// this command can not be runned by user
2
if(completed_commands_count==0){ return }
3
​
4
// only via Bot.runCommand, Bot.run or as "on_result"
5
// your secure code here
6
// ...
Copied!

Do not use any non official libs now.

Do not use any non official libs now.
    Any lib can run command with options.
    Any libs can read properties (and read your API Keys from other lib)
We have not way to protect this now. Just not use NON official libs with CP lib. Well, that now there are no such libraries

Bad practice

User can change nickname

Bad BJS:
1
let admin = "Jon Smith";
2
​
3
if (user.first_name==admin){
4
// do admin action here
5
...
6
}
Copied!
Any user can set any first_name, last_name and etc
Hacker can change or create account with this field

​

Use eval method with care

You can use eval for calculation
1
// with Wait for Answer
2
​
3
// message from user is: 2+2
4
// eval - it js execution from string
5
let result = eval(message);
6
​
7
// 2+2 = 4. So we have 4 in result now
8
Bot.sendMessage(result)
Copied!
With such code you can make math calculator.
But it is very danger! User can run anything!
For example, user can pass BJS code: bot.token. And bot will send your bot token!

See BB reports

​Read info about BB report. Demo report have nice recommendations.
Last modified 4mo ago