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

content.create: Random id generation is unsafe #445

Open
achimwilde opened this issue Nov 5, 2020 · 2 comments
Open

content.create: Random id generation is unsafe #445

achimwilde opened this issue Nov 5, 2020 · 2 comments

Comments

@achimwilde
Copy link

achimwilde commented Nov 5, 2020

/src/plone/api/content.py, lines 81/82:

# Create a temporary id if the id is not given
content_id = not safe_id and id or str(random.randint(0, 99999999))

We have a website with thousands of articles which are imported as Plone objects every couple of weeks and have numeric names which we pass as title parameter to api.content.create. Sometimes the random temporary id will conflict with an id of a previously generated object, resulting in a BadRequest error.

So instead of blindly using the random temporary id a test should be done if this id can be used safely, by checking if an object with this id already exists in the container. How about this:

<   content_id = not safe_id and id or str(random.randint(0, 99999999))
>   while (True):
>        content_id = not safe_id and id or str(random.randint(0, 99999999))
>        if content_id not in container.keys():
>            break

This solution definately isn't perfect, but if you have items with most or all possible ids from 0 to 99999999 in one folder, you will have plenty of other issues anyway...

@jensens
Copy link
Member

jensens commented Dec 29, 2020

I would generate an uuid4 instead. Its longer, but safe.

Personal I use https://pypi.org/project/shortuuid/ for such cases, but that is another dependency we do not want to have in core.

@mauritsvanrees
Copy link
Member

In Archetypes we used to try 100 times.
I guess code or inspiration could be borrowed from there. I assumed a similar thing happened in plone.api already.

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

No branches or pull requests

3 participants