question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RuntimeError: dictionary keys changed during iteration (with --json, and Python 3.8)

See original GitHub issue

Running with -j or --json with Python 3.8.x cause the following error

± ./ssh-audit.py -j raspberrypi2.local
Traceback (most recent call last):
  File "./ssh-audit.py", line 3350, in <module>
    main()
  File "./ssh-audit.py", line 3347, in main
    audit(conf)
  File "./ssh-audit.py", line 3337, in audit
    print(json.dumps(build_struct(banner, kex=kex, client_host=s.client_host), sort_keys=True))
  File "./ssh-audit.py", line 3245, in build_struct
    for host_key_type in host_keys.keys():
RuntimeError: dictionary keys changed during iteration

The code in question is

https://github.com/jtesta/ssh-audit/blob/e447c42a79df49841d5269eacde6dbeb811a6be4/ssh-audit.py#L3245-L3249

The fix for the immediate is straightforward, replace host_keys.keys() with list(host_keys.keys()) so the loop iterates over a copy.

However, I’m concerned about the rest of the loop. I think its

  • picking one rsa host key amongst (possibly) several, and discarding any others
  • the key it keeps is not predictable, because iteration over dict keys is non-deterministic in Python 3.5 and earlier.

I need to confirm whether an ssh server can have multiple RSA keys (perhaps with different key lengths).

Other details

± python3 --version
Python 3.8.2
± git show --summary | cat
commit e447c42a79df49841d5269eacde6dbeb811a6be4
Author: Joe Testa <jtesta@positronsecurity.com>
Date:   Wed Mar 11 11:55:14 2020 -0400

    Bumped version to v2.2.0.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
moreaticommented, Jul 18, 2020

Do you have any thoughts on the other possible bug? That if an SSH server offered multiple RSA keys, ssh-audit would pick one at random.

0reactions
moreaticommented, Aug 14, 2020

Does this answer your question?

Well enough. I’d more-or-less concluded that multiple RSA keys might be possible according to the protocol, but rarely (if ever) seen in the wild

And are you still running into any bugs in the dev branch?

No, thank you. Close this or leave it open however you prefer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

dictionary changed size during iteration" error? - Stack Overflow
The Python "RuntimeError: dictionary changed size during iteration" occurs when we change the size of a dictionary when iterating over it. · To...
Read more >
RuntimeError: dictionary changed size during iteration
The Python "RuntimeError: dictionary changed size during iteration" occurs when we change the size of a dictionary when iterating over it.
Read more >
Filtering Dictionary In Python 3. “RuntimeError - codeburst
“RuntimeError: Dictionary changed size during iteration”, Python slaps you in your face when you try to add/remove entries in dict object during iteration....
Read more >
“RuntimeError: dictionary keys changed during iter...
I am attempting to just add a CSV file to my issues as a test, but I keep receiving the error: ``` RuntimeError:...
Read more >
Fix Error - Dictionary Changed Size During Iteration | Delft Stack
This Runtime error occurs when we remove, modify, or add new entries in a dictionary object during iteration. This error occurs when iterating...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found