Repository navigation
Potential fix for code scanning alert no. 2: Incorrect conversion between integer types - #1
Merged
Merged
Conversation
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Potential fix for https://github.com/edjeffreys/nomad-tailscale-controller/security/code-scanning/2
In general, to fix this class of issue you must ensure that any integer value parsed from a string (especially with
strconv.Atoireturning anint) is either (a) parsed directly into the target-sized type usingstrconv.ParseInt/ParseUintwith an appropriate bit size, or (b) range-checked before converting to a smaller-width type. For ports, the valid range is0–65535, and typically1–65535(disallowing zero) for externally visible services.For this specific case, the best fix with minimal behavioral change is to add an explicit upper-bound (and optionally lower-bound) check before converting
svc.Porttouint16inbuildServeConfig. We already default zero ports to443, so it's reasonable to only accept non-negative ports up tomath.MaxUint16(which is65535) and treat anything else as invalid, falling back to443. This prevents wraparound when converting touint16without changing the public API or the structure ofService.Concretely:
internal/tailscale/client.go, insidebuildServeConfig.cfg := &ServeConfig{...}), define a localconst defaultPort = uint16(443)to keep the default consistent and typed correctly, or just useuint16(443)inline.port := uint16(svc.Port)and itsif port == 0 { port = 443 }with logic that:port := uint16(443)(the default).svc.Portis > 0 andsvc.Port <= math.MaxUint16, setport = uint16(svc.Port).mathat the top ofinternal/tailscale/client.goto accessmath.MaxUint16.This ensures that any out-of-range or zero port results in a safe default (443), and any valid port (1–65535) is preserved, while eliminating the unsafe narrowing conversion.
Suggested fixes powered by Copilot Autofix. Review carefully before merging.