mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 22:44:29 +01:00
Relax "Insecure transport protocol to access this service, please use HTTPS or TOR" error in server setting services
This commit is contained in:
@@ -2657,27 +2657,28 @@ donation:
|
|||||||
{
|
{
|
||||||
var unusedUri = new Uri("https://toto.com");
|
var unusedUri = new Uri("https://toto.com");
|
||||||
Assert.True(ExternalConnectionString.TryParse("server=/test", out var connStr, out var error));
|
Assert.True(ExternalConnectionString.TryParse("server=/test", out var connStr, out var error));
|
||||||
var expanded = await connStr.Expand(new Uri("https://toto.com"), ExternalServiceTypes.Charge);
|
var expanded = await connStr.Expand(new Uri("https://toto.com"), ExternalServiceTypes.Charge, NetworkType.Mainnet);
|
||||||
Assert.Equal(new Uri("https://toto.com/test"), expanded.Server);
|
Assert.Equal(new Uri("https://toto.com/test"), expanded.Server);
|
||||||
expanded = await connStr.Expand(new Uri("http://toto.onion"), ExternalServiceTypes.Charge);
|
expanded = await connStr.Expand(new Uri("http://toto.onion"), ExternalServiceTypes.Charge, NetworkType.Mainnet);
|
||||||
Assert.Equal(new Uri("http://toto.onion/test"), expanded.Server);
|
Assert.Equal(new Uri("http://toto.onion/test"), expanded.Server);
|
||||||
await Assert.ThrowsAsync<SecurityException>(() => connStr.Expand(new Uri("http://toto.com"), ExternalServiceTypes.Charge));
|
await Assert.ThrowsAsync<SecurityException>(() => connStr.Expand(new Uri("http://toto.com"), ExternalServiceTypes.Charge, NetworkType.Mainnet));
|
||||||
|
await connStr.Expand(new Uri("http://toto.com"), ExternalServiceTypes.Charge, NetworkType.Testnet);
|
||||||
|
|
||||||
// Make sure absolute paths are not expanded
|
// Make sure absolute paths are not expanded
|
||||||
Assert.True(ExternalConnectionString.TryParse("server=https://tow/test", out connStr, out error));
|
Assert.True(ExternalConnectionString.TryParse("server=https://tow/test", out connStr, out error));
|
||||||
expanded = await connStr.Expand(new Uri("https://toto.com"), ExternalServiceTypes.Charge);
|
expanded = await connStr.Expand(new Uri("https://toto.com"), ExternalServiceTypes.Charge, NetworkType.Mainnet);
|
||||||
Assert.Equal(new Uri("https://tow/test"), expanded.Server);
|
Assert.Equal(new Uri("https://tow/test"), expanded.Server);
|
||||||
|
|
||||||
// Error if directory not exists
|
// Error if directory not exists
|
||||||
Assert.True(ExternalConnectionString.TryParse($"server={unusedUri};macaroondirectorypath=pouet", out connStr, out error));
|
Assert.True(ExternalConnectionString.TryParse($"server={unusedUri};macaroondirectorypath=pouet", out connStr, out error));
|
||||||
await Assert.ThrowsAsync<DirectoryNotFoundException>(() => connStr.Expand(unusedUri, ExternalServiceTypes.LNDGRPC));
|
await Assert.ThrowsAsync<DirectoryNotFoundException>(() => connStr.Expand(unusedUri, ExternalServiceTypes.LNDGRPC, NetworkType.Mainnet));
|
||||||
await Assert.ThrowsAsync<DirectoryNotFoundException>(() => connStr.Expand(unusedUri, ExternalServiceTypes.LNDRest));
|
await Assert.ThrowsAsync<DirectoryNotFoundException>(() => connStr.Expand(unusedUri, ExternalServiceTypes.LNDRest, NetworkType.Mainnet));
|
||||||
await connStr.Expand(unusedUri, ExternalServiceTypes.Charge);
|
await connStr.Expand(unusedUri, ExternalServiceTypes.Charge, NetworkType.Mainnet);
|
||||||
|
|
||||||
var macaroonDirectory = CreateDirectory();
|
var macaroonDirectory = CreateDirectory();
|
||||||
Assert.True(ExternalConnectionString.TryParse($"server={unusedUri};macaroondirectorypath={macaroonDirectory}", out connStr, out error));
|
Assert.True(ExternalConnectionString.TryParse($"server={unusedUri};macaroondirectorypath={macaroonDirectory}", out connStr, out error));
|
||||||
await connStr.Expand(unusedUri, ExternalServiceTypes.LNDGRPC);
|
await connStr.Expand(unusedUri, ExternalServiceTypes.LNDGRPC, NetworkType.Mainnet);
|
||||||
expanded = await connStr.Expand(unusedUri, ExternalServiceTypes.LNDRest);
|
expanded = await connStr.Expand(unusedUri, ExternalServiceTypes.LNDRest, NetworkType.Mainnet);
|
||||||
Assert.NotNull(expanded.Macaroons);
|
Assert.NotNull(expanded.Macaroons);
|
||||||
Assert.Null(expanded.MacaroonFilePath);
|
Assert.Null(expanded.MacaroonFilePath);
|
||||||
Assert.Null(expanded.Macaroons.AdminMacaroon);
|
Assert.Null(expanded.Macaroons.AdminMacaroon);
|
||||||
@@ -2687,7 +2688,7 @@ donation:
|
|||||||
File.WriteAllBytes($"{macaroonDirectory}/admin.macaroon", new byte[] { 0xaa });
|
File.WriteAllBytes($"{macaroonDirectory}/admin.macaroon", new byte[] { 0xaa });
|
||||||
File.WriteAllBytes($"{macaroonDirectory}/invoice.macaroon", new byte[] { 0xab });
|
File.WriteAllBytes($"{macaroonDirectory}/invoice.macaroon", new byte[] { 0xab });
|
||||||
File.WriteAllBytes($"{macaroonDirectory}/readonly.macaroon", new byte[] { 0xac });
|
File.WriteAllBytes($"{macaroonDirectory}/readonly.macaroon", new byte[] { 0xac });
|
||||||
expanded = await connStr.Expand(unusedUri, ExternalServiceTypes.LNDRest);
|
expanded = await connStr.Expand(unusedUri, ExternalServiceTypes.LNDRest, NetworkType.Mainnet);
|
||||||
Assert.NotNull(expanded.Macaroons.AdminMacaroon);
|
Assert.NotNull(expanded.Macaroons.AdminMacaroon);
|
||||||
Assert.NotNull(expanded.Macaroons.InvoiceMacaroon);
|
Assert.NotNull(expanded.Macaroons.InvoiceMacaroon);
|
||||||
Assert.Equal("ab", expanded.Macaroons.InvoiceMacaroon.Hex);
|
Assert.Equal("ab", expanded.Macaroons.InvoiceMacaroon.Hex);
|
||||||
@@ -2696,7 +2697,7 @@ donation:
|
|||||||
|
|
||||||
Assert.True(ExternalConnectionString.TryParse($"server={unusedUri};cookiefilepath={macaroonDirectory}/charge.cookie", out connStr, out error));
|
Assert.True(ExternalConnectionString.TryParse($"server={unusedUri};cookiefilepath={macaroonDirectory}/charge.cookie", out connStr, out error));
|
||||||
File.WriteAllText($"{macaroonDirectory}/charge.cookie", "apitoken");
|
File.WriteAllText($"{macaroonDirectory}/charge.cookie", "apitoken");
|
||||||
expanded = await connStr.Expand(unusedUri, ExternalServiceTypes.Charge);
|
expanded = await connStr.Expand(unusedUri, ExternalServiceTypes.Charge, NetworkType.Mainnet);
|
||||||
Assert.Equal("apitoken", expanded.APIToken);
|
Assert.Equal("apitoken", expanded.APIToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using BTCPayServer.Controllers;
|
using BTCPayServer.Controllers;
|
||||||
|
using NBitcoin;
|
||||||
|
|
||||||
namespace BTCPayServer.Configuration
|
namespace BTCPayServer.Configuration
|
||||||
{
|
{
|
||||||
@@ -30,13 +31,16 @@ namespace BTCPayServer.Configuration
|
|||||||
/// Return a connectionString which does not depends on external resources or information like relative path or file path
|
/// Return a connectionString which does not depends on external resources or information like relative path or file path
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<ExternalConnectionString> Expand(Uri absoluteUrlBase, ExternalServiceTypes serviceType)
|
public async Task<ExternalConnectionString> Expand(Uri absoluteUrlBase, ExternalServiceTypes serviceType, NetworkType network)
|
||||||
{
|
{
|
||||||
var connectionString = this.Clone();
|
var connectionString = this.Clone();
|
||||||
// Transform relative URI into absolute URI
|
// Transform relative URI into absolute URI
|
||||||
var serviceUri = connectionString.Server.IsAbsoluteUri ? connectionString.Server : ToRelative(absoluteUrlBase, connectionString.Server.ToString());
|
var serviceUri = connectionString.Server.IsAbsoluteUri ? connectionString.Server : ToRelative(absoluteUrlBase, connectionString.Server.ToString());
|
||||||
if (!serviceUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase) &&
|
var isSecure = network != NetworkType.Mainnet ||
|
||||||
!serviceUri.DnsSafeHost.EndsWith(".onion", StringComparison.OrdinalIgnoreCase))
|
serviceUri.Scheme == "https" ||
|
||||||
|
serviceUri.DnsSafeHost.EndsWith(".onion", StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
Extensions.IsLocalNetwork(serviceUri.DnsSafeHost);
|
||||||
|
if (!isSecure)
|
||||||
{
|
{
|
||||||
throw new System.Security.SecurityException($"Insecure transport protocol to access this service, please use HTTPS or TOR");
|
throw new System.Security.SecurityException($"Insecure transport protocol to access this service, please use HTTPS or TOR");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -604,7 +604,7 @@ namespace BTCPayServer.Controllers
|
|||||||
ServiceLink = service.ConnectionString.Server.AbsoluteUri.WithoutEndingSlash()
|
ServiceLink = service.ConnectionString.Server.AbsoluteUri.WithoutEndingSlash()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
var connectionString = await service.ConnectionString.Expand(this.Request.GetAbsoluteUriNoPathBase(), service.Type);
|
var connectionString = await service.ConnectionString.Expand(this.Request.GetAbsoluteUriNoPathBase(), service.Type, _Options.NetworkType);
|
||||||
switch (service.Type)
|
switch (service.Type)
|
||||||
{
|
{
|
||||||
case ExternalServiceTypes.Charge:
|
case ExternalServiceTypes.Charge:
|
||||||
@@ -720,7 +720,7 @@ namespace BTCPayServer.Controllers
|
|||||||
ExternalConnectionString connectionString = null;
|
ExternalConnectionString connectionString = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
connectionString = await service.ConnectionString.Expand(this.Request.GetAbsoluteUriNoPathBase(), service.Type);
|
connectionString = await service.ConnectionString.Expand(this.Request.GetAbsoluteUriNoPathBase(), service.Type, _Options.NetworkType);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user