Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Download import pictures only from public addresses as real images (#862)

Krzysztof Pajak2abbaed23208
diff --git a/src/Business/Grand.Business.Catalog/Extensions/MimeTypeExtensions.cs b/src/Business/Grand.Business.Catalog/Extensions/MimeTypeExtensions.cs
deleted file mode 100644
index 4decbd71a..000000000
--- a/src/Business/Grand.Business.Catalog/Extensions/MimeTypeExtensions.cs
+++ /dev/null
@@ -1,13 +0,0 @@
-using Microsoft.AspNetCore.StaticFiles;
-
-namespace Grand.Business.Catalog.Extensions;
-
-public static class MimeTypeExtensions
-{
- public static string GetMimeTypeFromFilePath(string filePath)
- {
- new FileExtensionContentTypeProvider().TryGetContentType(filePath, out var mimeType);
- //set to jpeg in case mime type cannot be found
- return mimeType ?? "image/jpeg";
- }
-}
\ No newline at end of file
diff --git a/src/Business/Grand.Business.Catalog/Services/ExportImport/BrandImportDataObject.cs b/src/Business/Grand.Business.Catalog/Services/ExportImport/BrandImportDataObject.cs
index b3198b193..7a6cfddb9 100644
--- a/src/Business/Grand.Business.Catalog/Services/ExportImport/BrandImportDataObject.cs
+++ b/src/Business/Grand.Business.Catalog/Services/ExportImport/BrandImportDataObject.cs
@@ -1,11 +1,12 @@
-using Grand.Business.Catalog.Extensions;
-using Grand.Business.Core.Dto;
+using Grand.Business.Core.Dto;
using Grand.Business.Core.Interfaces.Catalog.Brands;
using Grand.Business.Core.Interfaces.Common.Seo;
using Grand.Business.Core.Interfaces.ExportImport;
using Grand.Business.Core.Interfaces.Storage;
+using Grand.Business.Core.Utilities.System;
using Grand.Domain.Catalog;
using Grand.Domain.Media;
+using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Mapper;
namespace Grand.Business.Catalog.Services.ExportImport;
@@ -17,19 +18,22 @@ public class BrandImportDataObject : IImportDataObject<BrandDto>
private readonly IPictureService _pictureService;
private readonly ISlugService _slugService;
private readonly ISeNameService _seNameService;
+ private readonly SecurityConfig _securityConfig;
public BrandImportDataObject(
IBrandService brandService,
IPictureService pictureService,
IBrandLayoutService brandLayoutService,
ISlugService slugService,
- ISeNameService seNameService)
+ ISeNameService seNameService,
+ SecurityConfig securityConfig)
{
_brandService = brandService;
_pictureService = pictureService;
_brandLayoutService = brandLayoutService;
_slugService = slugService;
_seNameService = seNameService;
+ _securityConfig = securityConfig;
}
public async Task Execute(IEnumerable<BrandDto> data)
@@ -89,17 +93,23 @@ public class BrandImportDataObject : IImportDataObject<BrandDto>
/// <summary>
/// Creates or loads the image
/// </summary>
- /// <param name="picturePath">The path to the image file</param>
+ /// <param name="pictureUrl">The URL of the image</param>
/// <param name="name">The name of the object</param>
/// <param name="picId">Image identifier, may be null</param>
/// <returns>The image or null if the image has not changed</returns>
- private async Task<Picture> LoadPicture(string picturePath, string name, string picId = "")
+ private async Task<Picture> LoadPicture(string pictureUrl, string name, string picId = "")
{
- if (string.IsNullOrEmpty(picturePath) || !File.Exists(picturePath))
+ if (string.IsNullOrEmpty(pictureUrl))
return null;
- var mimeType = MimeTypeExtensions.GetMimeTypeFromFilePath(picturePath);
- var newPictureBinary = await File.ReadAllBytesAsync(picturePath);
+ //only http(s) URLs to public or explicitly allowed hosts are downloaded - a local path would let the
+ //import read any file on the server
+ var image = await DownloadUrl.DownloadImage(pictureUrl, _securityConfig.PictureImportAllowedPrivateHosts);
+ if (image == null)
+ return null;
+
+ var mimeType = image.MimeType;
+ var newPictureBinary = image.Binary;
var pictureAlreadyExists = false;
if (!string.IsNullOrEmpty(picId))
{
diff --git a/src/Business/Grand.Business.Catalog/Services/ExportImport/CategoryImportDataObject.cs b/src/Business/Grand.Business.Catalog/Services/ExportImport/CategoryImportDataObject.cs
index 8e49775ab..13723cf59 100644
--- a/src/Business/Grand.Business.Catalog/Services/ExportImport/CategoryImportDataObject.cs
+++ b/src/Business/Grand.Business.Catalog/Services/ExportImport/CategoryImportDataObject.cs
@@ -1,11 +1,12 @@
-using Grand.Business.Catalog.Extensions;
-using Grand.Business.Core.Dto;
+using Grand.Business.Core.Dto;
using Grand.Business.Core.Interfaces.Catalog.Categories;
using Grand.Business.Core.Interfaces.Common.Seo;
using Grand.Business.Core.Interfaces.ExportImport;
using Grand.Business.Core.Interfaces.Storage;
+using Grand.Business.Core.Utilities.System;
using Grand.Domain.Catalog;
using Grand.Domain.Media;
+using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Mapper;
namespace Grand.Business.Catalog.Services.ExportImport;
@@ -17,19 +18,22 @@ public class CategoryImportDataObject : IImportDataObject<CategoryDto>
private readonly IPictureService _pictureService;
private readonly ISlugService _slugService;
private readonly ISeNameService _seNameService;
+ private readonly SecurityConfig _securityConfig;
public CategoryImportDataObject(
ICategoryService categoryService,
IPictureService pictureService,
ICategoryLayoutService categoryLayoutService,
ISlugService slugService,
- ISeNameService seNameService)
+ ISeNameService seNameService,
+ SecurityConfig securityConfig)
{
_categoryService = categoryService;
_pictureService = pictureService;
_categoryLayoutService = categoryLayoutService;
_slugService = slugService;
_seNameService = seNameService;
+ _securityConfig = securityConfig;
}
public async Task Execute(IEnumerable<CategoryDto> data)
@@ -94,17 +98,23 @@ public class CategoryImportDataObject : IImportDataObject<CategoryDto>
/// <summary>
/// Creates or loads the image
/// </summary>
- /// <param name="picturePath">The path to the image file</param>
+ /// <param name="pictureUrl">The URL of the image</param>
/// <param name="name">The name of the object</param>
/// <param name="picId">Image identifier, may be null</param>
/// <returns>The image or null if the image has not changed</returns>
- private async Task<Picture> LoadPicture(string picturePath, string name, string picId = "")
+ private async Task<Picture> LoadPicture(string pictureUrl, string name, string picId = "")
{
- if (string.IsNullOrEmpty(picturePath) || !File.Exists(picturePath))
+ if (string.IsNullOrEmpty(pictureUrl))
return null;
- var mimeType = MimeTypeExtensions.GetMimeTypeFromFilePath(picturePath);
- var newPictureBinary = await File.ReadAllBytesAsync(picturePath);
+ //only http(s) URLs to public or explicitly allowed hosts are downloaded - a local path would let the
+ //import read any file on the server
+ var image = await DownloadUrl.DownloadImage(pictureUrl, _securityConfig.PictureImportAllowedPrivateHosts);
+ if (image == null)
+ return null;
+
+ var mimeType = image.MimeType;
+ var newPictureBinary = image.Binary;
var pictureAlreadyExists = false;
if (!string.IsNullOrEmpty(picId))
{
diff --git a/src/Business/Grand.Business.Catalog/Services/ExportImport/CollectionImportDataObject.cs b/src/Business/Grand.Business.Catalog/Services/ExportImport/CollectionImportDataObject.cs
index 3ce18a156..ea49ac46c 100644
--- a/src/Business/Grand.Business.Catalog/Services/ExportImport/CollectionImportDataObject.cs
+++ b/src/Business/Grand.Business.Catalog/Services/ExportImport/CollectionImportDataObject.cs
@@ -1,11 +1,12 @@
-using Grand.Business.Catalog.Extensions;
-using Grand.Business.Core.Dto;
+using Grand.Business.Core.Dto;
using Grand.Business.Core.Interfaces.Catalog.Collections;
using Grand.Business.Core.Interfaces.Common.Seo;
using Grand.Business.Core.Interfaces.ExportImport;
using Grand.Business.Core.Interfaces.Storage;
+using Grand.Business.Core.Utilities.System;
using Grand.Domain.Catalog;
using Grand.Domain.Media;
+using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Mapper;
namespace Grand.Business.Catalog.Services.ExportImport;
@@ -17,12 +18,14 @@ public class CollectionImportDataObject : IImportDataObject<CollectionDto>
private readonly IPictureService _pictureService;
private readonly ISlugService _slugService;
private readonly ISeNameService _seNameService;
+ private readonly SecurityConfig _securityConfig;
public CollectionImportDataObject(
ICollectionService collectionService,
IPictureService pictureService,
ICollectionLayoutService collectionLayoutService,
ISlugService slugService,
- ISeNameService seNameService)
+ ISeNameService seNameService,
+ SecurityConfig securityConfig)
{
_collectionService = collectionService;
_pictureService = pictureService;
@@ -30,6 +33,7 @@ public class CollectionImportDataObject : IImportDataObject<CollectionDto>
_slugService = slugService;
_seNameService = seNameService;
+ _securityConfig = securityConfig;
}
public async Task Execute(IEnumerable<CollectionDto> data)
@@ -91,17 +95,23 @@ public class CollectionImportDataObject : IImportDataObject<CollectionDto>
/// <summary>
/// Creates or loads the image
/// </summary>
- /// <param name="picturePath">The path to the image file</param>
+ /// <param name="pictureUrl">The URL of the image</param>
/// <param name="name">The name of the object</param>
/// <param name="picId">Image identifier, may be null</param>
/// <returns>The image or null if the image has not changed</returns>
- private async Task<Picture> LoadPicture(string picturePath, string name, string picId = "")
+ private async Task<Picture> LoadPicture(string pictureUrl, string name, string picId = "")
{
- if (string.IsNullOrEmpty(picturePath) || !File.Exists(picturePath))
+ if (string.IsNullOrEmpty(pictureUrl))
return null;
- var mimeType = MimeTypeExtensions.GetMimeTypeFromFilePath(picturePath);
- var newPictureBinary = await File.ReadAllBytesAsync(picturePath);
+ //only http(s) URLs to public or explicitly allowed hosts are downloaded - a local path would let the
+ //import read any file on the server
+ var image = await DownloadUrl.DownloadImage(pictureUrl, _securityConfig.PictureImportAllowedPrivateHosts);
+ if (image == null)
+ return null;
+
+ var mimeType = image.MimeType;
+ var newPictureBinary = image.Binary;
var pictureAlreadyExists = false;
if (!string.IsNullOrEmpty(picId))
{
diff --git a/src/Business/Grand.Business.Catalog/Services/ExportImport/ProductImportDataObject.cs b/src/Business/Grand.Business.Catalog/Services/ExportImport/ProductImportDataObject.cs
index b02eb45c0..fb31d7b82 100644
--- a/src/Business/Grand.Business.Catalog/Services/ExportImport/ProductImportDataObject.cs
+++ b/src/Business/Grand.Business.Catalog/Services/ExportImport/ProductImportDataObject.cs
@@ -1,5 +1,4 @@
-using Grand.Business.Catalog.Extensions;
-using Grand.Business.Core.Dto;
+using Grand.Business.Core.Dto;
using Grand.Business.Core.Interfaces.Catalog.Brands;
using Grand.Business.Core.Interfaces.Catalog.Categories;
using Grand.Business.Core.Interfaces.Catalog.Collections;
@@ -13,6 +12,7 @@ using Grand.Business.Core.Interfaces.Storage;
using Grand.Business.Core.Utilities.System;
using Grand.Domain.Catalog;
using Grand.Domain.Common;
+using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Mapper;
namespace Grand.Business.Catalog.Services.ExportImport;
@@ -33,6 +33,7 @@ public class ProductImportDataObject : IImportDataObject<ProductDto>
private readonly ITaxCategoryService _taxService;
private readonly IWarehouseService _warehouseService;
private readonly ISeNameService _seNameService;
+ private readonly SecurityConfig _securityConfig;
public ProductImportDataObject(
IProductService productService,
IPictureService pictureService,
@@ -47,7 +48,8 @@ public class ProductImportDataObject : IImportDataObject<ProductDto>
IBrandService brandService,
ICollectionService collectionService,
IProductCollectionService productCollectionService,
- ISeNameService seNameService)
+ ISeNameService seNameService,
+ SecurityConfig securityConfig)
{
_productService = productService;
_pictureService = pictureService;
@@ -63,6 +65,7 @@ public class ProductImportDataObject : IImportDataObject<ProductDto>
_collectionService = collectionService;
_productCollectionService = productCollectionService;
_seNameService = seNameService;
+ _securityConfig = securityConfig;
}
public async Task Execute(IEnumerable<ProductDto> data)
@@ -212,56 +215,45 @@ public class ProductImportDataObject : IImportDataObject<ProductDto>
var picture2 = productDto.Picture2;
var picture3 = productDto.Picture3;
- foreach (var picturePath in new[] { picture1, picture2, picture3 })
+ foreach (var pictureUrl in new[] { picture1, picture2, picture3 })
{
- if (string.IsNullOrEmpty(picturePath))
+ if (string.IsNullOrEmpty(pictureUrl))
continue;
- if (!picturePath.ToLower().StartsWith("http".ToLower()))
+
+ //only http(s) URLs to public or explicitly allowed hosts are downloaded - a local path would let the
+ //import read any file on the server
+ var image = await DownloadUrl.DownloadImage(pictureUrl, _securityConfig.PictureImportAllowedPrivateHosts);
+ if (image == null)
+ continue;
+
+ var pictureAlreadyExists = false;
+ if (!isNew)
{
- var mimeType = MimeTypeExtensions.GetMimeTypeFromFilePath(picturePath);
- var newPictureBinary = await File.ReadAllBytesAsync(picturePath);
- var pictureAlreadyExists = false;
- if (!isNew)
+ //compare with existing product pictures
+ var existingPictures = product.ProductPictures;
+ foreach (var existingPicture in existingPictures)
{
- //compare with existing product pictures
- var existingPictures = product.ProductPictures;
- foreach (var existingPicture in existingPictures)
- {
- var pp = await _pictureService.GetPictureById(existingPicture.PictureId);
- var existingBinary = await _pictureService.LoadPictureBinary(pp);
- //picture binary after validation (like in database)
- var validatedPictureBinary = _pictureService.ValidatePicture(newPictureBinary, mimeType);
- if (!existingBinary.SequenceEqual(validatedPictureBinary) &&
- !existingBinary.SequenceEqual(newPictureBinary)) continue;
- //the same picture content
- pictureAlreadyExists = true;
- break;
- }
+ var pp = await _pictureService.GetPictureById(existingPicture.PictureId);
+ var existingBinary = await _pictureService.LoadPictureBinary(pp);
+ //picture binary after validation (like in database)
+ var validatedPictureBinary = _pictureService.ValidatePicture(image.Binary, image.MimeType);
+ if (!existingBinary.SequenceEqual(validatedPictureBinary) &&
+ !existingBinary.SequenceEqual(image.Binary)) continue;
+ //the same picture content
+ pictureAlreadyExists = true;
+ break;
}
-
- if (pictureAlreadyExists) continue;
- var picture = await _pictureService.InsertPicture(newPictureBinary, mimeType,
- _pictureService.GetPictureSeName(product.Name), "", "", false,
- Reference.Product, product.Id);
- var productPicture = new ProductPicture {
- PictureId = picture.Id,
- DisplayOrder = 1
- };
- await _productService.InsertProductPicture(productPicture, product.Id);
- }
- else
- {
- var fileBinary = await DownloadUrl.DownloadFile(picturePath);
- if (fileBinary == null) continue;
- var mimeType = MimeTypeExtensions.GetMimeTypeFromFilePath(picturePath);
- var picture = await _pictureService.InsertPicture(fileBinary, mimeType,
- _pictureService.GetPictureSeName(product.Name), "", "", false, Reference.Product, product.Id);
- var productPicture = new ProductPicture {
- PictureId = picture.Id,
- DisplayOrder = 1
- };
- await _productService.InsertProductPicture(productPicture, product.Id);
}
+
+ if (pictureAlreadyExists) continue;
+ var picture = await _pictureService.InsertPicture(image.Binary, image.MimeType,
+ _pictureService.GetPictureSeName(product.Name), "", "", false,
+ Reference.Product, product.Id);
+ var productPicture = new ProductPicture {
+ PictureId = picture.Id,
+ DisplayOrder = 1
+ };
+ await _productService.InsertProductPicture(productPicture, product.Id);
}
}
diff --git a/src/Business/Grand.Business.Core/Utilities/System/DownloadUrl.cs b/src/Business/Grand.Business.Core/Utilities/System/DownloadUrl.cs
index c258f2dee..e652ea10e 100644
--- a/src/Business/Grand.Business.Core/Utilities/System/DownloadUrl.cs
+++ b/src/Business/Grand.Business.Core/Utilities/System/DownloadUrl.cs
@@ -1,13 +1,183 @@
-namespace Grand.Business.Core.Utilities.System;
+using System.Net;
+using System.Net.Sockets;
+namespace Grand.Business.Core.Utilities.System;
+
+/// <summary>
+/// Downloads images referenced by URL in import files. The URL comes from whoever prepared the file,
+/// so the server must not become a proxy into its own network (SSRF): only http(s) on ports 80/443 to
+/// publicly routable addresses is allowed, and only content that is actually an image is returned.
+/// Hosts on the internal network can be allowed explicitly by the server configuration.
+/// </summary>
public static class DownloadUrl
{
- public static async Task<byte[]> DownloadFile(string url)
+ private const int MaxResponseBytes = 10 * 1024 * 1024;
+ private static readonly TimeSpan RequestTimeout = TimeSpan.FromSeconds(30);
+ private static readonly HttpRequestOptionsKey<string[]> AllowedPrivateHostsKey = new("AllowedPrivateHosts");
+
+ private static readonly IPNetwork[] BlockedIPv4Networks = [
+ IPNetwork.Parse("0.0.0.0/8"), //0.0.0.0 reaches localhost on Linux
+ IPNetwork.Parse("127.0.0.0/8"), //loopback
+ IPNetwork.Parse("10.0.0.0/8"), //private network
+ IPNetwork.Parse("172.16.0.0/12"), //private network
+ IPNetwork.Parse("192.168.0.0/16"), //private network
+ IPNetwork.Parse("169.254.0.0/16"), //link-local, cloud metadata endpoints
+ IPNetwork.Parse("100.64.0.0/10") //carrier-grade NAT, used as an internal network by some clouds
+ ];
+
+ //only global unicast is public; within it 6to4 and Teredo embed an IPv4 address that could be private
+ private static readonly IPNetwork IPv6GlobalUnicast = IPNetwork.Parse("2000::/3");
+
+ private static readonly IPNetwork[] BlockedIPv6Networks = [
+ IPNetwork.Parse("2001::/32"), //Teredo
+ IPNetwork.Parse("2002::/16") //6to4
+ ];
+
+ //the address is validated when the socket is opened, not before the request, so a DNS answer
+ //that changes between the check and the connect (DNS rebinding) cannot redirect the request;
+ //redirects open their connections through the same callback
+ private static readonly HttpClient Client = new(new SocketsHttpHandler {
+ UseProxy = false,
+ AllowAutoRedirect = true,
+ MaxAutomaticRedirections = 3,
+ ConnectTimeout = TimeSpan.FromSeconds(10),
+ PooledConnectionLifetime = TimeSpan.FromMinutes(2),
+ ConnectCallback = ConnectToAllowedAddress
+ }) {
+ Timeout = Timeout.InfiniteTimeSpan
+ };
+
+ /// <summary>
+ /// Downloads an image from a public http(s) URL
+ /// </summary>
+ /// <param name="url">Image URL</param>
+ /// <param name="allowedPrivateHosts">Hosts that may resolve to a non-public address and use any port;
+ /// a leading "*." matches any subdomain</param>
+ /// <returns>Image binary with the MIME type detected from its content, or null when the URL is not allowed,
+ /// the download fails or the content is not a supported image</returns>
+ public static async Task<DownloadedImage> DownloadImage(string url, string[] allowedPrivateHosts = null)
{
- using var client = new HttpClient();
- using var result = await client.GetAsync(url);
- if (result.IsSuccessStatusCode) return await result.Content.ReadAsByteArrayAsync();
+ if (!Uri.TryCreate(url, UriKind.Absolute, out var uri) ||
+ (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps))
+ return null;
+
+ using var cts = new CancellationTokenSource(RequestTimeout);
+ try
+ {
+ //redirects reuse this request message, so every hop is checked against the same list
+ using var request = new HttpRequestMessage(HttpMethod.Get, uri);
+ request.Options.Set(AllowedPrivateHostsKey, allowedPrivateHosts ?? []);
+ using var response = await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cts.Token);
+ if (!response.IsSuccessStatusCode)
+ return null;
+
+ if (response.Content.Headers.ContentLength > MaxResponseBytes)
+ return null;
+
+ var binary = await ReadLimited(response.Content, cts.Token);
+ if (binary == null)
+ return null;
+ var mimeType = DetectImageMimeType(binary);
+ return mimeType == null ? null : new DownloadedImage(binary, mimeType);
+ }
+ catch (Exception ex) when (ex is HttpRequestException or OperationCanceledException or IOException)
+ {
+ return null;
+ }
+ }
+
+ /// <summary>
+ /// Returns the MIME type of a JPEG, PNG, GIF or WebP image based on its signature, or null for any other content
+ /// </summary>
+ public static string DetectImageMimeType(ReadOnlySpan<byte> binary)
+ {
+ if (binary.StartsWith((ReadOnlySpan<byte>)[0xFF, 0xD8, 0xFF]))
+ return "image/jpeg";
+ if (binary.StartsWith((ReadOnlySpan<byte>)[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]))
+ return "image/png";
+ if (binary.StartsWith("GIF87a"u8) || binary.StartsWith("GIF89a"u8))
+ return "image/gif";
+ if (binary.Length >= 12 && binary.StartsWith("RIFF"u8) && binary.Slice(8, 4).SequenceEqual("WEBP"u8))
+ return "image/webp";
return null;
}
-}
\ No newline at end of file
+
+ /// <summary>
+ /// Returns true when the address is publicly routable (not loopback, private, link-local, reserved etc.)
+ /// </summary>
+ public static bool IsPublicAddress(IPAddress address)
+ {
+ if (address.IsIPv4MappedToIPv6)
+ address = address.MapToIPv4();
+
+ return address.AddressFamily switch {
+ AddressFamily.InterNetwork => !BlockedIPv4Networks.Any(n => n.Contains(address)),
+ AddressFamily.InterNetworkV6 => IPv6GlobalUnicast.Contains(address) &&
+ !BlockedIPv6Networks.Any(n => n.Contains(address)),
+ _ => false
+ };
+ }
+
+ private static async ValueTask<Stream> ConnectToAllowedAddress(SocketsHttpConnectionContext context,
+ CancellationToken cancellationToken)
+ {
+ var endpoint = context.DnsEndPoint;
+ context.InitialRequestMessage.Options.TryGetValue(AllowedPrivateHostsKey, out var allowedPrivateHosts);
+ var isAllowedPrivateHost = allowedPrivateHosts != null &&
+ allowedPrivateHosts.Any(allowed => IsHostMatch(endpoint.Host, allowed));
+
+ if (!isAllowedPrivateHost && endpoint.Port is not (80 or 443))
+ throw new HttpRequestException($"Port {endpoint.Port} is not allowed");
+
+ IPAddress[] addresses = IPAddress.TryParse(endpoint.Host, out var literal)
+ ? [literal]
+ : await Dns.GetHostAddressesAsync(endpoint.Host, cancellationToken);
+
+ //reject when any address is non-public, so a public record cannot smuggle in a private one
+ if (addresses.Length == 0 || (!isAllowedPrivateHost && !addresses.All(IsPublicAddress)))
+ throw new HttpRequestException($"Host {endpoint.Host} is not allowed");
+
+ var socket = new Socket(SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
+ try
+ {
+ await socket.ConnectAsync(addresses, endpoint.Port, cancellationToken);
+ return new NetworkStream(socket, true);
+ }
+ catch
+ {
+ socket.Dispose();
+ throw;
+ }
+ }
+
+ private static bool IsHostMatch(string host, string allowed)
+ {
+ if (string.IsNullOrWhiteSpace(allowed))
+ return false;
+
+ allowed = allowed.Trim();
+ if (allowed.StartsWith("*.", StringComparison.Ordinal))
+ return host.EndsWith(allowed[1..], StringComparison.OrdinalIgnoreCase);
+
+ return string.Equals(host, allowed, StringComparison.OrdinalIgnoreCase);
+ }
+
+ private static async Task<byte[]> ReadLimited(HttpContent content, CancellationToken cancellationToken)
+ {
+ await using var stream = await content.ReadAsStreamAsync(cancellationToken);
+ using var buffer = new MemoryStream();
+ var chunk = new byte[81920];
+ int read;
+ while ((read = await stream.ReadAsync(chunk, cancellationToken)) > 0)
+ {
+ if (buffer.Length + read > MaxResponseBytes)
+ return null;
+ buffer.Write(chunk, 0, read);
+ }
+
+ return buffer.ToArray();
+ }
+}
+
+public record DownloadedImage(byte[] Binary, string MimeType);
diff --git a/src/Core/Grand.Infrastructure/Configuration/SecurityConfig.cs b/src/Core/Grand.Infrastructure/Configuration/SecurityConfig.cs
index db88abcbf..f3b7dfad6 100644
--- a/src/Core/Grand.Infrastructure/Configuration/SecurityConfig.cs
+++ b/src/Core/Grand.Infrastructure/Configuration/SecurityConfig.cs
@@ -102,6 +102,14 @@ public class SecurityConfig
/// </summary>
public string[] SanitizerAllowedIframeHosts { get; set; }
+ /// <summary>
+ /// Hosts on the internal network that picture URLs in Excel imports may point to. Any other host must resolve to
+ /// a public address, because the URL is written by whoever prepared the import file and would otherwise let it
+ /// reach services only the server can see. A listed host may also use a port other than 80/443.
+ /// Matching is case-insensitive on the host only; a leading "*." matches any subdomain. Empty by default.
+ /// </summary>
+ public string[] PictureImportAllowedPrivateHosts { get; set; }
+
/// <summary>
/// Gets or sets a value indicating whether [SanitizeHtml] and [NoHtml] reject markup on save. Default true.
/// This is an operational escape hatch, not a security setting: turn it off only temporarily, if the
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/BrandImportDataObjectTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/BrandImportDataObjectTests.cs
index 90dd49d9b..e82606405 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/BrandImportDataObjectTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/BrandImportDataObjectTests.cs
@@ -1,4 +1,4 @@
-using Grand.Mapping;
+using Grand.Mapping;
using Grand.Business.Catalog.Services.Brands;
using Grand.Business.Catalog.Services.ExportImport;
using Grand.Business.Common.Services.Seo;
@@ -64,7 +64,8 @@ public class BrandImportDataObjectTests
new AccessControlConfig());
_seNameService = new SeNameService(_slugServiceMock.Object, _languageServiceMock.Object, new SeoSettings());
_brandImportDataObject = new BrandImportDataObject(_brandService, _pictureServiceMock.Object,
- _brandLayoutServiceMock.Object, _slugServiceMock.Object, _seNameService);
+ _brandLayoutServiceMock.Object, _slugServiceMock.Object, _seNameService,
+ new SecurityConfig());
}
[TestMethod]
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CategoryImportDataObjectTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CategoryImportDataObjectTests.cs
index 978923252..044b14184 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CategoryImportDataObjectTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CategoryImportDataObjectTests.cs
@@ -1,4 +1,4 @@
-using Grand.Mapping;
+using Grand.Mapping;
using Grand.Business.Catalog.Services.Categories;
using Grand.Business.Catalog.Services.ExportImport;
using Grand.Business.Common.Services.Security;
@@ -65,7 +65,8 @@ public class CategoryImportDataObjectTests
new AclService(new AccessControlConfig()), new AccessControlConfig());
_seNameService = new SeNameService(_slugServiceMock.Object, _languageServiceMock.Object, new SeoSettings());
_categoryImportDataObject = new CategoryImportDataObject(_categoryService, _pictureServiceMock.Object,
- _categoryLayoutServiceMock.Object, _slugServiceMock.Object, _seNameService);
+ _categoryLayoutServiceMock.Object, _slugServiceMock.Object, _seNameService,
+ new SecurityConfig());
}
[TestMethod]
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CollectionImportDataObjectTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CollectionImportDataObjectTests.cs
index 48286ab80..d5e069770 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CollectionImportDataObjectTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CollectionImportDataObjectTests.cs
@@ -1,4 +1,4 @@
-using Grand.Mapping;
+using Grand.Mapping;
using Grand.Business.Catalog.Services.Collections;
using Grand.Business.Catalog.Services.ExportImport;
using Grand.Business.Common.Services.Security;
@@ -65,7 +65,8 @@ public class CollectionImportDataObjectTests
_mediatorMock.Object, new AclService(new AccessControlConfig()), new AccessControlConfig());
_seNameService = new SeNameService(_slugServiceMock.Object, _languageServiceMock.Object, new SeoSettings());
_collectionImportDataObject = new CollectionImportDataObject(_collectionService, _pictureServiceMock.Object,
- _collectionLayoutServiceMock.Object, _slugServiceMock.Object, _seNameService);
+ _collectionLayoutServiceMock.Object, _slugServiceMock.Object, _seNameService,
+ new SecurityConfig());
}
[TestMethod]
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/DownloadUrlTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/DownloadUrlTests.cs
new file mode 100644
index 000000000..9ec385e42
--- /dev/null
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/DownloadUrlTests.cs
@@ -0,0 +1,165 @@
+using System.Net;
+using System.Net.Sockets;
+using Grand.Business.Core.Utilities.System;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Grand.Business.Catalog.Tests.Services.ExportImport;
+
+[TestClass]
+public class DownloadUrlTests
+{
+ [TestMethod]
+ [DataRow("127.0.0.1")]
+ [DataRow("0.0.0.0")]
+ [DataRow("10.1.2.3")]
+ [DataRow("100.64.0.1")]
+ [DataRow("169.254.169.254")]
+ [DataRow("172.16.0.1")]
+ [DataRow("172.31.255.255")]
+ [DataRow("192.168.1.1")]
+ [DataRow("::")]
+ [DataRow("::1")]
+ [DataRow("fe80::1")]
+ [DataRow("fc00::1")]
+ [DataRow("fd12:3456::1")]
+ [DataRow("ff02::1")]
+ [DataRow("::ffff:127.0.0.1")]
+ [DataRow("::ffff:169.254.169.254")]
+ [DataRow("64:ff9b::a9fe:a9fe")]
+ [DataRow("2002:7f00:1::1")]
+ [DataRow("2001:0:4136:e378::1")]
+ public void IsPublicAddress_NonPublicAddress_ReturnsFalse(string address)
+ {
+ Assert.IsFalse(DownloadUrl.IsPublicAddress(IPAddress.Parse(address)));
+ }
+
+ [TestMethod]
+ [DataRow("8.8.8.8")]
+ [DataRow("1.1.1.1")]
+ [DataRow("172.32.0.1")]
+ [DataRow("::ffff:8.8.8.8")]
+ [DataRow("2606:4700:4700::1111")]
+ public void IsPublicAddress_PublicAddress_ReturnsTrue(string address)
+ {
+ Assert.IsTrue(DownloadUrl.IsPublicAddress(IPAddress.Parse(address)));
+ }
+
+ [TestMethod]
+ [DataRow("http://127.0.0.1/image.png")]
+ [DataRow("http://169.254.169.254/latest/meta-data/")]
+ [DataRow("http://10.0.0.5/image.png")]
+ [DataRow("http://192.168.1.5/image.png")]
+ [DataRow("http://[::1]/image.png")]
+ [DataRow("http://[::ffff:127.0.0.1]/image.png")]
+ [DataRow("http://0x7f.1/image.png")]
+ [DataRow("http://localhost/image.png")]
+ [DataRow("http://example.com:8080/image.png")]
+ [DataRow("http://nonexistent.invalid/image.png")]
+ public async Task DownloadImage_DisallowedTarget_ReturnsNull(string url)
+ {
+ Assert.IsNull(await DownloadUrl.DownloadImage(url));
+ }
+
+ [TestMethod]
+ [DataRow("file:///etc/passwd")]
+ [DataRow("ftp://example.com/image.png")]
+ [DataRow("/etc/passwd")]
+ [DataRow(@"C:\Windows\win.ini")]
+ [DataRow(@"\\server\share\image.png")]
+ [DataRow("not a url")]
+ public async Task DownloadImage_NotHttpUrl_ReturnsNull(string url)
+ {
+ Assert.IsNull(await DownloadUrl.DownloadImage(url));
+ }
+
+ [TestMethod]
+ [DataRow(new byte[] { 0xFF, 0xD8, 0xFF, 0xE0, 0x00 }, "image/jpeg")]
+ [DataRow(new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00 }, "image/png")]
+ [DataRow(new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01 }, "image/gif")]
+ [DataRow(new byte[] { 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50 }, "image/webp")]
+ public void DetectImageMimeType_SupportedImage_ReturnsMimeType(byte[] binary, string expected)
+ {
+ Assert.AreEqual(expected, DownloadUrl.DetectImageMimeType(binary));
+ }
+
+ [TestMethod]
+ [DataRow("{\"AccessKeyId\":\"secret\"}")]
+ [DataRow("<html><body>internal</body></html>")]
+ [DataRow("<svg xmlns=\"http://www.w3.org/2000/svg\"><script>alert(1)</script></svg>")]
+ [DataRow("")]
+ public void DetectImageMimeType_NotAnImage_ReturnsNull(string content)
+ {
+ Assert.IsNull(DownloadUrl.DetectImageMimeType(Encoding.UTF8.GetBytes(content)));
+ }
+
+ [TestMethod]
+ [DataRow("127.0.0.1", "127.0.0.1")]
+ [DataRow("localhost", "LOCALHOST")]
+ public async Task DownloadImage_AllowedPrivateHost_DownloadsFromInternalServer(string host, string allowedHost)
+ {
+ using var server = new LocalImageServer();
+
+ var result = await DownloadUrl.DownloadImage($"http://{host}:{server.Port}/image.png", [allowedHost]);
+
+ Assert.IsNotNull(result);
+ Assert.AreEqual("image/png", result.MimeType);
+ CollectionAssert.AreEqual(LocalImageServer.Png, result.Binary);
+ }
+
+ [TestMethod]
+ [DataRow(null)]
+ [DataRow(new[] { "images.example.local", "*.example.local", " " })]
+ public async Task DownloadImage_InternalServerNotAllowed_NeverConnects(string[] allowedHosts)
+ {
+ using var server = new LocalImageServer();
+
+ var result = await DownloadUrl.DownloadImage($"http://127.0.0.1:{server.Port}/image.png", allowedHosts);
+
+ Assert.IsNull(result);
+ Assert.AreEqual(0, server.Connections);
+ }
+
+ private sealed class LocalImageServer : IDisposable
+ {
+ public static readonly byte[] Png = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x01, 0x02, 0x03];
+
+ private readonly TcpListener _listener = new(IPAddress.Loopback, 0);
+ private int _connections;
+
+ public LocalImageServer()
+ {
+ _listener.Start();
+ _ = Serve();
+ }
+
+ public int Port => ((IPEndPoint)_listener.LocalEndpoint).Port;
+ public int Connections => _connections;
+
+ private async Task Serve()
+ {
+ try
+ {
+ while (true)
+ {
+ using var client = await _listener.AcceptTcpClientAsync();
+ Interlocked.Increment(ref _connections);
+ var stream = client.GetStream();
+ await stream.ReadAsync(new byte[4096]);
+ var header = "HTTP/1.1 200 OK\r\nContent-Type: image/png\r\n" +
+ $"Content-Length: {Png.Length}\r\nConnection: close\r\n\r\n";
+ await stream.WriteAsync(Encoding.ASCII.GetBytes(header));
+ await stream.WriteAsync(Png);
+ }
+ }
+ catch (Exception ex) when (ex is SocketException or ObjectDisposedException)
+ {
+ //listener stopped
+ }
+ }
+
+ public void Dispose()
+ {
+ _listener.Stop();
+ }
+ }
+}
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/ProductImportDataObjectTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/ProductImportDataObjectTests.cs
index adb1088f0..502e991fe 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/ProductImportDataObjectTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/ProductImportDataObjectTests.cs
@@ -99,7 +99,8 @@ public class ProductImportDataObjectTests
_categoryServiceMock.Object, _productCategoryServiceMock.Object, _brandServiceMock.Object,
_collectionServiceMock.Object,
_productCollectionServiceMock.Object,
- _seNameService);
+ _seNameService,
+ new SecurityConfig());
}
[TestMethod]
diff --git a/src/Web/Grand.Web.Admin/App_Data/appsettings.json b/src/Web/Grand.Web.Admin/App_Data/appsettings.json
index abc2b81cc..9bbe5b436 100644
--- a/src/Web/Grand.Web.Admin/App_Data/appsettings.json
+++ b/src/Web/Grand.Web.Admin/App_Data/appsettings.json
@@ -83,6 +83,10 @@
//An iframe pointing anywhere else is removed, because its src is otherwise attacker-controlled. A leading "*." matches any subdomain.
//Remove the key entirely to use the built-in video-embed defaults (youtube, youtube-nocookie, vimeo, google); set it to [] to block every iframe.
"SanitizerAllowedIframeHosts": [ "youtube.com", "*.youtube.com", "youtube-nocookie.com", "*.youtube-nocookie.com", "vimeo.com", "*.vimeo.com", "google.com", "*.google.com" ],
+ //Internal-network hosts that picture URLs in Excel imports (products, brands, categories, collections) may point to.
+ //Any other host must resolve to a public address, because the URL is written by whoever prepared the import file.
+ //A listed host may also use a port other than 80/443. A leading "*." matches any subdomain, e.g. [ "images.company.local", "192.168.1.50" ].
+ "PictureImportAllowedPrivateHosts": [],
//Operational escape hatch, not a security setting: when [SanitizeHtml]/[NoHtml] wrongly reject legitimate
//content in production, set this to false to accept input unsanitized while a fix is prepared, then set it
//back to true as soon as possible. Default true.
diff --git a/src/Web/Grand.Web.Store/App_Data/appsettings.json b/src/Web/Grand.Web.Store/App_Data/appsettings.json
index 7d37b6440..3a468b3fc 100644
--- a/src/Web/Grand.Web.Store/App_Data/appsettings.json
+++ b/src/Web/Grand.Web.Store/App_Data/appsettings.json
@@ -83,6 +83,10 @@
//An iframe pointing anywhere else is removed, because its src is otherwise attacker-controlled. A leading "*." matches any subdomain.
//Remove the key entirely to use the built-in video-embed defaults (youtube, youtube-nocookie, vimeo, google); set it to [] to block every iframe.
"SanitizerAllowedIframeHosts": [ "youtube.com", "*.youtube.com", "youtube-nocookie.com", "*.youtube-nocookie.com", "vimeo.com", "*.vimeo.com", "google.com", "*.google.com" ],
+ //Internal-network hosts that picture URLs in Excel imports (products, brands, categories, collections) may point to.
+ //Any other host must resolve to a public address, because the URL is written by whoever prepared the import file.
+ //A listed host may also use a port other than 80/443. A leading "*." matches any subdomain, e.g. [ "images.company.local", "192.168.1.50" ].
+ "PictureImportAllowedPrivateHosts": [],
//Operational escape hatch, not a security setting: when [SanitizeHtml]/[NoHtml] wrongly reject legitimate
//content in production, set this to false to accept input unsanitized while a fix is prepared, then set it
//back to true as soon as possible. Default true.
diff --git a/src/Web/Grand.Web.Vendor/App_Data/appsettings.json b/src/Web/Grand.Web.Vendor/App_Data/appsettings.json
index abc2b81cc..9bbe5b436 100644
--- a/src/Web/Grand.Web.Vendor/App_Data/appsettings.json
+++ b/src/Web/Grand.Web.Vendor/App_Data/appsettings.json
@@ -83,6 +83,10 @@
//An iframe pointing anywhere else is removed, because its src is otherwise attacker-controlled. A leading "*." matches any subdomain.
//Remove the key entirely to use the built-in video-embed defaults (youtube, youtube-nocookie, vimeo, google); set it to [] to block every iframe.
"SanitizerAllowedIframeHosts": [ "youtube.com", "*.youtube.com", "youtube-nocookie.com", "*.youtube-nocookie.com", "vimeo.com", "*.vimeo.com", "google.com", "*.google.com" ],
+ //Internal-network hosts that picture URLs in Excel imports (products, brands, categories, collections) may point to.
+ //Any other host must resolve to a public address, because the URL is written by whoever prepared the import file.
+ //A listed host may also use a port other than 80/443. A leading "*." matches any subdomain, e.g. [ "images.company.local", "192.168.1.50" ].
+ "PictureImportAllowedPrivateHosts": [],
//Operational escape hatch, not a security setting: when [SanitizeHtml]/[NoHtml] wrongly reject legitimate
//content in production, set this to false to accept input unsanitized while a fix is prepared, then set it
//back to true as soon as possible. Default true.
diff --git a/src/Web/Grand.Web/App_Data/appsettings.json b/src/Web/Grand.Web/App_Data/appsettings.json
index 5861e41ae..cb8b0e50a 100644
--- a/src/Web/Grand.Web/App_Data/appsettings.json
+++ b/src/Web/Grand.Web/App_Data/appsettings.json
@@ -100,6 +100,10 @@
//An iframe pointing anywhere else is removed, because its src is otherwise attacker-controlled. A leading "*." matches any subdomain.
//Remove the key entirely to use the built-in video-embed defaults (youtube, youtube-nocookie, vimeo, google); set it to [] to block every iframe.
"SanitizerAllowedIframeHosts": [ "youtube.com", "*.youtube.com", "youtube-nocookie.com", "*.youtube-nocookie.com", "vimeo.com", "*.vimeo.com", "google.com", "*.google.com" ],
+ //Internal-network hosts that picture URLs in Excel imports (products, brands, categories, collections) may point to.
+ //Any other host must resolve to a public address, because the URL is written by whoever prepared the import file.
+ //A listed host may also use a port other than 80/443. A leading "*." matches any subdomain, e.g. [ "images.company.local", "192.168.1.50" ].
+ "PictureImportAllowedPrivateHosts": [],
//Operational escape hatch, not a security setting: when [SanitizeHtml]/[NoHtml] wrongly reject legitimate
//content in production, set this to false to accept input unsanitized while a fix is prepared, then set it
//back to true as soon as possible. Default true.