Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merging to release-5.7: [TT-11711] Fix listenpath validation (#6772)
[TT-11711] Fix listenpath validation (#6772) ### **User description** <details open> <summary><a href="https://tyktech.atlassian.net/browse/TT-11711" title="TT-11711" target="_blank">TT-11711</a></summary> <br /> <table> <tr> <th>Summary</th> <td>`listen path` formatting can panic worker gateway</td> </tr> <tr> <th>Type</th> <td> <img alt="Bug" src="https://tyktech.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10303?size=medium" /> Bug </td> </tr> <tr> <th>Status</th> <td>In Dev</td> </tr> <tr> <th>Points</th> <td>N/A</td> </tr> <tr> <th>Labels</th> <td><a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20'24Bugsmash%20ORDER%20BY%20created%20DESC" title="'24Bugsmash">'24Bugsmash</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20Gold%20ORDER%20BY%20created%20DESC" title="Gold">Gold</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20customer_bug%20ORDER%20BY%20created%20DESC" title="customer_bug">customer_bug</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20jira_escalated%20ORDER%20BY%20created%20DESC" title="jira_escalated">jira_escalated</a></td> </tr> </table> </details> <!-- do not remove this marker as it will break jira-lint's functionality. added_by_jira_lint --> --- PR adds listenpath validation using the mux library. ___ ### **PR Type** Bug fix, Tests ___ ### **Description** - Added `listenPath` validation using `httputil.ValidatePath` to prevent invalid paths from causing panics. - Enhanced logging in `MakeSpec` and `loadHTTPService` to include more context for debugging. - Updated `loadHTTPService` to validate `listenPath` and return errors when validation fails. - Introduced `ValidatePath` function in `httputil` to centralize path validation logic. - Added comprehensive unit tests for `ValidatePath` to ensure correctness and robustness. - Added integration tests in `api_loader_test.go` to verify `listenPath` validation during API loading. ___ ### **Changes walkthrough** 📝 <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Bug fix</strong></td><td><table> <tr> <td> <details> <summary><strong>api_definition.go</strong><dd><code>Add <code>listenPath</code> validation and improve logging in API definition <br>loader.</code></dd></summary> <hr> gateway/api_definition.go <li>Added validation for <code>listenPath</code> using <code>httputil.ValidatePath</code> to prevent <br>invalid paths.<br> <li> Enhanced logging with additional context fields for better debugging.<br> <li> Ensured <code>MakeSpec</code> and <code>loadHTTPService</code> validate <code>listenPath</code> to avoid <br>panics.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6772/files#diff-0cf80174bbafb36f6d4f4308ebbd971b2833b76a936bad568220aa1a4ba0ee8b">+18/-10</a> </td> </tr> <tr> <td> <details> <summary><strong>api_loader.go</strong><dd><code>Validate `listenPath` in `loadHTTPService` and handle errors.</code></dd></summary> <hr> gateway/api_loader.go <li>Added <code>httputil.ValidatePath</code> validation in <code>loadHTTPService</code> to ensure <br>valid <code>listenPath</code>.<br> <li> Modified <code>loadHTTPService</code> to return an error when validation fails.<br> <li> Updated API loading logic to handle errors from <code>loadHTTPService</code>.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6772/files#diff-cdf0b7f176c9d18e1a314b78ddefc2cb3a94b3de66f1f360174692c915734c68">+21/-5</a> </td> </tr> </table></td></tr><tr><td><strong>Tests</strong></td><td><table> <tr> <td> <details> <summary><strong>api_loader_test.go</strong><dd><code>Add test for `listenPath` validation in API loader.</code> </dd></summary> <hr> gateway/api_loader_test.go <li>Added a new test case <code>TestAPILoaderValidation</code> to ensure <code>listenPath</code> <br>validation works correctly.<br> <li> Verified that invalid <code>listenPath</code> values do not cause panics.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6772/files#diff-f696545a659f4d96421b253edef4bcc8da0e7f52120b8f8866d32cbbb7cc1afc">+39/-2</a> </td> </tr> <tr> <td> <details> <summary><strong>mux_test.go</strong><dd><code>Add unit tests for `ValidatePath` function.</code> </dd></summary> <hr> internal/httputil/mux_test.go <li>Added unit tests for <code>ValidatePath</code> to verify its behavior with valid <br>and invalid paths.<br> <li> Covered edge cases such as invalid regex and missing leading slashes.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6772/files#diff-8f7ce1891e221d7adb9e68f2e951f33edfbde2128187abb6e837ac01952d7888">+24/-0</a> </td> </tr> </table></td></tr><tr><td><strong>Enhancement</strong></td><td><table> <tr> <td> <details> <summary><strong>mux.go</strong><dd><code>Add `ValidatePath` function for `listenPath` validation.</code> </dd></summary> <hr> internal/httputil/mux.go <li>Introduced <code>ValidatePath</code> function to validate <code>listenPath</code> using mux <br>router.<br> <li> Ensures invalid paths are caught early to prevent runtime issues.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6772/files#diff-3d9ee5f5e946d72e6f2ae662ff03ee5253bbdc15203d2e4f6e9f46c13011ebf8">+7/-0</a> </td> </tr> </table></td></tr></tr></tbody></table> ___ > 💡 **PR-Agent usage**: Comment `/help "your question"` on any pull request to receive relevant information --------- Co-authored-by: Tit Petric <[email protected]>
- Loading branch information