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

pyasn1/coded/ber: Fix regressions dealing with component == None #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

noelpower
Copy link

since commit 4f644c5

#!/usr/bin/env python3
import sys, os.path, io, string
import base64

from pyasn1.codec.der.decoder import decode as pyasn1_der_decode

def main ():
blob = "fk8wTaADAgEFoQMCAR6kERgPMjAyMzA2MDYxMDAxMTlapQU"
"CAwIstqYDAgEsqRUbEzx1bnNwZWNpZmllZCByZWFsbT6qCzAJoAMCAQChAjAA"
obj, _ = pyasn1_der_decode(base64.b64decode(blob))
if name == 'main':
main()

results in

File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 2003, in call
for asn1Object in streamingDecoder:
File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 1918, in iter
for asn1Object in self._singleItemDecoder(
File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 1778, in call
for value in concreteDecoder.valueDecoder(
File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 99, in valueDecoder
for value in decodeFun(substrate, asn1Spec, tagSet, length, **options):
File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 1778, in call
for value in concreteDecoder.valueDecoder(
File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 660, in valueDecoder
for asn1Object in self._decodeComponentsSchemaless(
File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 596, in _decodeComponentsSchemaless
for component in decodeFun(substrate, **options):
File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 1778, in call
for value in concreteDecoder.valueDecoder(
File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 99, in valueDecoder
for value in decodeFun(substrate, asn1Spec, tagSet, length, **options):
File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 1778, in call
for value in concreteDecoder.valueDecoder(
File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 660, in valueDecoder
for asn1Object in self._decodeComponentsSchemaless(
File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 604, in _decodeComponentsSchemaless
componentTypes.add(component.tagSet)
AttributeError: 'NoneType' object has no attribute 'tagSet'

This change restores the previous code that will avoid this error.

also see

snippet mentioned in test code above should decode
see https://lapo.it/asn1js/#fk8wTaADAgEFoQMCAR6kERgPMjAyMzA2MDYxMDAxMTlapQUCAwIstqYDAgEsqRUbEzx1bnNwZWNpZmllZCByZWFsbT6qCzAJoAMCAQChAjAA

@noelpower
Copy link
Author

failing check doesn't seem to be related to change :/ not sure what the procedure is here (and don't see a way to restart the test)

@noelpower noelpower changed the title pyasn1/coded/ber: Fix regressions dealing component == None pyasn1/coded/ber: Fix regressions dealing with component == None Jun 6, 2023
@Avasam
Copy link

Avasam commented Sep 29, 2023

@noelpower Closing and re-opening a PR will cause GitHub to retrigger the checks with updated workflow.

@bor8
Copy link

bor8 commented Apr 5, 2024

Short:

Bump/LGTM

Long:

This PR addresses a potential "AttributeError: 'NoneType' object has no attribute 'tagSet'" during schema-less ASN.1 object decoding by refining the _decodeComponentsSchemaless method. It ensures asn1Object is properly initialized before accessing its tagSet, preventing errors when a component is None. Additionally, it improves type guessing for ASN.1 containers based on component diversity, enhancing overall decoding reliability.

Tested across various scenarios, these changes significantly bolster decoding accuracy without introducing new issues. I recommend this merge to improve our project's robustness and decoding functionality.

Thanks for considering this fix.

since commit 4f644c5

  #!/usr/bin/env python3
  import sys, os.path, io, string
  import base64

  from pyasn1.codec.der.decoder import decode as pyasn1_der_decode

  def main ():
      blob = "fk8wTaADAgEFoQMCAR6kERgPMjAyMzA2MDYxMDAxMTlapQU"\
              "CAwIstqYDAgEsqRUbEzx1bnNwZWNpZmllZCByZWFsbT6qCzAJoAMCAQChAjAA"
      obj, _ = pyasn1_der_decode(base64.b64decode(blob))
  if __name__ == '__main__':
      main()

results in

  File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 2003, in __call__
    for asn1Object in streamingDecoder:
  File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 1918, in __iter__
    for asn1Object in self._singleItemDecoder(
  File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 1778, in __call__
    for value in concreteDecoder.valueDecoder(
  File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 99, in valueDecoder
    for value in decodeFun(substrate, asn1Spec, tagSet, length, **options):
  File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 1778, in __call__
    for value in concreteDecoder.valueDecoder(
  File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 660, in valueDecoder
    for asn1Object in self._decodeComponentsSchemaless(
  File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 596, in _decodeComponentsSchemaless
    for component in decodeFun(substrate, **options):
  File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 1778, in __call__
    for value in concreteDecoder.valueDecoder(
  File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 99, in valueDecoder
    for value in decodeFun(substrate, asn1Spec, tagSet, length, **options):
  File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 1778, in __call__
    for value in concreteDecoder.valueDecoder(
  File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 660, in valueDecoder
    for asn1Object in self._decodeComponentsSchemaless(
  File "/home/tux/pyasn1/pyasn1/codec/ber/decoder.py", line 604, in _decodeComponentsSchemaless
    componentTypes.add(component.tagSet)
AttributeError: 'NoneType' object has no attribute 'tagSet'

This change restores the previous code that will avoid this error.

Signed-off-by: Noel Power <noel.power@suse.com>
@noelpower
Copy link
Author

@noelpower Closing and re-opening a PR will cause GitHub to retrigger the checks with updated workflow.

gah had forgotten about this, I think I must have missed this (and only saw it with notification of a recent comment)

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

3 participants