Bug report: Magic Function Holding
- Dominant language
- JavaScript
- Stars
- 35.8k
- Forks
- 4.1k
- Avg merge
- 2d 26m
- Merged PRs (30d)
- 33
Description
**Describe the bug**
When running the Magic function on certain inputs, the function will hold indefinitely and use up compute resources, then at times errors out. This occurs in both the node package, and on the Github hosted browser version as well on Chrome.
**To Reproduce**
1. Run the node `chef.magic` function with this input:
```
chef.magic('103.77.192[.]219\n104.140.114[.]110\n104.250.191[.]110\n108.61.246[.]56\n149.28.14[.]163\n157.230.221[.]198\n167.99.168[.]251\n185.250.151[.]72\n192.81.208[.]169\n203.160.69[.]66\n211.56.98[.]146\n5')
```
**Expected behaviour**
The function gives an output or errors out rather than holding.
**Desktop:**
- OS: Mac OS Catalina
- Browser: Node Package and Chrome
- CyberChef version: 9.28.0
**Additional context**
I've found the source of the problem to be one of the regex test functions found in [cyberchef/src/core/lib/Magic.mjs ln:45](https://github.com/gchq/CyberChef/blob/master/src/core/lib/Magic.mjs#L49), specifically in this case the `From Hexdump` operation pattern check `/^(?:(?:[\dA-F]{4,16}h?:?)?[ \t]*((?:[\dA-F]{2} ){1,8}(?:[ \t]|[\dA-F]{2}-)(?:[\dA-F]{2} ){1,8}|(?:[\dA-F]{4} )*[\dA-F]{4}|(?:[\dA-F]{2} )*[\dA-F]{2})[^\n]*\n?){2,}$/i`.
If you are to run this regex directly on this input you get the same issue as this seems to be the source of the issue.
```javascript
/^(?:(?:[\dA-F]{4,16}h?:?)?[ \t]*((?:[\dA-F]{2} ){1,8}(?:[ \t]|[\dA-F]{2}-)(?:[\dA-F]{2} ){1,8}|(?:[\dA-F]{4} )*[\dA-F]{4}|(?:[\dA-F]{2} )*[\dA-F]{2})[^\n]*\n?){2,}$/i.test('103.77.192[.]219\n104.140.114[.]110\n104.250.191[.]110\n108.61.246[.]56\n149.28.14[.]163\n157.230.221[.]198\n167.99.168[.]251\n185.250.151[.]72\n192.81.208[.]169\n203.160.69[.]66\n211.56.98[.]146\n5')
```
A potential solution could be changing this regex to be more efficient.
Contributor guide
Research direction
Start in src/core/lib/Magic.mjs around the From Hexdump pattern check referenced at line 45/49, then run the supplied JavaScript regular-expression test with the reproducing input. Compare behavior in the node package and hosted browser version. Done means the Magic function no longer holds indefinitely and instead produces output or errors promptly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- performance, web-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100