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

CustomExtensions for File with TimestampPropertys can't handle datetime objects #395

Closed
maybe-sybr opened this issue May 18, 2020 · 0 comments · Fixed by #402
Closed

CustomExtensions for File with TimestampPropertys can't handle datetime objects #395

maybe-sybr opened this issue May 18, 2020 · 0 comments · Fixed by #402

Comments

@maybe-sybr
Copy link
Contributor

STIX 2.1 File SCOs require that their extensions be considered when generating deterministic UUIDs, serialised following JSON canonicalisation rules. Defining a CustomExtension for Files that include a timestamp property results in exceptions being raised when that property is provided as a datetime object rather than a Zulu formatted datestamp string. The following snippet demonstrates the issue:

import datetime
import pprint

import stix2.v21
import stix2.properties

@stix2.v21.CustomExtension(stix2.v21.File, "x-xtime-ext", (
    ("xtime", stix2.properties.TimestampProperty(), ),
))
class XTimeExtension():
    pass

try:
    f = stix2.v21.File(
        name="foo",
        extensions={"x-xtime-ext": {"xtime": datetime.datetime.utcnow()}, },
    )
except Exception as exc:
    print(exc)
else:
    raise Exception("That should have failed...")

f = stix2.v21.File(
    name="foo",
    extensions={"x-xtime-ext": {"xtime": str(datetime.datetime.utcnow())}, },
)
s = f.serialize()
pprint.pprint(s)
stix2.parse(s)

f = stix2.parse("""
{"type": "file",
 "spec_version": "2.1",
 "name": "foo",
 "extensions": {
   "x-xtime-ext": {"xtime": "1970-01-01T00:00:00Z"}
 }
}
""")
print(f)

Presumably the fix would involve teaching _Observable._generate_id() to use a STIXJSONEncoder rather than just calling stix2.canonicalization.Canonicalize.canonicalization() which uses the default JSONEncoder implementation.

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 a pull request may close this issue.

1 participant