Skip to content

Commit da1dcc9

Browse files
committed
docs: adds section on HMACSecretKey to Imaging Settings
1 parent efec273 commit da1dcc9

File tree

4 files changed

+280
-8
lines changed

4 files changed

+280
-8
lines changed

13/umbraco-cms/reference/configuration/imagingsettings.md

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ All these settings contain default values, so nothing needs to be explicitly con
2323
"Resize": {
2424
"MaxWidth": 5000,
2525
"MaxHeight": 5000
26-
}
26+
},
27+
"HMACSecretKey": ""
2728
}
2829
}
2930
}
3031
```
3132

3233
## Cache
3334

34-
Contains configuration for browser and server caching.
35+
Contains configuration for browser and server caching.
3536
When changing these cache headers, it is recommended to clear your media cache. This is due to the data being stored in the cache and not updated when the configuration is changed.
3637

3738
### Browser max age
@@ -82,3 +83,70 @@ public class ConfigureImageSharpMiddlewareOptionsComposer : IComposer
8283
});
8384
}
8485
```
86+
87+
## Hmac secret key
88+
89+
Specifies the key used to secure image requests by generating a hash-based message authentication code (HMAC). This ensures that only valid requests can access or manipulate images.
90+
91+
To enable it, you need to set a secure random key. This key should be kept secret and not shared publicly. The key can be set through the IOptions pattern, or you can insert a base64 encoded key in the `appsettings.json` file. The key should ideally be 64 bytes long.
92+
93+
The key must be the same across all environments (development, staging, production) to ensure that image requests work for content published across environments.
94+
95+
### Default Behavior
96+
97+
If the `HMACSecretKey` is not set, image requests will not be secured, and any person can request images with any parameters. This may expose your server to abuse, such as excessive resizing requests or unauthorized access to images.
98+
99+
### Key Length
100+
101+
The `HMACSecretKey` should be a secure, random key. For most use cases, a 64-byte (512-bit) key is recommended. If you are using HMACSHA384 or HMACSHA512, you may want to use a longer key (e.g., 128 bytes).
102+
103+
### Example Configuration
104+
105+
**Appsettings.json**
106+
```json
107+
"Umbraco": {
108+
"CMS": {
109+
"Imaging": {
110+
"HMACSecretKey": "some-base64-encoded-key"
111+
}
112+
}
113+
}
114+
```
115+
116+
**Using the IOptions pattern**
117+
118+
If you prefer to generate the `HMACSecretKey` programmatically or want to avoid hardcoding it in your configuration files, you can use the `IOptions` pattern. The following example demonstrates how to generate a secure random key at runtime:
119+
120+
```csharp
121+
using System.Security.Cryptography;
122+
using Umbraco.Cms.Core.Composing;
123+
using Umbraco.Cms.Core.Configuration.Models;
124+
125+
public class HMACSecretKeyComposer : IComposer
126+
{
127+
public void Compose(IUmbracoBuilder builder)
128+
=> builder.Services.Configure<ImagingSettings>(options =>
129+
{
130+
if (options.HMACSecretKey.Length == 0)
131+
{
132+
byte[] secret = new byte[64]; // Change to 128 when using HMACSHA384 or HMACSHA512
133+
RandomNumberGenerator.Create().GetBytes(secret);
134+
options.HMACSecretKey = secret;
135+
136+
var logger = builder.BuilderLoggerFactory.CreateLogger<HMACSecretKeyComposer>();
137+
logger.LogInformation("Imaging settings is now using HMACSecretKey: {HMACSecretKey}", Convert.ToBase64String(secret));
138+
}
139+
});
140+
}
141+
```
142+
143+
{% hint style="warning" %}
144+
The HMACSecretKey should be kept secret and never exposed publicly. If the key is leaked, malicious users could generate valid HMACs and abuse your server resources.
145+
{% endhint %}
146+
147+
### Testing the Configuration
148+
149+
To verify that your `HMACSecretKey` is working correctly:
150+
1. Configure the key in your `appsettings.json` or using the `IOptions` pattern.
151+
2. Make a request to an image URL with valid parameters and ensure it works as expected.
152+
3. Modify the URL parameters or remove the HMAC signature and confirm that the request is rejected.

14/umbraco-cms/reference/configuration/imagingsettings.md

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ All these settings contain default values, so nothing needs to be explicitly con
2323
"Resize": {
2424
"MaxWidth": 5000,
2525
"MaxHeight": 5000
26-
}
26+
},
27+
"HMACSecretKey": ""
2728
}
2829
}
2930
}
3031
```
3132

3233
## Cache
3334

34-
Contains configuration for browser and server caching.
35+
Contains configuration for browser and server caching.
3536
When changing these cache headers, it is recommended to clear your media cache. This is due to the data being stored in the cache and not updated when the configuration is changed.
3637

3738
### Browser max age
@@ -82,3 +83,70 @@ public class ConfigureImageSharpMiddlewareOptionsComposer : IComposer
8283
});
8384
}
8485
```
86+
87+
## Hmac secret key
88+
89+
Specifies the key used to secure image requests by generating a hash-based message authentication code (HMAC). This ensures that only valid requests can access or manipulate images.
90+
91+
To enable it, you need to set a secure random key. This key should be kept secret and not shared publicly. The key can be set through the IOptions pattern, or you can insert a base64 encoded key in the `appsettings.json` file. The key should ideally be 64 bytes long.
92+
93+
The key must be the same across all environments (development, staging, production) to ensure that image requests work for content published across environments.
94+
95+
### Default Behavior
96+
97+
If the `HMACSecretKey` is not set, image requests will not be secured, and any person can request images with any parameters. This may expose your server to abuse, such as excessive resizing requests or unauthorized access to images.
98+
99+
### Key Length
100+
101+
The `HMACSecretKey` should be a secure, random key. For most use cases, a 64-byte (512-bit) key is recommended. If you are using HMACSHA384 or HMACSHA512, you may want to use a longer key (e.g., 128 bytes).
102+
103+
### Example Configuration
104+
105+
**Appsettings.json**
106+
```json
107+
"Umbraco": {
108+
"CMS": {
109+
"Imaging": {
110+
"HMACSecretKey": "some-base64-encoded-key"
111+
}
112+
}
113+
}
114+
```
115+
116+
**Using the IOptions pattern**
117+
118+
If you prefer to generate the `HMACSecretKey` programmatically or want to avoid hardcoding it in your configuration files, you can use the `IOptions` pattern. The following example demonstrates how to generate a secure random key at runtime:
119+
120+
```csharp
121+
using System.Security.Cryptography;
122+
using Umbraco.Cms.Core.Composing;
123+
using Umbraco.Cms.Core.Configuration.Models;
124+
125+
public class HMACSecretKeyComposer : IComposer
126+
{
127+
public void Compose(IUmbracoBuilder builder)
128+
=> builder.Services.Configure<ImagingSettings>(options =>
129+
{
130+
if (options.HMACSecretKey.Length == 0)
131+
{
132+
byte[] secret = new byte[64]; // Change to 128 when using HMACSHA384 or HMACSHA512
133+
RandomNumberGenerator.Create().GetBytes(secret);
134+
options.HMACSecretKey = secret;
135+
136+
var logger = builder.BuilderLoggerFactory.CreateLogger<HMACSecretKeyComposer>();
137+
logger.LogInformation("Imaging settings is now using HMACSecretKey: {HMACSecretKey}", Convert.ToBase64String(secret));
138+
}
139+
});
140+
}
141+
```
142+
143+
{% hint style="warning" %}
144+
The HMACSecretKey should be kept secret and never exposed publicly. If the key is leaked, malicious users could generate valid HMACs and abuse your server resources.
145+
{% endhint %}
146+
147+
### Testing the Configuration
148+
149+
To verify that your `HMACSecretKey` is working correctly:
150+
1. Configure the key in your `appsettings.json` or using the `IOptions` pattern.
151+
2. Make a request to an image URL with valid parameters and ensure it works as expected.
152+
3. Modify the URL parameters or remove the HMAC signature and confirm that the request is rejected.

15/umbraco-cms/reference/configuration/imagingsettings.md

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ All these settings contain default values, so nothing needs to be explicitly con
2323
"Resize": {
2424
"MaxWidth": 5000,
2525
"MaxHeight": 5000
26-
}
26+
},
27+
"HMACSecretKey": ""
2728
}
2829
}
2930
}
3031
```
3132

3233
## Cache
3334

34-
Contains configuration for browser and server caching.
35+
Contains configuration for browser and server caching.
3536
When changing these cache headers, it is recommended to clear your media cache. This is due to the data being stored in the cache and not updated when the configuration is changed.
3637

3738
### Browser max age
@@ -82,3 +83,70 @@ public class ConfigureImageSharpMiddlewareOptionsComposer : IComposer
8283
});
8384
}
8485
```
86+
87+
## Hmac secret key
88+
89+
Specifies the key used to secure image requests by generating a hash-based message authentication code (HMAC). This ensures that only valid requests can access or manipulate images.
90+
91+
To enable it, you need to set a secure random key. This key should be kept secret and not shared publicly. The key can be set through the IOptions pattern, or you can insert a base64 encoded key in the `appsettings.json` file. The key should ideally be 64 bytes long.
92+
93+
The key must be the same across all environments (development, staging, production) to ensure that image requests work for content published across environments.
94+
95+
### Default Behavior
96+
97+
If the `HMACSecretKey` is not set, image requests will not be secured, and any person can request images with any parameters. This may expose your server to abuse, such as excessive resizing requests or unauthorized access to images.
98+
99+
### Key Length
100+
101+
The `HMACSecretKey` should be a secure, random key. For most use cases, a 64-byte (512-bit) key is recommended. If you are using HMACSHA384 or HMACSHA512, you may want to use a longer key (e.g., 128 bytes).
102+
103+
### Example Configuration
104+
105+
**Appsettings.json**
106+
```json
107+
"Umbraco": {
108+
"CMS": {
109+
"Imaging": {
110+
"HMACSecretKey": "some-base64-encoded-key"
111+
}
112+
}
113+
}
114+
```
115+
116+
**Using the IOptions pattern**
117+
118+
If you prefer to generate the `HMACSecretKey` programmatically or want to avoid hardcoding it in your configuration files, you can use the `IOptions` pattern. The following example demonstrates how to generate a secure random key at runtime:
119+
120+
```csharp
121+
using System.Security.Cryptography;
122+
using Umbraco.Cms.Core.Composing;
123+
using Umbraco.Cms.Core.Configuration.Models;
124+
125+
public class HMACSecretKeyComposer : IComposer
126+
{
127+
public void Compose(IUmbracoBuilder builder)
128+
=> builder.Services.Configure<ImagingSettings>(options =>
129+
{
130+
if (options.HMACSecretKey.Length == 0)
131+
{
132+
byte[] secret = new byte[64]; // Change to 128 when using HMACSHA384 or HMACSHA512
133+
RandomNumberGenerator.Create().GetBytes(secret);
134+
options.HMACSecretKey = secret;
135+
136+
var logger = builder.BuilderLoggerFactory.CreateLogger<HMACSecretKeyComposer>();
137+
logger.LogInformation("Imaging settings is now using HMACSecretKey: {HMACSecretKey}", Convert.ToBase64String(secret));
138+
}
139+
});
140+
}
141+
```
142+
143+
{% hint style="warning" %}
144+
The HMACSecretKey should be kept secret and never exposed publicly. If the key is leaked, malicious users could generate valid HMACs and abuse your server resources.
145+
{% endhint %}
146+
147+
### Testing the Configuration
148+
149+
To verify that your `HMACSecretKey` is working correctly:
150+
1. Configure the key in your `appsettings.json` or using the `IOptions` pattern.
151+
2. Make a request to an image URL with valid parameters and ensure it works as expected.
152+
3. Modify the URL parameters or remove the HMAC signature and confirm that the request is rejected.

16/umbraco-cms/reference/configuration/imagingsettings.md

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ All these settings contain default values, so nothing needs to be explicitly con
2323
"Resize": {
2424
"MaxWidth": 5000,
2525
"MaxHeight": 5000
26-
}
26+
},
27+
"HMACSecretKey": ""
2728
}
2829
}
2930
}
3031
```
3132

3233
## Cache
3334

34-
Contains configuration for browser and server caching.
35+
Contains configuration for browser and server caching.
3536
When changing these cache headers, it is recommended to clear your media cache. This is due to the data being stored in the cache and not updated when the configuration is changed.
3637

3738
### Browser max age
@@ -82,3 +83,70 @@ public class ConfigureImageSharpMiddlewareOptionsComposer : IComposer
8283
});
8384
}
8485
```
86+
87+
## Hmac secret key
88+
89+
Specifies the key used to secure image requests by generating a hash-based message authentication code (HMAC). This ensures that only valid requests can access or manipulate images.
90+
91+
To enable it, you need to set a secure random key. This key should be kept secret and not shared publicly. The key can be set through the IOptions pattern, or you can insert a base64 encoded key in the `appsettings.json` file. The key should ideally be 64 bytes long.
92+
93+
The key must be the same across all environments (development, staging, production) to ensure that image requests work for content published across environments.
94+
95+
### Default Behavior
96+
97+
If the `HMACSecretKey` is not set, image requests will not be secured, and any person can request images with any parameters. This may expose your server to abuse, such as excessive resizing requests or unauthorized access to images.
98+
99+
### Key Length
100+
101+
The `HMACSecretKey` should be a secure, random key. For most use cases, a 64-byte (512-bit) key is recommended. If you are using HMACSHA384 or HMACSHA512, you may want to use a longer key (e.g., 128 bytes).
102+
103+
### Example Configuration
104+
105+
**Appsettings.json**
106+
```json
107+
"Umbraco": {
108+
"CMS": {
109+
"Imaging": {
110+
"HMACSecretKey": "some-base64-encoded-key"
111+
}
112+
}
113+
}
114+
```
115+
116+
**Using the IOptions pattern**
117+
118+
If you prefer to generate the `HMACSecretKey` programmatically or want to avoid hardcoding it in your configuration files, you can use the `IOptions` pattern. The following example demonstrates how to generate a secure random key at runtime:
119+
120+
```csharp
121+
using System.Security.Cryptography;
122+
using Umbraco.Cms.Core.Composing;
123+
using Umbraco.Cms.Core.Configuration.Models;
124+
125+
public class HMACSecretKeyComposer : IComposer
126+
{
127+
public void Compose(IUmbracoBuilder builder)
128+
=> builder.Services.Configure<ImagingSettings>(options =>
129+
{
130+
if (options.HMACSecretKey.Length == 0)
131+
{
132+
byte[] secret = new byte[64]; // Change to 128 when using HMACSHA384 or HMACSHA512
133+
RandomNumberGenerator.Create().GetBytes(secret);
134+
options.HMACSecretKey = secret;
135+
136+
var logger = builder.BuilderLoggerFactory.CreateLogger<HMACSecretKeyComposer>();
137+
logger.LogInformation("Imaging settings is now using HMACSecretKey: {HMACSecretKey}", Convert.ToBase64String(secret));
138+
}
139+
});
140+
}
141+
```
142+
143+
{% hint style="warning" %}
144+
The HMACSecretKey should be kept secret and never exposed publicly. If the key is leaked, malicious users could generate valid HMACs and abuse your server resources.
145+
{% endhint %}
146+
147+
### Testing the Configuration
148+
149+
To verify that your `HMACSecretKey` is working correctly:
150+
1. Configure the key in your `appsettings.json` or using the `IOptions` pattern.
151+
2. Make a request to an image URL with valid parameters and ensure it works as expected.
152+
3. Modify the URL parameters or remove the HMAC signature and confirm that the request is rejected.

0 commit comments

Comments
 (0)