-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathApplicationParametersParser.cs
More file actions
172 lines (136 loc) · 6.95 KB
/
Copy pathApplicationParametersParser.cs
File metadata and controls
172 lines (136 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using CodeLess.Interfaces;
using DCL.Diagnostics;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using UnityEngine;
namespace Global.AppArgs
{
[AutoInterface]
public class ApplicationParametersParser : IAppArgs
{
private readonly Dictionary<string, string> appParameters = new ();
private static readonly IReadOnlyDictionary<string, string> ALWAYS_IN_EDITOR = new Dictionary<string, string>
{
[AppArgsFlags.DEBUG] = string.Empty,
};
public ApplicationParametersParser() : this(Environment.GetCommandLineArgs()) { }
public ApplicationParametersParser(string[] args) : this(true, args) { }
public ApplicationParametersParser(bool useInEditorFlags = true, params string[] args)
{
ParseApplicationParameters(args);
if (useInEditorFlags && Application.isEditor)
AddAlwaysInEditorFlags();
LogArguments();
}
public bool HasFlag(string flagName) =>
appParameters.ContainsKey(flagName);
public bool TryGetValue(string flagName, out string? value) =>
appParameters.TryGetValue(flagName, out value);
public IEnumerable<string> Flags() =>
appParameters.Keys;
public IReadOnlyDictionary<string, string> Args() =>
appParameters;
private void AddAlwaysInEditorFlags()
{
foreach ((string? key, string? value) in ALWAYS_IN_EDITOR)
appParameters.TryAdd(key, value);
}
private void ParseApplicationParameters(string[] cmdArgs)
{
var deepLinkFound = false;
string lastKeyStored = string.Empty;
foreach (string arg in cmdArgs)
{
if (arg.StartsWith("--"))
{
if (arg.Length > 2)
{
lastKeyStored = arg.Substring(2);
appParameters[lastKeyStored] = string.Empty;
}
else
lastKeyStored = string.Empty;
}
else if (!deepLinkFound && arg.StartsWith("decentraland://"))
{
deepLinkFound = true;
lastKeyStored = string.Empty;
// Application parameters may come embedded in a deep link:
// Example (Windows) -> start decentraland://"realm=http://127.0.0.1:8000&position=100,100&local-scene=true&otherparam=blahblah"
Dictionary<string, string> deepLinkParameters = ProcessDeepLinkParameters(arg);
foreach ((string key, string value) in deepLinkParameters)
appParameters[key] = value;
}
else if (!string.IsNullOrEmpty(lastKeyStored))
appParameters[lastKeyStored] = arg;
}
}
public static Dictionary<string, string> ProcessDeepLinkParameters(string deepLinkString)
{
var output = new Dictionary<string, string>();
// Drop the optional host segment (e.g. "open" in decentraland://open?signin=... or decentraland://open/?signin=...) so only the query remains;
deepLinkString = Regex.Replace(deepLinkString, @"^(decentraland:/+)[A-Za-z][A-Za-z0-9_-]*/*\?", "$1?");
// Update deep link so that Uri class allows the host name
deepLinkString = Regex.Replace(deepLinkString, @"^decentraland:/+", "https://decentraland.org/?");
if (!Uri.TryCreate(deepLinkString, UriKind.Absolute, out Uri? _)) return output;
var uri = new Uri(deepLinkString);
NameValueCollection uriQuery = HttpUtility.ParseQueryString(uri.Query);
var droppedKeys = new List<string>();
// Tier 1: always-permitted (base) navigation/login params.
foreach (string uriQueryKey in uriQuery.AllKeys)
{
// if the deep link is not constructed correctly (AKA 'decentraland://?&blabla=blabla') a 'null' parameter can be detected...
if (uriQueryKey == null) continue;
if (DeepLinkAllowlist.IsPermitted(uriQueryKey))
output[uriQueryKey] = uriQuery.Get(uriQueryKey);
}
if (output.TryGetValue(AppArgsFlags.REALM, out string? realmParamValue))
{
// Patch for WinOS sometimes affecting the 'realm' parameter in deep links putting a '/' at the end
if (realmParamValue.EndsWith('/'))
realmParamValue = realmParamValue.Remove(realmParamValue.Length - 1);
// Patch for MacOS removing the ':' from the realm parameter protocol
realmParamValue = Regex.Replace(realmParamValue, @"(https?)//(.*?)$", @"$1://$2");
output[AppArgsFlags.REALM] = realmParamValue;
}
// Tier 2 (SEC-019/020): the local-development params Creator Hub / sdk-commands attach to preview deep
// links (DeepLinkAllowlist.LOOPBACK_REALM_PERMITTED_KEYS, with per-key rationale) are permitted only
// when the target realm is loopback — a remote-realm deep link from a web page cannot enable them.
// Everything not in either tier is dropped.
bool realmIsLoopback = output.TryGetValue(AppArgsFlags.REALM, out string? loopbackRealm)
&& Uri.TryCreate(loopbackRealm, UriKind.Absolute, out Uri? loopbackRealmUri)
&& loopbackRealmUri.IsLoopback;
foreach (string uriQueryKey in uriQuery.AllKeys)
{
if (uriQueryKey == null || output.ContainsKey(uriQueryKey)) continue;
if (realmIsLoopback && DeepLinkAllowlist.IsPermittedForLoopbackRealm(uriQueryKey))
output[uriQueryKey] = uriQuery.Get(uriQueryKey);
else
droppedKeys.Add(uriQueryKey);
}
if (droppedKeys.Count > 0)
ReportHub.LogWarning(ReportCategory.ALWAYS, $"Dropped {droppedKeys.Count} non-allowlisted deep-link param(s): {string.Join(", ", droppedKeys)}");
return output;
}
private void LogArguments()
{
const int COUNT_PER_LINE = 7;
var sb = new StringBuilder(COUNT_PER_LINE * appParameters.Count);
var count = 1;
sb.AppendLine("==================");
sb.AppendLine("Application arguments:");
sb.AppendLine("==================\n");
foreach ((string? key, string? value) in appParameters)
{
sb.Append("Arg ").Append(count).Append(": ").Append(key).Append(" = ").Append(value).Append("\n");
count++;
}
sb.AppendLine("==================\n");
ReportHub.LogProductionInfo(sb.ToString());
}
}
}