1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.content.res; 18 19 import android.annotation.NonNull; 20 import android.annotation.StyleableRes; 21 22 import com.android.internal.R; 23 24 import org.xmlpull.v1.XmlPullParser; 25 import org.xmlpull.v1.XmlPullParserException; 26 27 import java.util.ArrayDeque; 28 29 /** 30 * Validates manifest files by ensuring that tag counts and the length of string attributes are 31 * restricted. 32 * 33 * {@hide} 34 */ 35 public class Validator { 36 37 private final ArrayDeque<Element> mElements = new ArrayDeque<>(); 38 cleanUp()39 private void cleanUp() { 40 while (!mElements.isEmpty()) { 41 mElements.pop().recycle(); 42 } 43 } 44 45 /** 46 * Validates the elements and it's attributes as the XmlPullParser traverses the xml. 47 */ validate(@onNull XmlPullParser parser)48 public void validate(@NonNull XmlPullParser parser) throws XmlPullParserException { 49 int eventType = parser.getEventType(); 50 int depth = parser.getDepth(); 51 // The mElement size should equal to the parser depth-1 when the parser eventType is 52 // START_TAG. If depth - mElement.size() is larger than 1 then that means 53 // validation for the previous element was skipped so we should skip validation for all 54 // descendant elements as well 55 if (depth > mElements.size() + 1) { 56 return; 57 } 58 if (eventType == XmlPullParser.START_TAG) { 59 String tag = parser.getName(); 60 if (Element.shouldValidate(tag)) { 61 Element element = Element.obtain(tag); 62 Element parent = mElements.peek(); 63 if (parent != null && parent.hasChild(tag)) { 64 try { 65 parent.seen(element); 66 } catch (SecurityException e) { 67 cleanUp(); 68 throw e; 69 } 70 } 71 mElements.push(element); 72 } 73 } else if (eventType == XmlPullParser.END_TAG && depth == mElements.size()) { 74 mElements.pop().recycle(); 75 } else if (eventType == XmlPullParser.END_DOCUMENT) { 76 cleanUp(); 77 } 78 } 79 80 /** 81 * Validates the resource string of a manifest tag attribute. 82 */ validateResStrAttr(@onNull XmlPullParser parser, @StyleableRes int index, CharSequence stringValue)83 public void validateResStrAttr(@NonNull XmlPullParser parser, @StyleableRes int index, 84 CharSequence stringValue) { 85 if (parser.getDepth() > mElements.size()) { 86 return; 87 } 88 mElements.peek().validateResStrAttr(index, stringValue); 89 if (index == R.styleable.AndroidManifestMetaData_value) { 90 validateComponentMetadata(stringValue.toString()); 91 } 92 } 93 94 /** 95 * Validates the string of a manifest tag attribute by name. 96 */ validateStrAttr(@onNull XmlPullParser parser, String attrName, String attrValue)97 public void validateStrAttr(@NonNull XmlPullParser parser, String attrName, String attrValue) { 98 if (parser.getDepth() > mElements.size()) { 99 return; 100 } 101 mElements.peek().validateStrAttr(attrName, attrValue); 102 if (attrName.equals(Element.TAG_ATTR_VALUE)) { 103 validateComponentMetadata(attrValue); 104 } 105 } 106 validateComponentMetadata(String attrValue)107 private void validateComponentMetadata(String attrValue) { 108 Element element = mElements.peek(); 109 // Meta-data values are evaluated on the parent element which is the next element in the 110 // mElements stack after the meta-data element. The top of the stack is always the current 111 // element being validated so check that the top element is meta-data. 112 if (element.mTag.equals(Element.TAG_META_DATA) && mElements.size() > 1) { 113 element = mElements.pop(); 114 mElements.peek().validateComponentMetadata(attrValue); 115 mElements.push(element); 116 } 117 } 118 } 119