Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud Development Content Security Detection


May 22, 2021 Mini Program Cloud Development Advanced



Many business scenarios of WeChat's small programs require a UGC (user-generated content) approach, such as nicknames/flower names, profile signatures/logs/chats/comments, avatars/emoticons/photos, live streams, etc., in a format that includes, but is not limited to, short text, long content, pictures or videos to achieve a better user experience or richer content features and service scenarios. H owever, if the use of such features does not do a good job of security review of user-published content, may result in politically harmful and other illegal content. O nce used for dissemination, it can be detrimental to the user of the small program, and the small program developer may also be liable for the platform or the law and punishment. Therefore, small programs that contain UGC functionality require content security detection.

First, the text content is safe

Use the developer tool to create a new cloud function, such as msgsec, and then add permissions called by the security.msgSecCheck cloud inconfig.json, and upload and deploy all files (and update the permissions at this point) after installing the dependency using npm install.

  1. {
  2. "permissions": {
  3. "openapi": [
  4. "security.imgSecCheck",
  5. "security.msgSecCheck"
  6. ]
  7. }
  8. }

Then enter the following code .js the index code,

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV
  4. })
  5. exports.main = async (event, context) => {
  6. try {
  7. const result = await cloud.openapi.security.msgSecCheck({
  8. content:`特3456yuuo6543zxcz7782fgnv
  9. 2347dfji3726asad3847qwez`
  10. })
  11. return result
  12. } catch (error) {
  13. return error
  14. }
  15. }

Calling the cloud function, interface errcode returns 87014 (content contains illegal content):

  1. errMsg: "cloud.callFunction:ok",
  2. result: {
  3. errCode: 87014
  4. errMsg: "openapi.security.msgSecCheck:fail risky content hint: [bgh98a06644711]"}

If the returned result.errCode has a value of 0, the content is correct.

  1. errMsg: "cloud.callFunction:ok"
  2. result: {
  3. errMsg: "openapi.security.msgSecCheck:ok",
  4. errCode: 0}

Second, the picture is yellow

The biggest difference between image content security detection and text content security detection is that we need to consider the time-consuming image transmission and the detection of pictures can not be greater than 1M such a limit, when the picture size is relatively large, we need to compress the picture. A lso, the image files to be detected are PNG, JPEG, JPG, GIF, and the picture size does not exceed 750px x 1334px. Usually when we use the small program endchooseImage to upload images, we try to require the use of compressed compression diagrams, album compression charts generally do not exceed 1M.

  • If the picture is relatively small (about 200k or less), we can store the picture directly in the cloud storage, and then download the picture in the cloud function for image security detection;

  • If the picture is relatively large (e.g. greater than 100k, less than 1M), we can store the picture to the cloud storage, download the picture at the cloud function side, use the Sharp module mentioned earlier, compress the picture, and then carry out the picture security detection.

  • If the picture is larger than 1M, then the compression of the picture is not recommended in the cloud function processing, to use Canvas for compression in the small terminal, and then transferred to the cloud function for security detection.

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV
  4. })
  5. exports.main = async (event, context) => {
  6. const fileID = 'cloud://xly-xrlur.786c-xly-xrlur-1300446086/1572315793628-366.png'
  7. const res = await cloud.downloadFile({
  8. fileID: fileID,
  9. })
  10. const Buffer = res.fileContent
  11. try {
  12. const result = await cloud.openapi.security.imgSecCheck({
  13. media: {
  14. contentType: 'image/png',
  15. value: Buffer
  16. }
  17. })
  18. return result
  19. } catch (error) {
  20. return error
  21. }
  22. }

Third, image content security expansion capabilities

Cloud call in the image content security processing, there are some shortcomings in the function (such as no subdivision of yellow-related, political-related, tyrannic and advertising guidance class), there is a little strict restrictions (the size of the picture has strict requirements), it is recommended that we install cloud development image security audit expansion capabilities.

Its installation method is described in the Cloud Call and Expansion Capabilities section, while the expansion capabilities of image processing in the previous section of the usage method are in line with each other and have the same usage methods, because image content security is part of image processing. So to use the image content security expansion capabilities suggest reading the previous content first, here only give the actual code.

1, in the small terminal use of image security audit

First we refer to the last section to build npm of image processing, and then we introduce packages in the .js and write an event handler in the Page function. Image security audit can only be post-verified, that is, content security audit can only be performed on images that have been uploaded to cloud storage, as follows:

  1. const extCi = require("./../../miniprogram_npm/@cloudbase/extension-ci-wxmp");
  2. Page({
  3. async imgSec(){
  4. extCi.invoke({
  5. action: "DetectType",
  6. cloudPath: "tcbdemo.jpg",
  7. operations: {
  8. type: 'porn,ads,terrorist,politics'
  9. }
  10. }).then(res => {
  11. console.log(res.data);
  12. }).catch(err => {
  13. console.log(err);
  14. })
  15. }
  16. })

Here the type of type of content review, porn (yellow-related identification), terrorist (trump-related terrorism identification), politics (politics-related identification), ads (advertising identification), we can write like the above four together, or we can only write a few of them, with , separated can be.

Print res.data, which contains the Object of RecognitionResult, which displays the results of the review of the image content, with commercials as shown below:

  • HitFlag, indicating whether to hit: 0 (missed), 1 (hit), 2 (suspected);

  • Score, audit score: 0 - 60 (normal), 60 - 90 (suspected sensitive), 90 - 100 (determined sensitive)

  • Label, for the identified label

  1. RecognitionResult{
  2. PornInfo: {Code: 0, Msg: "OK", HitFlag: 0, Score: 14, Label: ""}
  3. TerroristInfo: {Code: 0, Msg: "OK", HitFlag: 0, Score: 0, Label: ""}
  4. PoliticsInfo: {Code: 0, Msg: "OK", HitFlag: 0, Score: 26, Label: ""}
  5. AdsInfo: {Code: 0, Msg: "OK", HitFlag: 1, Score: 98, Label: "淘宝"}
  6. }

In the small terminal audit image, we can first upload the picture to the cloud storage, and then get the picture in the cloud storage cloudPath (not fileID, is the absolute path of relative cloud storage), and then the picture audit, audit success to be displayed, audit failure to delete the picture, let the user re-upload.

2, in the cloud function using image security audit

Use the developer tool to create a new imgSec cloud function, then add @cloudbase/extension-ci the cloud function directory to select the input command npm install installation dependency to open in the terminal:

  1. "dependencies": {
  2. "wx-server-sdk": "latest",
  3. "@cloudbase/extension-ci": "latest"
  4. }

Then enter the following code .js in the index code, the specific meaning of the code can be explained by reference to the contents of the small terminal:

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV
  4. })
  5. const extCi = require('@cloudbase/extension-ci')
  6. cloud.registerExtension(extCi)
  7. async function imgSec() {
  8. try {
  9. const res = await app.invokeExtension('CloudInfinite', {
  10. action: 'DetectType',
  11. cloudPath: 'tcbdemo.png',
  12. operations: {
  13. type: 'porn,ads,terrorist,politics'
  14. }
  15. })
  16. console.log(res)
  17. return res
  18. } catch (err) {
  19. console.log(err)
  20. }
  21. }