Supported from Base Library 2.17.3 onwards
I. Feature Overview
During the front-end and back-end development and integration process, developers typically use proxy tools to forward requests to their own project services, facilitating the switching of development environments and testing.
Proxy tools can transmit data between the client and the real server.
In this scenario, the proxy tool can decrypt and inspect data encrypted via SSL, then re-encrypt the data before sending it to the original destination. To the client and the server, they still believe they are in a secure encrypted connection, but in reality, the security of the data has been compromised.
In addition to monitoring, since proxy tools can decrypt SSL-encrypted communications, they also support modifying data in transit and even injecting malicious content. This type of man-in-the-middle (MITM) proxy is also maliciously exploited by bad actors to arbitrarily monitor all requests on their own devices.
They run "Mini Program," "WEB pages," or "native APPs" developed by other developers on their devices, then use a man-in-the-middle proxy to capture and tamper with the "request packets" and "response packets" of normal requests. They also perform targeted replay attacks on target requests to achieve malicious objectives (such as grinding experience points, modifying game results, or creating orders for 0.01 CNY).
To prevent man-in-the-middle proxy attacks, we need to perform an additional encryption layer on the plaintext of request and response packets on top of SSL encryption. This approach provides an extra layer of security, ensuring that data remains secure even after being decrypted by proxy tools.
Currently, the WeChat team provides two solutions for encrypted network channels:
- API Self-Implementation Solution: The WeChat platform maintains a reliable Key at the user level. Developers can retrieve this Key separately via the Mini Program front-end API and the server-side interface provided by the WeChat backend, and then implement the encryption of request packets themselves.
- WeChat Gateway Solution: The WeChat Gateway (1000780), launched by the WeChat team based on years of accumulated security protection experience, possesses systematic and multi-level security protection capabilities. Once developers integrate with it, the communication link from "Mini Program" to the "Developer Server" is automatically switched from the public network link to the WeChat dedicated line, with two-tier encryption applied to the request content throughout the process, effectively preventing man-in-the-middle proxy attacks.
Retrieving the encryption key information requires sending requests to the WeChat platform, incurring latency, which imposes a burden on business performance. If developers wish to achieve secure encryption while reducing business-irrelevant latency, they can use the WeChat Gateway Solution.
2. Custom API Implementation
To prevent data interception and tampering during communication between the Mini Program and the developer backend, the WeChat platform maintains a reliable user-level Key for encryption and signing during communication between the Mini Program and the backend.
Developers can obtain the user encryption key separately through the Mini Program frontend API and the server-side interface provided by the WeChat backend.
The WeChat platform only provides the reliable encryption key. Developers must implement the encryption method themselves to encrypt and decrypt the data requested from the server-side interface on the client side.
In the Mini Program, developers can use UserCryptoManager.getLatestUserKey to retrieve the latest user encryption key information.
2.1 Frontend Invocation Example
const somedata = 'xxxxx'
const userCryptoManager = wx.getUserCryptoManager()
userCryptoManager.getLatestUserKey({
success({encryptKey, iv, version, expireTime}) {
const encryptedData = someAESEncryptMethod(encryptKey, iv, somedata)
wx.request({
data: encryptedData,
success(res) {
const decryptedData = someAESDEcryptMethod(encryptKey, iv, res.data)
console.log(decryptedData)
}
})
}
})
someAESEncryptMethod and someAESDEcryptMethod are encryption and decryption functions, respectively. Developers must introduce an encryption/decryption library to implement these. The Base Library does not currently provide encryption/decryption capabilities.
Developers can refer to open-source encryption libraries: https://github.com/flash1293/aes-wasm https://github.com/ricmoo/aes-js
Based on your specific business requirements, choose an appropriate encryption method:
- Asymmetric Encryption: The client and server maintain two sets of public and private encryption keys. For each API request, the plaintext data is encrypted first. The other party decrypts the ciphertext upon receipt using the corresponding reverse key. Common asymmetric encryption algorithms include RSA, DSA, and ECC. Asymmetric encryption offers high security but slower encryption/decryption speeds, making it unsuitable for scenarios with large request or response bodies.
- Symmetric Encryption: The client and server maintain a single shared key. For each API request, the plaintext data is encrypted, and the other party decrypts it upon receipt using the same key. Common symmetric encryption algorithms include DES, 3DES, IDEA, RC5, RC6, and Blowfish. Symmetric encryption offers fast encryption/decryption speeds and is suitable for processing large data volumes, though with slightly lower security compared to asymmetric methods.
- Hybrid Scheme: During a request, the client encrypts the plaintext using a symmetric encryption method with a randomly generated symmetric key. It then encrypts this symmetric key using an asymmetric encryption method. Finally, the ciphertext and the encrypted symmetric key are combined and sent. Upon receipt, the server decrypts using the reverse process. The response packet follows the same processing flow.
On the developer's server side, you can call the getUserEncryptKey backend API to retrieve the user's latest three keys. The API also includes version information when retrieving the keys. Developers can compare the version to select the corresponding key for data encryption and decryption.
2.2 Server-side Call Example
curl -X POST "https://api.weixin.qq.com/wxa/business/getuserencryptkey?access_token=ACCESS_TOKEN&openid=OPENID&signature=SIGNATURE&sig_method=hmac_sha256"
3. WeChat Gateway Solution
You can refer to this document to integrate with the WeChat Gateway: Mini Program One-click Integration with the WeChat Gateway