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

🎨 Use filter_var to check IPv4 and IPv6 addresses #10

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

rudnerbjoern
Copy link
Contributor

@rudnerbjoern rudnerbjoern commented Dec 5, 2024

Base information

Question Answer
Related to a SourceForge thread / Another PR / Combodo ticket? No
Type of change? Enhancement

Symptom (bug) / Objective (enhancement)

  • Use filter_var($variable, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) and filter_var($variable, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) when checking for IP addresses.
  • Remove redundant checks for empty strings.

Proposed solution (bug and enhancement)

Make validation of IP addresses more robust due to the use of PHP functions

Checklist before requesting a review

  • I have performed a self-review of my code, and that it's compliant with Combodo's guidelines
  • I have tested all changes I made on an iTop instance
  • I have added a unit test, otherwise I have explained why I couldn't
  • I have made sure the PR is clear and detailled enough so anyone can understand the real purpose without digging in the code

Checklist of things to do before PR is ready to merge

none

Copy link
Contributor

@Hipska Hipska left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to change this to better to maintain methods, just some minor remarks.

src/vSphereIPv4AddressCollector.class.inc.php Outdated Show resolved Hide resolved
src/vSphereIPv4AddressCollector.class.inc.php Outdated Show resolved Hide resolved
src/vSphereIPv4AddressCollector.class.inc.php Outdated Show resolved Hide resolved
src/vSphereIPv6AddressCollector.class.inc.php Outdated Show resolved Hide resolved
src/vSphereVirtualMachineCollector.class.inc.php Outdated Show resolved Hide resolved
rudnerbjoern and others added 5 commits December 6, 2024 10:48
Co-authored-by: Thomas Casteleyn <thomas.casteleyn@me.com>
Co-authored-by: Thomas Casteleyn <thomas.casteleyn@me.com>
Co-authored-by: Thomas Casteleyn <thomas.casteleyn@me.com>
@rudnerbjoern
Copy link
Contributor Author

I've updated the code to the suggestions of @Hipska

'short_name' => $sShortName,
'status' => $this->oCollectionPlan->GetTeemIpOption('default_ip_status'),
);
if (filter_var($sIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also rewrite the following:

				$sIP = $oVM['managementip'] ?? '';
				if (filter_var($sIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {

To something like:

				$sIP = filter_var($oVM['managementip'] ?? '', FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
				if ($sIP !== false) {

Or just if ($sIP) {, but I think Combodo would prefer the one above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we discussed that. But to me it "hurts" to see a bool written into a variable pretending to be a string. (I know now that this is valid PHP, but it just feels not right for me to do this)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reopen conversation to let Combodo decide on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry

rudnerbjoern and others added 2 commits December 6, 2024 12:14
Yes, I've overseen this.

Co-authored-by: Thomas Casteleyn <thomas.casteleyn@me.com>
Co-authored-by: Thomas Casteleyn <thomas.casteleyn@me.com>
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 this pull request may close these issues.

2 participants