SecurityElement.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.s
  4. using System.Collections;
  5. using System.Diagnostics;
  6. using System.Text;
  7. namespace System.Security
  8. {
  9. public sealed class SecurityElement
  10. {
  11. internal string _tag = null!;
  12. internal string? _text;
  13. private ArrayList? _children;
  14. internal ArrayList? _attributes;
  15. private const int AttributesTypical = 4 * 2; // 4 attributes, times 2 strings per attribute
  16. private const int ChildrenTypical = 1;
  17. private static readonly char[] s_tagIllegalCharacters = new char[] { ' ', '<', '>' };
  18. private static readonly char[] s_textIllegalCharacters = new char[] { '<', '>' };
  19. private static readonly char[] s_valueIllegalCharacters = new char[] { '<', '>', '\"' };
  20. private static readonly char[] s_escapeChars = new char[] { '<', '>', '\"', '\'', '&' };
  21. private static readonly string[] s_escapeStringPairs = new string[]
  22. {
  23. // these must be all once character escape sequences or a new escaping algorithm is needed
  24. "<", "&lt;",
  25. ">", "&gt;",
  26. "\"", "&quot;",
  27. "\'", "&apos;",
  28. "&", "&amp;"
  29. };
  30. //-------------------------- Constructors ---------------------------
  31. internal SecurityElement()
  32. {
  33. }
  34. public SecurityElement(string tag)
  35. {
  36. if (tag == null)
  37. throw new ArgumentNullException(nameof(tag));
  38. if (!IsValidTag(tag))
  39. throw new ArgumentException(SR.Format(SR.Argument_InvalidElementTag, tag));
  40. _tag = tag;
  41. _text = null;
  42. }
  43. public SecurityElement(string tag, string? text)
  44. {
  45. if (tag == null)
  46. throw new ArgumentNullException(nameof(tag));
  47. if (!IsValidTag(tag))
  48. throw new ArgumentException(SR.Format(SR.Argument_InvalidElementTag, tag));
  49. if (text != null && !IsValidText(text))
  50. throw new ArgumentException(SR.Format(SR.Argument_InvalidElementText, text));
  51. _tag = tag;
  52. _text = text;
  53. }
  54. //-------------------------- Properties -----------------------------
  55. public string Tag
  56. {
  57. get => _tag;
  58. set
  59. {
  60. if (value == null)
  61. throw new ArgumentNullException(nameof(Tag));
  62. if (!IsValidTag(value))
  63. throw new ArgumentException(SR.Format(SR.Argument_InvalidElementTag, value));
  64. _tag = value;
  65. }
  66. }
  67. public Hashtable? Attributes
  68. {
  69. get
  70. {
  71. if (_attributes == null || _attributes.Count == 0)
  72. {
  73. return null;
  74. }
  75. else
  76. {
  77. Hashtable hashtable = new Hashtable(_attributes.Count / 2);
  78. int iMax = _attributes.Count;
  79. Debug.Assert(iMax % 2 == 0, "Odd number of strings means the attr/value pairs were not added correctly");
  80. for (int i = 0; i < iMax; i += 2)
  81. {
  82. hashtable.Add(_attributes[i]!, _attributes[i + 1]);
  83. }
  84. return hashtable;
  85. }
  86. }
  87. set
  88. {
  89. if (value == null || value.Count == 0)
  90. {
  91. _attributes = null;
  92. }
  93. else
  94. {
  95. ArrayList list = new ArrayList(value.Count);
  96. IDictionaryEnumerator enumerator = value.GetEnumerator();
  97. while (enumerator.MoveNext())
  98. {
  99. string attrName = (string)enumerator.Key;
  100. string? attrValue = (string?)enumerator.Value;
  101. if (!IsValidAttributeName(attrName))
  102. throw new ArgumentException(SR.Format(SR.Argument_InvalidElementName, attrName));
  103. if (!IsValidAttributeValue(attrValue))
  104. throw new ArgumentException(SR.Format(SR.Argument_InvalidElementValue, attrValue));
  105. list.Add(attrName);
  106. list.Add(attrValue);
  107. }
  108. _attributes = list;
  109. }
  110. }
  111. }
  112. public string? Text
  113. {
  114. get => Unescape(_text);
  115. set
  116. {
  117. if (value == null)
  118. {
  119. _text = null;
  120. }
  121. else
  122. {
  123. if (!IsValidText(value))
  124. throw new ArgumentException(SR.Format(SR.Argument_InvalidElementTag, value));
  125. _text = value;
  126. }
  127. }
  128. }
  129. public ArrayList? Children
  130. {
  131. get
  132. {
  133. return _children;
  134. }
  135. set
  136. {
  137. if (value != null && value.Contains(null))
  138. {
  139. throw new ArgumentException(SR.ArgumentNull_Child);
  140. }
  141. _children = value;
  142. }
  143. }
  144. //-------------------------- Public Methods -----------------------------
  145. internal void AddAttributeSafe(string name, string value)
  146. {
  147. if (_attributes == null)
  148. {
  149. _attributes = new ArrayList(AttributesTypical);
  150. }
  151. else
  152. {
  153. int iMax = _attributes.Count;
  154. Debug.Assert(iMax % 2 == 0, "Odd number of strings means the attr/value pairs were not added correctly");
  155. for (int i = 0; i < iMax; i += 2)
  156. {
  157. string? strAttrName = (string?)_attributes[i];
  158. if (string.Equals(strAttrName, name))
  159. throw new ArgumentException(SR.Argument_AttributeNamesMustBeUnique);
  160. }
  161. }
  162. _attributes.Add(name);
  163. _attributes.Add(value);
  164. }
  165. public void AddAttribute(string name, string value)
  166. {
  167. if (name == null)
  168. throw new ArgumentNullException(nameof(name));
  169. if (value == null)
  170. throw new ArgumentNullException(nameof(value));
  171. if (!IsValidAttributeName(name))
  172. throw new ArgumentException(SR.Format(SR.Argument_InvalidElementName, name));
  173. if (!IsValidAttributeValue(value))
  174. throw new ArgumentException(SR.Format(SR.Argument_InvalidElementValue, value));
  175. AddAttributeSafe(name, value);
  176. }
  177. public void AddChild(SecurityElement child)
  178. {
  179. if (child == null)
  180. throw new ArgumentNullException(nameof(child));
  181. _children ??= new ArrayList(ChildrenTypical);
  182. _children.Add(child);
  183. }
  184. public bool Equal(SecurityElement? other)
  185. {
  186. if (other == null)
  187. return false;
  188. // Check if the tags are the same
  189. if (!string.Equals(_tag, other._tag))
  190. return false;
  191. // Check if the text is the same
  192. if (!string.Equals(_text, other._text))
  193. return false;
  194. // Check if the attributes are the same and appear in the same
  195. // order.
  196. if (_attributes == null || other._attributes == null)
  197. {
  198. if (_attributes != other._attributes)
  199. return false;
  200. }
  201. else
  202. {
  203. int iMax = _attributes.Count;
  204. Debug.Assert(iMax % 2 == 0, "Odd number of strings means the attr/value pairs were not added correctly");
  205. // Maybe we can get away by only checking the number of attributes
  206. if (iMax != other._attributes.Count)
  207. return false;
  208. for (int i = 0; i < iMax; i++)
  209. {
  210. string? lhs = (string?)_attributes[i];
  211. string? rhs = (string?)other._attributes[i];
  212. if (!string.Equals(lhs, rhs))
  213. return false;
  214. }
  215. }
  216. // Finally we must check the child and make sure they are
  217. // equal and in the same order
  218. if (_children == null || other._children == null)
  219. {
  220. if (_children != other._children)
  221. return false;
  222. }
  223. else
  224. {
  225. // Maybe we can get away by only checking the number of children
  226. if (_children.Count != other._children.Count)
  227. return false;
  228. IEnumerator lhs = _children.GetEnumerator();
  229. IEnumerator rhs = other._children.GetEnumerator();
  230. SecurityElement? e1, e2;
  231. while (lhs.MoveNext())
  232. {
  233. rhs.MoveNext();
  234. e1 = (SecurityElement?)lhs.Current;
  235. e2 = (SecurityElement?)rhs.Current;
  236. if (e1 == null || !e1.Equal(e2))
  237. return false;
  238. }
  239. }
  240. return true;
  241. }
  242. public SecurityElement Copy()
  243. {
  244. SecurityElement element = new SecurityElement(_tag, _text);
  245. element._children = _children == null ? null : new ArrayList(_children);
  246. element._attributes = _attributes == null ? null : new ArrayList(_attributes);
  247. return element;
  248. }
  249. public static bool IsValidTag(string? tag)
  250. {
  251. if (tag == null)
  252. return false;
  253. return tag.IndexOfAny(s_tagIllegalCharacters) == -1;
  254. }
  255. public static bool IsValidText(string? text)
  256. {
  257. if (text == null)
  258. return false;
  259. return text.IndexOfAny(s_textIllegalCharacters) == -1;
  260. }
  261. public static bool IsValidAttributeName(string? name)
  262. {
  263. return IsValidTag(name);
  264. }
  265. public static bool IsValidAttributeValue(string? value)
  266. {
  267. if (value == null)
  268. return false;
  269. return value.IndexOfAny(s_valueIllegalCharacters) == -1;
  270. }
  271. private static string GetEscapeSequence(char c)
  272. {
  273. int iMax = s_escapeStringPairs.Length;
  274. Debug.Assert(iMax % 2 == 0, "Odd number of strings means the attr/value pairs were not added correctly");
  275. for (int i = 0; i < iMax; i += 2)
  276. {
  277. string strEscSeq = s_escapeStringPairs[i];
  278. string strEscValue = s_escapeStringPairs[i + 1];
  279. if (strEscSeq[0] == c)
  280. return strEscValue;
  281. }
  282. Debug.Fail("Unable to find escape sequence for this character");
  283. return c.ToString();
  284. }
  285. public static string? Escape(string? str)
  286. {
  287. if (str == null)
  288. return null;
  289. StringBuilder? sb = null;
  290. int strLen = str.Length;
  291. int index; // Pointer into the string that indicates the location of the current '&' character
  292. int newIndex = 0; // Pointer into the string that indicates the start index of the "remaining" string (that still needs to be processed).
  293. while (true)
  294. {
  295. index = str.IndexOfAny(s_escapeChars, newIndex);
  296. if (index == -1)
  297. {
  298. if (sb == null)
  299. return str;
  300. else
  301. {
  302. sb.Append(str, newIndex, strLen - newIndex);
  303. return sb.ToString();
  304. }
  305. }
  306. else
  307. {
  308. sb ??= new StringBuilder();
  309. sb.Append(str, newIndex, index - newIndex);
  310. sb.Append(GetEscapeSequence(str[index]));
  311. newIndex = (index + 1);
  312. }
  313. }
  314. // no normal exit is possible
  315. }
  316. private static string GetUnescapeSequence(string str, int index, out int newIndex)
  317. {
  318. int maxCompareLength = str.Length - index;
  319. int iMax = s_escapeStringPairs.Length;
  320. Debug.Assert(iMax % 2 == 0, "Odd number of strings means the attr/value pairs were not added correctly");
  321. for (int i = 0; i < iMax; i += 2)
  322. {
  323. string strEscSeq = s_escapeStringPairs[i];
  324. string strEscValue = s_escapeStringPairs[i + 1];
  325. int length = strEscValue.Length;
  326. if (length <= maxCompareLength && string.Compare(strEscValue, 0, str, index, length, StringComparison.Ordinal) == 0)
  327. {
  328. newIndex = index + strEscValue.Length;
  329. return strEscSeq;
  330. }
  331. }
  332. newIndex = index + 1;
  333. return str[index].ToString();
  334. }
  335. private static string? Unescape(string? str)
  336. {
  337. if (str == null)
  338. return null;
  339. StringBuilder? sb = null;
  340. int strLen = str.Length;
  341. int index; // Pointer into the string that indicates the location of the current '&' character
  342. int newIndex = 0; // Pointer into the string that indicates the start index of the "remainging" string (that still needs to be processed).
  343. while (true)
  344. {
  345. index = str.IndexOf('&', newIndex);
  346. if (index == -1)
  347. {
  348. if (sb == null)
  349. return str;
  350. else
  351. {
  352. sb.Append(str, newIndex, strLen - newIndex);
  353. return sb.ToString();
  354. }
  355. }
  356. else
  357. {
  358. sb ??= new StringBuilder();
  359. sb.Append(str, newIndex, index - newIndex);
  360. sb.Append(GetUnescapeSequence(str, index, out newIndex)); // updates the newIndex too
  361. }
  362. }
  363. }
  364. public override string ToString()
  365. {
  366. StringBuilder sb = new StringBuilder();
  367. ToString(sb, (obj, str) => ((StringBuilder)obj).Append(str));
  368. return sb.ToString();
  369. }
  370. private void ToString(object obj, Action<object, string?> write)
  371. {
  372. write(obj, "<");
  373. write(obj, _tag);
  374. // If there are any attributes, plop those in.
  375. if (_attributes != null && _attributes.Count > 0)
  376. {
  377. write(obj, " ");
  378. int iMax = _attributes.Count;
  379. Debug.Assert(iMax % 2 == 0, "Odd number of strings means the attr/value pairs were not added correctly");
  380. for (int i = 0; i < iMax; i += 2)
  381. {
  382. string? strAttrName = (string?)_attributes[i];
  383. string? strAttrValue = (string?)_attributes[i + 1];
  384. write(obj, strAttrName);
  385. write(obj, "=\"");
  386. write(obj, strAttrValue);
  387. write(obj, "\"");
  388. if (i != _attributes.Count - 2)
  389. {
  390. write(obj, Environment.NewLineConst);
  391. }
  392. }
  393. }
  394. if (_text == null && (_children == null || _children.Count == 0))
  395. {
  396. // If we are a single tag with no children, just add the end of tag text.
  397. write(obj, "/>");
  398. write(obj, Environment.NewLineConst);
  399. }
  400. else
  401. {
  402. // Close the current tag.
  403. write(obj, ">");
  404. // Output the text
  405. write(obj, _text);
  406. // Output any children.
  407. if (_children != null)
  408. {
  409. write(obj, Environment.NewLineConst);
  410. for (int i = 0; i < _children.Count; ++i)
  411. {
  412. ((SecurityElement)_children[i]!).ToString(obj, write);
  413. }
  414. }
  415. // Output the closing tag
  416. write(obj, "</");
  417. write(obj, _tag);
  418. write(obj, ">");
  419. write(obj, Environment.NewLineConst);
  420. }
  421. }
  422. public string? Attribute(string name)
  423. {
  424. if (name == null)
  425. throw new ArgumentNullException(nameof(name));
  426. // Note: we don't check for validity here because an
  427. // if an invalid name is passed we simply won't find it.
  428. if (_attributes == null)
  429. return null;
  430. // Go through all the attribute and see if we know about
  431. // the one we are asked for
  432. int iMax = _attributes.Count;
  433. Debug.Assert(iMax % 2 == 0, "Odd number of strings means the attr/value pairs were not added correctly");
  434. for (int i = 0; i < iMax; i += 2)
  435. {
  436. string? strAttrName = (string?)_attributes[i];
  437. if (string.Equals(strAttrName, name))
  438. {
  439. string? strAttrValue = (string?)_attributes[i + 1];
  440. return Unescape(strAttrValue);
  441. }
  442. }
  443. // In the case where we didn't find it, we are expected to
  444. // return null
  445. return null;
  446. }
  447. public SecurityElement? SearchForChildByTag(string tag)
  448. {
  449. // Go through all the children and see if we can
  450. // find the ones that are asked for (matching tags)
  451. if (tag == null)
  452. throw new ArgumentNullException(nameof(tag));
  453. // Note: we don't check for a valid tag here because
  454. // an invalid tag simply won't be found.
  455. if (_children == null)
  456. return null;
  457. foreach (SecurityElement? current in _children)
  458. {
  459. if (current != null && string.Equals(current.Tag, tag))
  460. return current;
  461. }
  462. return null;
  463. }
  464. public string? SearchForTextOfTag(string tag)
  465. {
  466. // Search on each child in order and each
  467. // child's child, depth-first
  468. if (tag == null)
  469. throw new ArgumentNullException(nameof(tag));
  470. // Note: we don't check for a valid tag here because
  471. // an invalid tag simply won't be found.
  472. if (string.Equals(_tag, tag))
  473. return Unescape(_text);
  474. if (_children == null)
  475. return null;
  476. foreach (SecurityElement? child in Children!)
  477. {
  478. string? text = child?.SearchForTextOfTag(tag);
  479. if (text != null)
  480. return text;
  481. }
  482. return null;
  483. }
  484. public static SecurityElement? FromString(string xml)
  485. {
  486. if (xml == null)
  487. throw new ArgumentNullException(nameof(xml));
  488. return default;
  489. }
  490. }
  491. }