Repository navigation
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens the knownhost add APIs by rejecting keylen == 0 instead of falling back to computing the key length (previously via strlen() in some cases), aligning behavior with safer length-explicit handling higher up the call chain.
Changes:
- Reject zero
keylenin the internalknownhost_add()helper by returningLIBSSH2_ERROR_BAD_USE. - Remove the implicit “determine key length” fallback behavior when
keylenis not provided.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…ownhost_add*()` To apply the same fix made earlier to `ssh2_base64_encode()`, but this one level up in the call chain. This is stricly speaking an incompatible change, but because the automatic-strlen behavior was never documented for these functions, it required a valid `keylen` value be passed, and because the fallback feature is fragile, potentially unsafe, I propose this. Replace by returning `LIBSSH2_ERROR_BAD_USE` if zero `keylen` is passed. Refs: https://github.com/libssh2/libssh2/pull/2363/files#diff-5182b571dc174e7b59106f0ea398a54784a0eb6c32eb95b9952cfa1aca893a71R866 curl/curl@5e2d4d7 curl/curl#18617 Follow-up to 102e444 libssh2#2363 Follow-up to d1c0e14 libssh2#1641
82982a3 to
87f4b77
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
src/knownhost.c:137
- This change makes
keylen == 0an unconditional error, but the public API documentation still says it may be omitted when the key is a NUL-terminated base64 string (e.g.,include/libssh2.haroundlibssh2_knownhost_add*()and the comment blocks abovelibssh2_knownhost_add*()in this file). The docs should be updated in the same PR to avoid a documented/implemented mismatch (and because this is an intentional breaking change, it should also be called out in user-facing docs/release notes).
if(!hosts || !host || !key || !keylen)
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (2)
src/knownhost.c:143
- This change intentionally makes
keylen==0an error, but the public API contract ininclude/libssh2.hstill documents thatkeylen“may be omitted (zero)” for null-terminated base64 keys (seeinclude/libssh2.h:1097-1099and:1159-1160). Please update the public header docs (and any other API docs/release notes as appropriate) in the same PR to avoid a documented/implemented mismatch forlibssh2_knownhost_add*().
/* Set an error message because this was a breaking change. */
if(!keylen)
return ssh2_err(hosts->session, LIBSSH2_ERROR_BAD_USE,
"Known-host key length required");
src/knownhost.c:868
- Removing the explicit
keylen==0check here means a malformed known_hosts line with a long comment but an empty key can now fail later withLIBSSH2_ERROR_BAD_USE/ "Known-host key length required" (fromknownhost_add()), which is a misleading error for a parse failure. Add a dedicated parse error when the parsed key span is empty.
/* Figure out host format */
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
src/knownhost.c:144
- This change makes
keylen == 0an error, but the public API is still documented as allowingkeylento be omitted (0) for NUL-terminated base64 keys (e.g.,include/libssh2.hand the comment blocks abovelibssh2_knownhost_add*()in this file). That creates a documented/implemented mismatch, and it also contradicts the PR description’s claim that the docs “always required a validkeylen”. Please update the public header (and any in-tree API docs/release notes as appropriate) to reflect the new requiredkeylencontract, or adjust the code/PR description if the behavior should remain supported.
/* keylen == 0 previously fell back to strlen(key); require explicit
length. */
if(!keylen)
return ssh2_err(hosts->session, LIBSSH2_ERROR_BAD_USE,
"Known-host key length required");
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/knownhost.c:144
- This change intentionally makes
keylen == 0a hard error, but the public API contract is still documented as allowing omitted/zerokeylenfor NUL-terminated base64 keys (see the comment blocks abovelibssh2_knownhost_add()/libssh2_knownhost_addc()in this file and ininclude/libssh2.h). As-is, the implementation and documented contract diverge, which will be confusing for callers and downstream packagers.
To keep the API behavior change safe and explicit, please update the public docs in the same PR (at minimum: include/libssh2.h and the header comments in this file) to remove the “keylen may be omitted (zero)” language and document the new LIBSSH2_ERROR_BAD_USE behavior.
/* keylen == 0 previously fell back to strlen(key) for base64-encoded keys.
Require explicit length now. */
if(!keylen)
return ssh2_err(hosts->session, LIBSSH2_ERROR_BAD_USE,
"Known-host key length required");
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (2)
src/knownhost.c:142
- The new historical note is a bit misleading: previously
keylen == 0only fell back tostrlen(key)when the key was base64-encoded (i.e., whenLIBSSH2_KNOWNHOST_KEYENC_BASE64was set). Tightening the wording avoids future confusion about what behavior actually changed.
/* keylen == 0 fell back to strlen(key) until libssh2 1.11.1.
Require explicit length now. */
if(!keylen)
src/knownhost.c:144
- Now that
keylen == 0is rejected, the public API documentation is out of sync: bothinclude/libssh2.hand the API doc blocks in this file still say thatkeylenmay be omitted (zero) for NUL-terminated base64 keys (e.g.,include/libssh2.haround thelibssh2_knownhost_add*()docs, andsrc/knownhost.caround thelibssh2_knownhost_addc()docs). Those comments should be updated in the same PR to avoid a documented/implemented mismatch for API consumers.
if(!keylen)
return ssh2_err(hosts->session, LIBSSH2_ERROR_BAD_USE,
"Known-host key length required");
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
src/knownhost.c:141
- The new comment says
keylen == 0fell back tostrlen(key)unconditionally, but the removed code shows that fallback only applied whenLIBSSH2_KNOWNHOST_KEYENC_BASE64was set. Tightening the wording avoids misleading future maintenance about the actual historical behavior.
/* keylen == 0 fell back to strlen(key) until libssh2 1.11.1.
Require explicit length now. */
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/knownhost.c:141
- The new comment implies
keylen == 0always fell back tostrlen(key)historically, but the removed codepath shows the fallback only applied whentypemaskincludedLIBSSH2_KNOWNHOST_KEYENC_BASE64. Tightening the comment to match the prior behavior will avoid misleading future maintenance.
/* keylen == 0 fell back to strlen(key) until libssh2 1.11.1.
Require explicit length now. */
To complement/replace similar fixes made earlier to
ssh2_base64_encode()andknownhost_line(), respectively.Until libssh2 1.11.1,
libssh2_knownhost_add*()public APIs had aspecial behavior when they received
keylen == 0argument. Theyautomatically determined the length of the
keybuffer viastrlen().When passing zero length either on purpose or by accident, it could
result in an OOB read if the buffer's actual length was zero. (Such case
could also happen within libssh2's own code.)
This behavior was not documented on the man page, but the public
libssh2.hheader mentioned it in comments.Earlier patch #1641 dropped this behavior for one codepath, which was a
breaking change, arguably for the better. This patch fixes the remaining
codepath and removes this behavior from these functions completely. They
now return
LIBSSH2_ERROR_BAD_USEwhen called with a zerokeylenargument, with an explanatory error string.
Recommended fix is passing
keybuffer length, orstrlen()it at thecall site if the buffer is null-terminated, and making sure the buffer
is non-empty.
Refs:
https://github.com/libssh2/libssh2/pull/2363/files#diff-5182b571dc174e7b59106f0ea398a54784a0eb6c32eb95b9952cfa1aca893a71R866
curl/curl@5e2d4d7
curl/curl#18617
Follow-up to 102e444 #2363
Follow-up to d1c0e14 #1641
Follow-up to 4b991b2
libssh2.h. Reconsider?https://codesearch.debian.net/search?q=libssh2_knownhost_add&literal=1
https://github.com/search?q=libssh2_knownhost_add+language%3AC&type=code