Skip to content

Commit defa23a

Browse files
authored
Documentation: Add Security Best Practices Page (#308)
1 parent dc1e83e commit defa23a

File tree

5 files changed

+397
-6
lines changed

5 files changed

+397
-6
lines changed

Documentation/docs/connecting.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ var connectResult = await client.ConnectAsync().ConfigureAwait(false);
7474

7575
## See Also
7676

77+
* [Security Best Practices](/docs/security) - Comprehensive security guide
7778
* [HiveMQClientOptionsBuilder Reference](/docs/reference/client_options_builder)
7879
* [Automatic Reconnect](/docs/reference/automatic_reconnect)
7980
* [How to Set a Last Will & Testament](/docs/how-to/set-lwt)

Documentation/docs/how-to/client-certificates.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,36 @@ var options = new HiveMQClientOptionsBuilder()
3333
var client = new HiveMQClient(options);
3434
```
3535

36-
## Using Certificates with a Passwords
36+
## Using Certificates with Passwords
3737

38-
If your certificate and protected with a password, you can either instantiate the
39-
`X509Certificate2` object manually and pass it to the HiveMQtt client with
40-
`WithClientCertificate`:
38+
If your certificate is protected with a password, you can use `SecureString` for enhanced security (recommended), or pass the password directly.
39+
40+
### Using SecureString (Recommended)
41+
42+
For enhanced security, use `SecureString` to prevent password exposure in memory:
43+
44+
```csharp
45+
using System.Security;
46+
using HiveMQtt.Client;
47+
48+
// Create SecureString for the certificate password
49+
var certPassword = new SecureString();
50+
foreach (char c in GetPasswordFromSecureSource())
51+
{
52+
certPassword.AppendChar(c);
53+
}
54+
certPassword.MakeReadOnly();
55+
56+
var options = new HiveMQClientOptionsBuilder()
57+
.WithClientCertificate("path/to/certificate-with-password.pfx", certPassword)
58+
.Build();
59+
60+
var client = new HiveMQClient(options);
61+
```
62+
63+
### Using X509Certificate2 Directly
64+
65+
You can also instantiate the `X509Certificate2` object manually:
4166

4267
```csharp
4368
using HiveMQtt.Client;
@@ -54,12 +79,16 @@ var options = new HiveMQClientOptionsBuilder()
5479
var client = new HiveMQClient(options);
5580
```
5681

57-
...or alternatively, just pass the string path to the certificate with the password:
82+
### Using String Password (Deprecated)
83+
84+
:::warning
85+
The string password overload is deprecated. Use `SecureString` for enhanced security.
86+
:::
5887

5988
```csharp
6089
using HiveMQtt.Client;
61-
using System.Security.Cryptography.X509Certificates;
6290

91+
// ⚠️ This generates a deprecation warning - use SecureString instead
6392
var options = new HiveMQClientOptionsBuilder()
6493
.WithClientCertificate(
6594
"path/to/certificate-with-password.pem",
@@ -198,6 +227,7 @@ TLS negotiation with client certificates is based on the `X509Certificate2` clas
198227

199228
## See Also
200229

230+
* [Security Best Practices](/docs/security) - Comprehensive security guide including SecureString usage
201231
* [X509 Client Certificate Authentication - MQTT Security Fundamentals](https://www.hivemq.com/blog/mqtt-security-fundamentals-x509-client-certificate-authentication/)
202232
* [TLS/SSL - MQTT Security Fundamentals](https://www.hivemq.com/blog/mqtt-security-fundamentals-tls-ssl/)
203233
* [HiveMQClientOptionsBuilder.cs](https://github.com/hivemq/hivemq-mqtt-client-dotnet/blob/main/Source/HiveMQtt/Client/HiveMQClientOptionsBuilder.cs)

Documentation/docs/how-to/connect-with-auth.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ if (connectResult.ReasonCode == ConnAckReasonCode.Success)
3535
Never hardcode credentials in your source code. Use environment variables, configuration files, or a secrets manager.
3636
:::
3737

38+
:::tip SecureString Support
39+
For enhanced security, use `SecureString` to prevent password exposure in memory. See the [Security Best Practices](/docs/security#securestring-for-credentials) guide for details.
40+
:::
41+
3842
### Using Environment Variables
3943

4044
```csharp
@@ -95,6 +99,7 @@ switch (connectResult.ReasonCode)
9599

96100
## See Also
97101

102+
- [Security Best Practices](/docs/security) - Comprehensive security guide including SecureString usage
98103
- [Authentication with Username and Password - MQTT Security Fundamentals](https://www.hivemq.com/blog/mqtt-security-fundamentals-authentication-username-password/)
99104
- [Advanced Authentication Mechanisms - MQTT Security Fundamentals](https://www.hivemq.com/blog/mqtt-security-fundamentals-advanced-authentication-mechanisms/)
100105
- [HiveMQ Cloud - Authentication and Authorization](https://docs.hivemq.com/hivemq-cloud/authn-authz.html)

Documentation/docs/intro.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ Then check out the [Quickstart Guide](/docs/quickstart) to begin.
3939

4040
### Security
4141
- **TLS/SSL Encryption**: Secure connections out of the box
42+
- **SecureString Support**: [Secure credential handling](/docs/security#securestring-for-credentials) to prevent password exposure in memory
4243
- **X.509 Client Certificates**: [Full support](/docs/how-to/client-certificates) for certificate-based authentication
4344
- **Username/Password Auth**: Simple credential-based authentication
45+
- **[Security Best Practices](/docs/security)**: Comprehensive guide for secure implementations
4446

4547
### Developer Experience
4648
- **Extensive Event System**: [Hook into all operations](/docs/events) from connection to packet level

0 commit comments

Comments
 (0)