Skip to content

Differentiate checksum exceptions #731

Closed

Description

@Evidlo

I have a complicated Struct with multiple Checksums throughout and no way to differentiate between them if they fail. It would be nice to be able to tell these apart programmatically, either by adding an exception kwarg to Checksum or by adding the path string as an attribute to ChecksumError.

A more general solution would be to include the path for all exceptions, which would also be useful for quickly zeroing in problems while debugging.

(also, why is path a string and not something more easily parsed, like a list?)

Activity

  1. arekbulski commented on Jun 8, 2018

    @arekbulski
    Member

    Why its a string not a list? Well, there wasnt much decision making put into that. Anyway, I am not sure exactly what is it that you are asking. If you would want to provide a PR, I would look at it.

  2. Evidlo commented on Jun 8, 2018

    @Evidlo
    Author

    PR at #732

    Example usage:

    class HeaderHMACError(Exception):
        pass
    class HeaderSHA256Error(Exception):
        pass
    
    KDBX4 = Struct(
        "header" / RawCopy(
            Struct(
                "magic1" / Bytes(4),
                "magic2" / Bytes(4),
                "minor_version" / Int16ul,
                "major_version" / Int16ul,
                "dynamic_header" / DynamicHeader
            )
        ),
        "transformed_key" / Computed(compute_transformed),
        "master_key" / Computed(compute_master),
        "sha256" / Checksum(
            Bytes(32),
            lambda data: hashlib.sha256(data).digest(),
            this.header.data,
            exception=HeaderSHA256Error,
        ),
        "hmac" / Checksum(
            Bytes(32),
            compute_header_hmac_hash,
            this,
            exception=HeaderHMACError,
        ),
        "payload" / UnpackedPayload(
            IfThenElse(
                this.header.value.dynamic_header.compression_flags.data.compression,
                Decompressed(DecryptedPayload),
                DecryptedPayload
            )
        )
    )
    
    password = b'sshatpass'
    keyfile = 'test4.key'
    
    try:
        result = KDBX4.parse_stream(s, password=password, keyfile=keyfile)
    except HeaderSHA256Error:
        log.error("Database header validation failed.")
        sys.exit()
    except HeaderHMACError:
        log.error("Password or keyfile incorrect")
        sys.exit()
  3. Evidlo commented on Aug 8, 2018

    @Evidlo
    Author

    Did you look at my proposal in #732 ?

  4. arekbulski commented on Aug 9, 2018

    @arekbulski
    Member

    I am not going to merge it, truth be told, I simply dont like it. If you want to use it, you can copy (git clone) the Construct into a local folder, and there add your own commits as you like.

  5. Evidlo commented on Aug 9, 2018

    @Evidlo
    Author

    Ouch.

    This would've taken care of two deficiencies in construct:

    1. No ability to distinguish checksum errors and provide helpful information to the end user.

    2. Exceptions in complicated/large projects (especially stream errors) are not useful because you can't tell where they occur. I'm forced to insert a Probe in different places and rerun the code multiple times to determine where the error happens.

    Aren't these issues worth tackling?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions