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

Password input with no echo is not working #21731

Open
mzoliker opened this issue Apr 9, 2024 · 9 comments
Open

Password input with no echo is not working #21731

mzoliker opened this issue Apr 9, 2024 · 9 comments

Comments

@mzoliker
Copy link

mzoliker commented Apr 9, 2024

Hi,
I am trying to compile the simple program below which works fine with gcc but does not work with emcc.
The objective is to input a password with no echo on terminal for security reasons.
When compiled with emcc, getchar() is not blocking for user input, always leading to an empty password.
Is there any way to make this work with emscripten? (maybe with another method?)
Thanks a lot for your help and feedback.
Kind regards,
Maurice

Failing command lines in full:
emcc pass.c -o pass.js

Same question with:
emcc -lnodefs.js -lnoderawfs.js pass.c -o pass.js

#include <stdio.h>
#include <termios.h>
#include <unistd.h>

#define SIZE 100

void getPassword(char password[]) {
  static struct termios oldt, newt;
  int i = 0;
  int c;
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  while ((c = getchar())!= '\n' && c != EOF && i < SIZE) {
    password[i++] = c;
  }
  password[i] = '\0';
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}

int main(void) {
  char password[SIZE];
  printf("Password: ");
  getPassword(password);
  printf("\nPassword = <%s>\n", password);
  return 0;
}

Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.53 (ce5114b)
clang version 19.0.0git (https://github.com/llvm/llvm-project febb4c42b192ed7c88c17f91cb903a59acf20baf)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /emsdk/upstream/bin

@sbc100
Copy link
Collaborator

sbc100 commented Apr 9, 2024

Emscripten's TTY support is very basic. I would recommend completely avoiding tcsetattr and stdin and just writing a UI for that in JavaScript.

Altneratively if you would like to work on improving the TTY support that would be most welcome.

BTW, do the tcsetattr calls above return errors?

@mzoliker
Copy link
Author

mzoliker commented Apr 9, 2024

Thanks a lot for this very quick feedback.

Looks like tcgetattr and tcsetattr are not implemented when compiled with "-lnodefs.js -lnoderawfs.js" as I get a runtime error. No error otherwise, it runs but skips user input.

By the way, do you have any guideline on how to implement this in JavaScript? I would like to avoid -sASYNCIFY and I believe this functionality would require async/await calls which would only work from a worker thread. And this would mean redirecting stdin and stdout to the worker. Does it make sense for you or am I missing a simple and straightforward solution?

Thanks again for your help!

Kind regards,
Maurice

@sbc100
Copy link
Collaborator

sbc100 commented Apr 9, 2024

Is you final target nodejs rather than the web? If that is the case I think we can make tcgetattr work with -lnoderawfs.js fairly easily.

When I suggested implementing this yourself in JS I was assuming you were targeting the web.

@mzoliker
Copy link
Author

mzoliker commented Apr 9, 2024

Yes indeed, I am only targeting nodejs (not the Web). Are you suggesting that I need to modify emcc itself, or that I should write some kind of hook to implement tcgetattr and tcsetattr to work with -lnoderawfs.js?

Any example, pointer or direction would be much appreciated as I think that I am stuck right now. Thanks a lot!

@sbc100
Copy link
Collaborator

sbc100 commented Apr 9, 2024

Yes, I think we (you?) can modify noderawfs (part of emscripten) so that this should work.

@sbc100
Copy link
Collaborator

sbc100 commented Apr 9, 2024

Can I ask what you use case is for using emscripten-built modules inside of node js?

@mzoliker
Copy link
Author

mzoliker commented Apr 9, 2024

OK, thank you, I will look into "library_noderawfs.js" to try to make it work as expected. I would have preferred not to do so, as I will then need to port this patch on newer versions of emcc, but if this is the best way to go, I’ll do this :-).

Sorry I do not understand your question. I am just porting a C program (which is asking for a password) to JavaScript (nodejs) so that it can be portable. I managed to get it to work with echo of the password to the terminal but I would really prefer to hide this echo as it would be better for security reasons.

@sbc100
Copy link
Collaborator

sbc100 commented Apr 10, 2024

I see, so you choosing wasm over a native node extension for portability reasons.

BTW I wasn't suggesting you carry a local patch to library_noderawfs.js but rather send us a PR so we can include it upstream.

@mzoliker
Copy link
Author

OK, thanks a lot for your feedback, I have started to look into this, and I think that the chance I succeed is quite low as I am neither proficient in emscripten’s internals nor in TTY… Have a nice day!
Kind regards,
Maurice

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

No branches or pull requests

2 participants