Skip to content

Fix empty token on swift package-registry login (Issue #7453) #7454

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

Merged
merged 1 commit into from
Apr 15, 2024

Conversation

pwallrich
Copy link
Contributor

Fix empty token when adding a new package-registry and adding token interactively on release builds

Motivation:

When executing swift package-registry login it asks for a token but fails to retrieve it properly. It sends an empty string to the registry server and login therefore fails. (see #7453)

Modifications:

I've debugged the code a bit and found out, that somehow buffer and passwordPtr don't seem to be holding correct values after the readpassphrase call.

  • An easy quick fix is to make sure that buffer doesn't get deallocated by adding smth like _ = buffer after the String init. This works but is not nice.
  • My first try was to use buffer instead of passwordPtr to create the string. These works, but I remember reading somewhere that & can be quite nasty sometimes.
  • I also tried buffer.withUnsafeMutablePointer. However readpassphrase seems to not only change the content of buffer but move it. This leads to a runtime failure Fatal error: Array withUnsafeMutableBufferPointer: replacing the buffer is not allowed

Result:

The buffer is retained, the token is properly parsed and sent to the server.

the allocated buffer is not valid anymore after reading in the access token from the command line.
`withUnsafeMutableBuffer` unfortunately doesn't work since readpassphrase internally seems to move the buffer which leads to a runtime exception.
@MaxDesiatov
Copy link
Contributor

Hi @pwallrich, thank you for the contribution? Would you be able to add a test for this to our test suite to prevent possible regressions in the future?

@MaxDesiatov MaxDesiatov added bug needs tests This change needs test coverage registry SwiftPM Registry-related changes labels Apr 12, 2024
@pwallrich
Copy link
Contributor Author

Hey @MaxDesiatov. Could be quite hard, since it only happens in release builds. But I'll try to find a way to trigger it reliably in tests.

Do you know if there already exist tests for things like that so I wouldn't have to reinvent the wheel?

@pwallrich
Copy link
Contributor Author

Ok I've managed to write a test for it, but it's actually a bit nasty since I have to change the stdin.
Did that based on https://forums.swift.org/t/how-to-read-from-standard-input-during-unit-tests/43237/.

I also had to create a separate testTarget since the test needs to be built with optimisation and therefore can't use @testable. I don't know, how I would add to the CI.

The more I think about this bug the more I question whether reverting the change introducing readpassphrase would make sense. So use getpass again and add an additional error for users who use tokens >128 characters telling them to either use --token or --token-file. But I guess that would need a bigger discussion.

Do you have any thoughts about that?

@MaxDesiatov MaxDesiatov requested a review from yim-lee April 12, 2024 23:06
@MaxDesiatov
Copy link
Contributor

@swift-ci test

@MaxDesiatov MaxDesiatov merged commit d8c8d0f into swiftlang:main Apr 15, 2024
5 checks passed
furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
swiftlang#7454)

Fix empty token when adding a new package-registry and adding token
interactively on release builds

### Motivation:

When executing swift package-registry login it asks for a token but
fails to retrieve it properly. It sends an empty string to the registry
server and login therefore fails. (see
swiftlang#7453)

### Modifications:

I've debugged the code a bit and found out, that somehow `buffer` and
`passwordPtr` don't seem to be holding correct values after the
`readpassphrase` call.

- An easy quick fix is to make sure that `buffer` doesn't get
deallocated by adding smth like `_ = buffer` after the String init. This
works but is not nice.
- My first try was to use `buffer` instead of `passwordPtr` to create
the string. These works, but I remember reading somewhere that `&` can
be quite nasty sometimes.
- I also tried `buffer.withUnsafeMutablePointer`. However
`readpassphrase` seems to not only change the content of `buffer` but
move it. This leads to a runtime failure `Fatal error: Array
withUnsafeMutableBufferPointer: replacing the buffer is not allowed`

### Result:
The buffer is retained, the token is properly parsed and sent to the
server.
furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
swiftlang#7454)

Fix empty token when adding a new package-registry and adding token
interactively on release builds

### Motivation:

When executing swift package-registry login it asks for a token but
fails to retrieve it properly. It sends an empty string to the registry
server and login therefore fails. (see
swiftlang#7453)

### Modifications:

I've debugged the code a bit and found out, that somehow `buffer` and
`passwordPtr` don't seem to be holding correct values after the
`readpassphrase` call.

- An easy quick fix is to make sure that `buffer` doesn't get
deallocated by adding smth like `_ = buffer` after the String init. This
works but is not nice.
- My first try was to use `buffer` instead of `passwordPtr` to create
the string. These works, but I remember reading somewhere that `&` can
be quite nasty sometimes.
- I also tried `buffer.withUnsafeMutablePointer`. However
`readpassphrase` seems to not only change the content of `buffer` but
move it. This leads to a runtime failure `Fatal error: Array
withUnsafeMutableBufferPointer: replacing the buffer is not allowed`

### Result:
The buffer is retained, the token is properly parsed and sent to the
server.
bnbarham pushed a commit to bnbarham/swift-package-manager that referenced this pull request May 18, 2024
swiftlang#7454)

Fix empty token when adding a new package-registry and adding token
interactively on release builds

### Motivation:

When executing swift package-registry login it asks for a token but
fails to retrieve it properly. It sends an empty string to the registry
server and login therefore fails. (see
swiftlang#7453)

### Modifications:

I've debugged the code a bit and found out, that somehow `buffer` and
`passwordPtr` don't seem to be holding correct values after the
`readpassphrase` call.

- An easy quick fix is to make sure that `buffer` doesn't get
deallocated by adding smth like `_ = buffer` after the String init. This
works but is not nice.
- My first try was to use `buffer` instead of `passwordPtr` to create
the string. These works, but I remember reading somewhere that `&` can
be quite nasty sometimes.
- I also tried `buffer.withUnsafeMutablePointer`. However
`readpassphrase` seems to not only change the content of `buffer` but
move it. This leads to a runtime failure `Fatal error: Array
withUnsafeMutableBufferPointer: replacing the buffer is not allowed`

### Result:
The buffer is retained, the token is properly parsed and sent to the
server.

(cherry picked from commit d8c8d0f)
shahmishal pushed a commit that referenced this pull request May 18, 2024
*Explanation*: I went through the last few months of PRs to make sure
anything relevant is cherry-picked. Most of these are NFC but
cherry-picking will help with conflicts. The main are:
* Better error message -
#7419
* Fix for visionOS for `--build-system xcode` -
#7448
* Package registry fix -
#7454
* Manifest editing API for adding target dependencies -
#7552
* Various sendable annotations

*Scope*: Package manifests/graphs with duplicate product/target names.
*Risk*: Very low
*Reviewed By*: Various, mostly @MaxDesiatov

---------

Co-authored-by: Max Desiatov <m_desiatov@apple.com>
Co-authored-by: k-kohey <kawagutsuchi@gmail.com>
Co-authored-by: miharu <35392604+mnaruse@users.noreply.github.com>
Co-authored-by: Ryu <87907656+Ryu0118@users.noreply.github.com>
Co-authored-by: Philipp Wallrich <p.wallrich@gmail.com>
Co-authored-by: Boris Bügling <bbuegling@apple.com>
Co-authored-by: coffmark <52638834+coffmark@users.noreply.github.com>
Co-authored-by: Doug Gregor <dgregor@apple.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs tests This change needs test coverage registry SwiftPM Registry-related changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants