Implemented escaping expanded environment variables when they are used inside regular expressions.
This fixes #1500.
This commit is contained in:
@@ -906,13 +906,40 @@ namespace Duplicati.Library.Utility
|
||||
/// <summary>
|
||||
/// Expands environment variables, including the tilde character
|
||||
/// </summary>
|
||||
/// <returns>The environment variables.</returns>
|
||||
/// <returns>The expanded string.</returns>
|
||||
/// <param name="str">The string to expand.</param>
|
||||
public static string ExpandEnvironmentVariables(string str)
|
||||
{
|
||||
return Environment.ExpandEnvironmentVariables(str.Replace("~", HOME_PATH));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Regexp for matching environment variables on Windows (%VAR%)
|
||||
/// </summary>
|
||||
private static readonly Regex ENVIRONMENT_VARIABLE_MATCHER_WINDOWS = new Regex(@"\%(?<name>\w+)\%");
|
||||
|
||||
/// <summary>
|
||||
/// Regexp for matching environment variables on Linux ($VAR or ${VAR})
|
||||
/// </summary>
|
||||
private static readonly Regex ENVIRONMENT_VARIABLE_MATCHER_LINUX = new Regex(@"\$(?<name>\w+)|(\{(?<name>[^\}]+)\})");
|
||||
|
||||
/// <summary>
|
||||
/// Expands environment variables, including the tilde character, in a RegExp safe format
|
||||
/// </summary>
|
||||
/// <returns>The expanded string.</returns>
|
||||
/// <param name="str">The string to expand.</param>
|
||||
public static string ExpandEnvironmentVariablesRegexp(string str)
|
||||
{
|
||||
return
|
||||
|
||||
// TODO: Should we switch to using the native format, instead of following the Windows scheme?
|
||||
//IsClientLinux ? ENVIRONMENT_VARIABLE_MATCHER_LINUX : ENVIRONMENT_VARIABLE_MATCHER_WINDOWS
|
||||
|
||||
ENVIRONMENT_VARIABLE_MATCHER_WINDOWS
|
||||
.Replace(str.Replace("~", Regex.Escape(HOME_PATH)), (m) =>
|
||||
Regex.Escape(Environment.GetEnvironmentVariable(m.Groups["name"].Value)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks that a hostname is valid
|
||||
/// </summary>
|
||||
|
||||
@@ -654,7 +654,7 @@ namespace Duplicati.Server
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
|
||||
private static Duplicati.Library.Utility.IFilter ApplyFilter(Duplicati.Server.Serialization.Interface.IBackup backup, DuplicatiOperation mode, Duplicati.Library.Utility.IFilter filter)
|
||||
{
|
||||
var f2 = backup.Filters;
|
||||
@@ -662,7 +662,10 @@ namespace Duplicati.Server
|
||||
{
|
||||
var nf =
|
||||
(from n in f2
|
||||
let exp = Library.Utility.Utility.ExpandEnvironmentVariables(n.Expression)
|
||||
let exp =
|
||||
n.Expression.StartsWith("[") && n.Expression.EndsWith("]")
|
||||
? Library.Utility.Utility.ExpandEnvironmentVariablesRegexp(n.Expression)
|
||||
: Library.Utility.Utility.ExpandEnvironmentVariables(n.Expression)
|
||||
orderby n.Order
|
||||
select (Duplicati.Library.Utility.IFilter)(new Duplicati.Library.Utility.FilterExpression(exp, n.Include)))
|
||||
.Aggregate((a, b) => Duplicati.Library.Utility.FilterExpression.Combine(a, b));
|
||||
|
||||
Reference in New Issue
Block a user