The Leancloud visitor counter plugin used in NexT has a big security bug, by which someone could change your visitor number easily and even add/delete records in your database.
This bug is found by LEAFERx and confirmed by Ivan.Nginx.
Related issue: #25
Related pr: #137
Related plugin: hexo-leancloud-counter-security
This bug could only be fixed manually.
Warning: All NexT sites using Leancloud visitor counter that are not fixed and other sites integrated this function by similiar ways are considered unsecurity. Please fix it as soon as possible.
Chinese version guide is here
For convience, this doc also includes the way to setup the plugin. If you have already done this, skip to Deploy web engine to avoid your data being changed illegally.
Before you make the config, please upgrade your NexT version to v6.0.6 or greater.
Please note the difference between site config file and theme config file
¶Sign up to Leancloud and create an app
Go to Leancloud website leancloud.cn and sign up to Leancloud. Then login.
Click
1to enter the console:
Then click
1to create an app:
Type your app name in
1in the pop up window(eg. "test"), then choose2, which means developer's plan, and then click3to create the app:
¶Create Counter class and enable plugin in NexT
Click
1(app name) to enter the app manage page:
then click
1to create a class for counter:
Type
Counterin the pop up window in1, check2, then click3:
Click
1to enter the app setting, then click2:
Paste
App IDandApp Keyto theme config file_config.ymllike this:1
2
3
4
5
6
7leancloud_visitors:
enable: true
app_id: <<your app id>>
app_key: <<your app key>>
# Dependencies: https://github.com/theme-next/hexo-leancloud-counter-security
security: true
betterPerformance: falseSet domain whitelist: Click
1, then type your domain into2(protocol, domain and port should be exactly the same):

¶Deploy web engine to avoid your data being changed illegally
Click
1 -> 2 -> 3by order
Click
1:
In the pop up window, click
1to choose typeHook, then choosebeforeUpdatein2, chooseCounterin3. Paste code below into4, then click5to save it:1
2
3
4
5
6
7
8var query = new AV.Query("Counter");
if (request.object.updatedKeys.indexOf('time') !== -1) {
return query.get(request.object.id).then(function (obj) {
if (obj.get("time") + 1 !== request.object.get("time")) {
throw new AV.Cloud.Error('Invalid update!');
}
})
}
Click
1to deploy after the message in the red rect shows up:
Click
1in the pop up:
Click
1to close the pop up window after the message in the red rect shows up:
¶Set access control for your database
Open theme config file
_config.yml, setleancloud_visitors: securitytotrue:1
2
3
4
5
6
7leancloud_visitors:
enable: true
app_id: <<your app id>>
app_key: <<your app key>>
# Dependencies: https://github.com/theme-next/hexo-leancloud-counter-security
security: true
betterPerformance: falseExplaination for
betterPerformance:
Because the Leancloud developer's plan has limits in requst thread amount and running time, counter number may be very slow to load in some times. If setbetterPerformanceto true, counter number will be displayed quickly by assuming the request is accepted normally.Open cmd then switch to root path of site, type commands to install
hexo-leancloud-counter-securityplugin:1
npm install hexo-leancloud-counter-security --save
Open site config file
_config.yml, add those config:1
2
3
4
5
6leancloud_counter_security:
enable_sync: true
app_id: <<your app id>>
app_key: <<your app key>
username:
password:Type command:
1
hexo lc-counter register <<username>> <<password>>
or
1
hexo lc-counter r <<username>> <<password>>
Change
<<username>>and<<password>>to your own username and password (no need to be the same as leancloud account). They will be used in the hexo deploying.- Open site config file
_config.yml, change<<username>>and<<password>>to those you set above:
1
2
3
4
5
6leancloud_counter_security:
enable_sync: true
app_id: <<your app id>>
app_key: <<your app key>
username: <<your username>> # will be asked while deploying if be left blank
password: <<your password>> # recommend to leave it blank for security, will be asked while deploying if be left blank- Open site config file
Add the deployer in the
deployof site config file_config.yml:1
2
3deploy:
# other deployer
- type: leancloud_counter_security_syncReturn to the Leancloud console. Click
1 -> 2, check if there is a record added in the _User (the img below is using username "admin" for example):
Click
1 -> 2 -> 3by order:
ClickDo as below "create" setting(choose the user you create):1(add_fields), then choose2:
click
1(create), then choose2, type the username in3, then click4 -> 5:
Now your page should be similar to this img after finishing the step.

Click
1(delete), then choose2:

Now the bug is fixed.
Every time when you run hexo d, plugin will scan posts in the source/_posts and compare to the database, then add create records for those posts which are not list in the database. This procedure is done locally so that database can only be changed by you.

