Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] Remove spaces from nsmap #1022

Merged
merged 1 commit into from Mar 27, 2024
Merged

Conversation

subho007
Copy link
Member

Current behaviour:

If manifest contains a space in xmlns attributes, APK can be installed without issue, but pyaxmlparser fails.

<manifest 
	xmlns:android="http://schemas.android.com/apk/res/android" 
	xmlns:tools="http://schemas.android.com/tools "
ValueError: Invalid namespace URI 'http://schemas.android.com/tools '

Analysis

>>> etree.Element("manifest", nsmap={"tools": "http://schemas.android.com/tools"})
<Element manifest at 0x1226f1600>

>>> etree.Element("manifest", nsmap={"tools": "http://schemas.android.com/tools "})
etree.Element("manifest", nsmap={"tools": "http://schemas.android.com/tools "})
File src/lxml/etree.pyx:3022, in lxml.etree.Element()
File src/lxml/apihelpers.pxi:131, in lxml.etree._makeElement()
File src/lxml/apihelpers.pxi:118, in lxml.etree._makeElement()
File src/lxml/apihelpers.pxi:215, in lxml.etree._setNodeNamespaces()
File src/lxml/apihelpers.pxi:1755, in lxml.etree._uriValidOrRaise()
ValueError: Invalid namespace URI 'http://schemas.android.com/tools '

There is a recover option in lxml to make the parser less strict:

from io import StringIO

attrs = [f"xmlns:{k}=\"{v}\"" for k,v in nsmap.items()]
manifest_str = f"<manifest {' '.join(attrs)} />"
tree = etree.parse(StringIO(manifest_str), magical_parser)
elem = tree.getroot()

>>> elem
<Element manifest at 0x11c04ca00>

This implementation is not clean and can't be used. There is another way to create Element directly, but it is not taking the parser passed:

magical_parser = etree.XMLParser(encoding='utf-8', recover=True)
elem = magical_parser.makeelement("manifest", nsmap=nsmap)

etree.Element("manifest", nsmap={"tools": "http://schemas.android.com/tools "})
File src/lxml/etree.pyx:3022, in lxml.etree.Element()
File src/lxml/apihelpers.pxi:131, in lxml.etree._makeElement()
File src/lxml/apihelpers.pxi:118, in lxml.etree._makeElement()
File src/lxml/apihelpers.pxi:215, in lxml.etree._setNodeNamespaces()
File src/lxml/apihelpers.pxi:1755, in lxml.etree._uriValidOrRaise()
ValueError: Invalid namespace URI 'http://schemas.android.com/tools '

So changing the way we create Element is currently not possible, better approach would be to send the cleaned nsmap itself.


Changelog

  • Remove spaces from nsmap values
  • Add tests for AXMLParser.nsmap for this bug scenario

See more details in: appknox/pyaxmlparser#64

@subho007
Copy link
Member Author

@erev0s please have a look at this PR, also waiting for a new release 👍

@erev0s erev0s merged commit 30fdb63 into androguard:master Mar 27, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants