From af4a5065673ef95afb33f0f60b6c9bc575d288c6 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Fri, 4 Jan 2019 17:45:36 -0500 Subject: [PATCH 1/9] fix: add aapt1/aapt2 xsd file --- .../aapt1/testapp/res/values/public.xml | 1 + .../testapp/res/xml/ww_box_styles_schema.xsd | 20 +++++++++++++++++++ .../testapp/res/xml/ww_box_styles_schema.xsd | 20 +++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 brut.apktool/apktool-lib/src/test/resources/aapt1/testapp/res/xml/ww_box_styles_schema.xsd create mode 100644 brut.apktool/apktool-lib/src/test/resources/aapt2/testapp/res/xml/ww_box_styles_schema.xsd diff --git a/brut.apktool/apktool-lib/src/test/resources/aapt1/testapp/res/values/public.xml b/brut.apktool/apktool-lib/src/test/resources/aapt1/testapp/res/values/public.xml index 4a0a3a39..86c77ede 100644 --- a/brut.apktool/apktool-lib/src/test/resources/aapt1/testapp/res/values/public.xml +++ b/brut.apktool/apktool-lib/src/test/resources/aapt1/testapp/res/values/public.xml @@ -1,4 +1,5 @@ + \ No newline at end of file diff --git a/brut.apktool/apktool-lib/src/test/resources/aapt1/testapp/res/xml/ww_box_styles_schema.xsd b/brut.apktool/apktool-lib/src/test/resources/aapt1/testapp/res/xml/ww_box_styles_schema.xsd new file mode 100644 index 00000000..1b4badf8 --- /dev/null +++ b/brut.apktool/apktool-lib/src/test/resources/aapt1/testapp/res/xml/ww_box_styles_schema.xsd @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/brut.apktool/apktool-lib/src/test/resources/aapt2/testapp/res/xml/ww_box_styles_schema.xsd b/brut.apktool/apktool-lib/src/test/resources/aapt2/testapp/res/xml/ww_box_styles_schema.xsd new file mode 100644 index 00000000..1b4badf8 --- /dev/null +++ b/brut.apktool/apktool-lib/src/test/resources/aapt2/testapp/res/xml/ww_box_styles_schema.xsd @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From a5cfe41a8f5d59e50d4f47a8140929557cb9ebe7 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Fri, 4 Jan 2019 17:45:51 -0500 Subject: [PATCH 2/9] test: add test for aapt1/aapt2 - ignore aapt2 due to failure --- .../test/java/brut/androlib/aapt1/BuildAndDecodeTest.java | 5 +++++ .../test/java/brut/androlib/aapt2/BuildAndDecodeTest.java | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt1/BuildAndDecodeTest.java b/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt1/BuildAndDecodeTest.java index e2992018..ee9db151 100644 --- a/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt1/BuildAndDecodeTest.java +++ b/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt1/BuildAndDecodeTest.java @@ -198,6 +198,11 @@ public class BuildAndDecodeTest extends BaseTest { compareXmlFiles("res/xml/references.xml"); } + @Test + public void xmlXsdFileTest() throws BrutException { + compareXmlFiles("res/xml/ww_box_styles_schema.xsd"); + } + @Test public void xmlIdsEmptyTest() throws BrutException { compareXmlFiles("res/values/ids.xml"); diff --git a/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java b/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java index bd4785de..8ab8dd78 100644 --- a/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java +++ b/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java @@ -23,6 +23,7 @@ import brut.directory.ExtFile; import brut.util.OS; import org.junit.AfterClass; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; import java.io.File; @@ -98,4 +99,10 @@ public class BuildAndDecodeTest extends BaseTest { public void confirmManifestStructureTest() throws BrutException { compareXmlFiles("AndroidManifest.xml"); } + + @Test + @Ignore + public void xmlXsdFileTest() throws BrutException { + compareXmlFiles("res/xml/ww_box_styles_schema.xsd"); + } } From 7cd894684954399e6d9bb398c5356584de63d654 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Fri, 4 Jan 2019 18:00:19 -0500 Subject: [PATCH 3/9] fix: fallback to raw decoder if xml decoder failed - prevents injection of false value --- .../java/brut/androlib/res/decoder/ResFileDecoder.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResFileDecoder.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResFileDecoder.java index 33a5fbc0..ba73dbcd 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResFileDecoder.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResFileDecoder.java @@ -119,6 +119,14 @@ public class ResFileDecoder { decode(inDir, inFileName, outDir, outFileName, "xml"); } catch (AndrolibException ex) { + // If we got an error to decode XML, lets assume the file is in raw format. + // This is a large assumption, that might increase runtime, but will save us for situations where + // XSD files are AXML`d on aapt1, but left in plaintext in aapt2. + if (ex.getMessage().equalsIgnoreCase("Could not decode XML")) { + decode(inDir, inFileName, outDir, outFileName, "raw"); + return; + } + LOGGER.log(Level.SEVERE, String.format( "Could not decode file, replacing by FALSE value: %s", inFileName), ex); From 39faff0a211508ee61fff9ad46903b938ce76df5 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Fri, 4 Jan 2019 18:00:33 -0500 Subject: [PATCH 4/9] test: allow aapt2 xsd test to run --- .../src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java b/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java index 8ab8dd78..86d9a4f9 100644 --- a/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java +++ b/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java @@ -101,7 +101,6 @@ public class BuildAndDecodeTest extends BaseTest { } @Test - @Ignore public void xmlXsdFileTest() throws BrutException { compareXmlFiles("res/xml/ww_box_styles_schema.xsd"); } From c833a32a6e986103f668861cd2c81014323cbcb1 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Fri, 4 Jan 2019 18:05:51 -0500 Subject: [PATCH 5/9] style: syntax changes for review --- .../src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java | 1 - .../resources/aapt1/testapp/res/xml/ww_box_styles_schema.xsd | 2 +- .../resources/aapt2/testapp/res/xml/ww_box_styles_schema.xsd | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java b/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java index 86d9a4f9..f031f4da 100644 --- a/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java +++ b/brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/BuildAndDecodeTest.java @@ -23,7 +23,6 @@ import brut.directory.ExtFile; import brut.util.OS; import org.junit.AfterClass; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import java.io.File; diff --git a/brut.apktool/apktool-lib/src/test/resources/aapt1/testapp/res/xml/ww_box_styles_schema.xsd b/brut.apktool/apktool-lib/src/test/resources/aapt1/testapp/res/xml/ww_box_styles_schema.xsd index 1b4badf8..70bfc6c3 100644 --- a/brut.apktool/apktool-lib/src/test/resources/aapt1/testapp/res/xml/ww_box_styles_schema.xsd +++ b/brut.apktool/apktool-lib/src/test/resources/aapt1/testapp/res/xml/ww_box_styles_schema.xsd @@ -1,4 +1,4 @@ - + diff --git a/brut.apktool/apktool-lib/src/test/resources/aapt2/testapp/res/xml/ww_box_styles_schema.xsd b/brut.apktool/apktool-lib/src/test/resources/aapt2/testapp/res/xml/ww_box_styles_schema.xsd index 1b4badf8..70bfc6c3 100644 --- a/brut.apktool/apktool-lib/src/test/resources/aapt2/testapp/res/xml/ww_box_styles_schema.xsd +++ b/brut.apktool/apktool-lib/src/test/resources/aapt2/testapp/res/xml/ww_box_styles_schema.xsd @@ -1,4 +1,4 @@ - + From ac3518e098cdaa042d13bb2c5550704cce207c6e Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Wed, 9 Jan 2019 17:10:21 -0500 Subject: [PATCH 6/9] feat: add new exception types --- .../androlib/err/AXmlDecodingException.java | 37 +++++++++++++++++++ .../err/RawXmlEncounteredException.java | 37 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 brut.apktool/apktool-lib/src/main/java/brut/androlib/err/AXmlDecodingException.java create mode 100644 brut.apktool/apktool-lib/src/main/java/brut/androlib/err/RawXmlEncounteredException.java diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/err/AXmlDecodingException.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/err/AXmlDecodingException.java new file mode 100644 index 00000000..cc28d000 --- /dev/null +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/err/AXmlDecodingException.java @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2018 Ryszard Wiśniewski + * Copyright (C) 2018 Connor Tumbleson + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package brut.androlib.err; + +import brut.androlib.AndrolibException; + +public class AXmlDecodingException extends AndrolibException { + + public AXmlDecodingException(Throwable cause) { + super(cause); + } + + public AXmlDecodingException(String message, Throwable cause) { + super(message, cause); + } + + public AXmlDecodingException(String message) { + super(message); + } + + public AXmlDecodingException() { + } +} \ No newline at end of file diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/err/RawXmlEncounteredException.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/err/RawXmlEncounteredException.java new file mode 100644 index 00000000..762f1074 --- /dev/null +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/err/RawXmlEncounteredException.java @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2018 Ryszard Wiśniewski + * Copyright (C) 2018 Connor Tumbleson + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package brut.androlib.err; + +import brut.androlib.AndrolibException; + +public class RawXmlEncounteredException extends AndrolibException { + + public RawXmlEncounteredException(Throwable cause) { + super(cause); + } + + public RawXmlEncounteredException(String message, Throwable cause) { + super(message, cause); + } + + public RawXmlEncounteredException(String message) { + super(message); + } + + public RawXmlEncounteredException() { + } +} From 6bb73b509d1cc758e1e9af1d4693f92ba08b55e9 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Wed, 9 Jan 2019 17:10:56 -0500 Subject: [PATCH 7/9] fix: throw specific errors during XMLPullParser error --- .../brut/androlib/res/decoder/XmlPullStreamDecoder.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/XmlPullStreamDecoder.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/XmlPullStreamDecoder.java index 888e47c4..c8411d03 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/XmlPullStreamDecoder.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/XmlPullStreamDecoder.java @@ -21,6 +21,8 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.logging.Logger; +import brut.androlib.err.AXmlDecodingException; +import brut.androlib.err.RawXmlEncounteredException; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.wrapper.XmlPullParserWrapper; @@ -142,9 +144,9 @@ public class XmlPullStreamDecoder implements ResStreamDecoder { } ser.flush(); } catch (XmlPullParserException ex) { - throw new AndrolibException("Could not decode XML", ex); + throw new AXmlDecodingException("Could not decode XML", ex); } catch (IOException ex) { - throw new AndrolibException("Could not decode XML", ex); + throw new RawXmlEncounteredException("Could not decode XML", ex); } } From c1f50a2810592faade98555ca5804224a03d4345 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Wed, 9 Jan 2019 17:11:32 -0500 Subject: [PATCH 8/9] style: remove unused LOGGER for PullStreamDecoder --- .../java/brut/androlib/res/decoder/XmlPullStreamDecoder.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/XmlPullStreamDecoder.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/XmlPullStreamDecoder.java index c8411d03..25ef81e6 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/XmlPullStreamDecoder.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/XmlPullStreamDecoder.java @@ -19,7 +19,6 @@ package brut.androlib.res.decoder; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.logging.Logger; import brut.androlib.err.AXmlDecodingException; import brut.androlib.err.RawXmlEncounteredException; @@ -157,6 +156,4 @@ public class XmlPullStreamDecoder implements ResStreamDecoder { private final XmlPullParser mParser; private final ExtXmlSerializer mSerial; - - private final static Logger LOGGER = Logger.getLogger(XmlPullStreamDecoder.class.getName()); } From 45152c269a6c0fe97f5ccf8bee8ace292ab32140 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Wed, 9 Jan 2019 17:11:51 -0500 Subject: [PATCH 9/9] refactor: use specific exceptions when decoding raw xml --- .../java/brut/androlib/res/decoder/ResFileDecoder.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResFileDecoder.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResFileDecoder.java index ba73dbcd..cc98c0b5 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResFileDecoder.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResFileDecoder.java @@ -18,6 +18,7 @@ package brut.androlib.res.decoder; import brut.androlib.AndrolibException; import brut.androlib.err.CantFind9PatchChunk; +import brut.androlib.err.RawXmlEncounteredException; import brut.androlib.res.data.ResResource; import brut.androlib.res.data.value.ResBoolValue; import brut.androlib.res.data.value.ResFileValue; @@ -118,15 +119,12 @@ public class ResFileDecoder { } decode(inDir, inFileName, outDir, outFileName, "xml"); - } catch (AndrolibException ex) { + } catch (RawXmlEncounteredException ex) { // If we got an error to decode XML, lets assume the file is in raw format. // This is a large assumption, that might increase runtime, but will save us for situations where // XSD files are AXML`d on aapt1, but left in plaintext in aapt2. - if (ex.getMessage().equalsIgnoreCase("Could not decode XML")) { - decode(inDir, inFileName, outDir, outFileName, "raw"); - return; - } - + decode(inDir, inFileName, outDir, outFileName, "raw"); + } catch (AndrolibException ex) { LOGGER.log(Level.SEVERE, String.format( "Could not decode file, replacing by FALSE value: %s", inFileName), ex);