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

VSCode terminal doesn't allow/request permissions to access media devices #95062

Closed
jvanheugten opened this issue Apr 12, 2020 · 51 comments
Closed
Assignees
Labels
author-verification-requested Issues potentially verifiable by issue author bug Issue identified by VS Code Team member as probable bug confirmed Issue has been confirmed by VS Code Team member insiders-released Patch has been released in VS Code Insiders macos-catalina terminal Integrated terminal issues verified Verification succeeded
Milestone

Comments

@jvanheugten
Copy link

  • VSCode Version: 1.44.0
  • OS Version: Max OSX Catalina 10.15.2

Steps to Reproduce:

  1. Run this python script in the VSCode terminal which needs access to the camera:
import cv2

cam = cv2.VideoCapture(0)
print(cam.isOpened())
cam.release()

The result will be Abort trap: 6. This also occurs if you try to run the debugger.
The script does work in the Mac Terminal and iTerm.

This is likely because vscode does not have permissions to access the camera. There is no way to force vscode to appear in the Mac permissions (Security & Privacy -> Privacy -> Camera), such that it cannot be enabled by the user.

Does this issue occur when all extensions are disabled?: Yes

@vscodebot
Copy link

vscodebot bot commented Apr 12, 2020

(Experimental duplicate detection)
Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

@jvanheugten
Copy link
Author

(Experimental duplicate detection)
Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

Probably both issues have similar origin, namely camera permissions are not set for vscode.

@deepak1556
Copy link
Contributor

@jvanheugten your assumption is correct. But if your macOS terminal app got the permissions to access camera, then any process spawned from it should get that. I am unable to reproduce the crash with the terminal from inside code after I grant the terminal access to camera.

Can you share a video demo ?

@deepak1556 deepak1556 added info-needed Issue requires more information from poster macos-catalina labels Apr 13, 2020
@jvanheugten
Copy link
Author

image
Here is a simple demonstration. Mac OS Terminal shows the correct result, but terminal in VSCode does not.

Does VSCode spawn a emulated terminal? If MacOS thinks it is VSCode that spawns it, I would expect a permission request to access the camera, but that never happens. My privacy settings do show that Terminal has access to the camera, but there is no way to add VSCode to that list.

@deepak1556
Copy link
Contributor

Have your restarted code after giving your macOS terminal access to camera ? Otherwise code wouldn't pick up the privilege for the terminals it opens.

Does VSCode spawn a emulated terminal?

@Tyriar how do we spawn terminals on macOS ?

I tried the following steps and it seems to work , https://streamable.com/zsa8ro

My privacy settings do show that Terminal has access to the camera, but there is no way to add VSCode to that list.

Yes you can't do that at the moment, because code doesn't carry the entitlement that requests access to camera. I am looking to avoid adding this unless necessary, but for the current use case through the terminal things should work the way it is.

@jvanheugten
Copy link
Author

Yes, I've restarted vscode a couple of times. I'll try uninstalling and reinstalling it from scratch tomorrow.

@Tyriar
Copy link
Member

Tyriar commented Apr 15, 2020

@deepak1556

https://github.com/microsoft/node-pty/blob/cd72d3303cb484e402381741e7e6a613886eda7b/src/unix/pty.cc#L441

@jvanheugten
Copy link
Author

I just completely uninstalled VSCode and re-installed it and restarted my computer. The problem is still there.

I even deleted everything according to similar paths here: https://stackoverflow.com/questions/42603103/how-to-completely-uninstall-vscode-on-mac

@jvanheugten
Copy link
Author

jvanheugten commented Apr 15, 2020

Just ran VSCode as administrator:
sudo /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron
Then my terminal in VSCode will run as root and the program executes as normal.

Of course, that is not a real solution though, but it shows some permissions are not handled correctly.

@deepak1556
Copy link
Contributor

Thanks for the context, based on #95062 (comment) since we execvp, macOS tcc utility will consider the permissions of the parent process instead of the forked process.

But I am still confused why it wasn't an issue with my setup, moving this next milestone for further investigation.

@deepak1556 deepak1556 added bug Issue identified by VS Code Team member as probable bug confirmed Issue has been confirmed by VS Code Team member terminal Integrated terminal issues and removed info-needed Issue requires more information from poster labels Apr 15, 2020
@deepak1556 deepak1556 added this to the May 2020 milestone Apr 15, 2020
@jelling
Copy link

jelling commented Apr 27, 2020

Similar issue exists with access to the microphone via Pythoh. In both cases it seems that the VSCode terminal is not requesting permissions from MacOS. I say this because if you run the same CLI command via the built-in MacOS Terminal app then permissions are requested and everything works as it should.

Proposed solution: update VSCode terminal to request permissions from MacOS.

@deepak1556 deepak1556 changed the title VSCode terminal doesn't have access to Mac Camera VSCode terminal doesn't allow/request permissions to access media devices Apr 27, 2020
@deepak1556
Copy link
Contributor

@jelling thanks for the report. the root cause is same. I have updated the bug title.

@MHDFahz
Copy link

MHDFahz commented May 6, 2020

I tried all those step but doesn't working

@becting
Copy link

becting commented May 7, 2020

I am having the same issue the python code runs without issue from terminal which is able to request the appropriate permissions. VSCode however does not request permissions as expected.

The line
cap = cv.VideoCapture(0)
generates the output
OpenCV: not authorized to capture video (status 0), requesting...
yet there is no request

@mattialibera
Copy link

@csarami see solution I posted above: #95062 (comment); it should work for Mojave too.

@mattialibera But there are no such attributes as " auth_value, auth_reason, auth_version" in the table in Mojave.

@csarami my solution doesn't mention those attributes; below the screenshot:
image

@wrogner
Copy link

wrogner commented Feb 18, 2021

The table structure in Mojave is different to BigSur.

With your qualifications you should be able to figure out the attributes.

@Naohiro2g
Copy link

First of all, there is no need to be in the recovery mode to edit the database but terminal app needs full disk access.

Mojave version is here, at your own risk. Be careful.

cd ~/Library/Application\ Support/com.apple.TCC/
cp TCC.db TCC.db.bak
sqlite3 TCC.db    # sqlite> prompt appears.


# for Camera

INSERT into access VALUES('kTCCServiceCamera',"com.microsoft.VSCode",0,1,1,NULL,NULL,NULL,'UNUSED',NULL,0,1541440109);

#  for Microphone

INSERT into access VALUES('kTCCServiceMicrophone','com.microsoft.VSCode',0,1,1,NULL,NULL,NULL,'UNUSED',NULL,0,1541440109);

@ph1p
Copy link

ph1p commented Feb 20, 2021

Thanks @Naohiro2g ! Worked for me on Big Sur 11.3 Beta after adding another value and restarting VSCode.

For the Insider version, just use "com.microsoft.VSCodeInsiders".

VALUES('kTCCServiceCamera',"com.microsoft.VSCode",0,1,1,1,NULL,NULL,NULL,'UNUSED',NULL,0,1541440109);

@Naohiro2g
Copy link

Naohiro2g commented Feb 21, 2021

ph1q, thanks for the info regarding BigSur and insiders.

We'd better to wait for the bug fix release but you can try the solution at your own risk. BE CAREFUL, IT'S DANGEROUS ENOUGH TO EDIT THE DATABASE BY HAND.

You Have Been Warned.

Check if 'Full Disk Access' is granted to the terminal app you are using inside VS Code.

  • Open System Preferences ▸ Security & Privacy. Select the Privacy tab and select Full Disk Access,
  • If you can't see the terminal app there, click the lock icon to unlock, then add the app.
cd ~/Library/Application\ Support/com.apple.TCC/
cp TCC.db TCC.db.bak
sqlite3 TCC.db    # sqlite> prompt appears.
for Mojave, Catalina
INSERT into access VALUES('kTCCServiceCamera',"com.microsoft.VSCode",0,1,1,NULL,NULL,NULL,'UNUSED',NULL,0,1541440109);

for BigSur
INSERT into access VALUES('kTCCServiceCamera',"com.microsoft.VSCode",0,1,1,1,NULL,NULL,NULL,'UNUSED',NULL,0,1541440109);

for Microphone, replace the keyword with
'kTCCServiceMicrophone'

for VS Code Insiders, go with
'com.microsoft.VSCodeInsiders'

@sst2010
Copy link

sst2010 commented Feb 21, 2021

Comment: #95062 (comment)

cd ~/Library/Application Support/com.apple.TCC
cp TCC.db TCC.db_backup
sqlite3 TCC.db
INSERT into access (service, client, client_type, auth_value, auth_reason, auth_version) VALUES ("kTCCServiceCamera","com.microsoft.VSCode",0,2,0,1);
INSERT into access (service, client, client_type, auth_value, auth_reason, auth_version) VALUES ("kTCCServiceMicrophone","com.microsoft.VSCode",0,2,0,1);
.exit

Thanks, @wrogner for this useful solution !!

@GusnaIkhsan
Copy link

I code with vscode but run the code with iTerm. Its work for me

@Naohiro2g
Copy link

@GusnaIkhsan You launch VS Code via code . command on the terminal, right? We knew it can be done as the first and the safer workaround. But we are too lazy and would like to have every options in hand. :)

If you have VS Code open all the time, and are used to using the terminal in VS Code, you may find it a bit cumbersome to restart VS Code using the terminal outside. And you need to grant full disk access permission to the terminal app beforehand, like in another workaround.

@adgbu
Copy link

adgbu commented Mar 11, 2021

Using the webcam (and virtual webcams) with Python+OpenCV from Terminal works fine. I can not get it to work within VSCode no matter what I do.

I have tried to grant permission to VSCode through the TCC but it still won't work. Don't know if I did it right.

I used this tool tccutil by jacobsalmela. I also had to reboot into Recovery mode and disable SIP before I could try this. I got the util to work, but still no camera access inside VSCode.

I was thinking whether it would be possible to write a VSCode extension that just opens the camera and the microphone and does so by the book according to Apple's API recommendation. Would that bring up the dialog to grant permission to these resources for VSCode?

@mattialibera
Copy link

Using the webcam (and virtual webcams) with Python+OpenCV from Terminal works fine. I can not get it to work within VSCode no matter what I do.

I have tried to grant permission to VSCode through the TCC but it still won't work. Don't know if I did it right.

I used this tool tccutil by jacobsalmela. I also had to reboot into Recovery mode and disable SIP before I could try this. I got the util to work, but still no camera access inside VSCode.

I was thinking whether it would be possible to write a VSCode extension that just opens the camera and the microphone and does so by the book according to Apple's API recommendation. Would that bring up the dialog to grant permission to these resources for VSCode?

On MacOS sometimes OpenCV has some communication problems with some webcams; I'm almost sure that the problem is about that and not related with vscode in this case.

@adgbu
Copy link

adgbu commented Mar 12, 2021

@mattialibera
Then why does it work perfectly from Terminal but not from within VSCode?
It's even the same Python virtual environment. The only difference is inside or outside VSCode.

@mattialibera
Copy link

@adgbu did you use exactly commands in my/others posts above? Which OS version do you have?

@adgbu
Copy link

adgbu commented Mar 12, 2021

@mattialibera No, I used the tccutil tool.
I got it working now, by using the SQL INSERT commands directly.
A couple of things that may help someone else:

  1. There is a typo in your SQL command for Catalina where is says "com.microsoft.VSCoda".
  2. My biggest problem was that I could not read the TCC.db file or even list that directory until after I booted into Recovery mode and turned off SIP. What I really should have done was to turn on Full disk access for Terminal, but I didn't know that.

Thanks for taking the time to answer.

@HoBu23
Copy link

HoBu23 commented Mar 13, 2021

macOS Big Sur, version 11.2.3.

I have written a script in Python3 for speech recognition. When I run this script in VSCode internal terminal, I can't pick up any signal from the microphone. Then I tried to run my script natively in MAC terminal, it works after accepting the permission for microphone.
I tried again in VSCode internal terminal and same problem as before. No signal from the microphone.
I miss the permission for microphone in the VSCode terminal.

I tried out the way by @wrogner with no success at first.
Then i found Comment: #95062 (comment)
"""
Also, before running the above comments, you need to grant Full Disk Access to Terminal.
You can do so at: System Preferences -> Security & Privacy -> Privacy tab -> Full Disk Access row in left sidebar
"""
That was the key. Now i can get data from microphon in VSCode.

Thanks @wrogner and @sst2010

@wrogner
Copy link

wrogner commented Mar 13, 2021

@HoBu23 : Please read the suggested solutions (and verify them) before submitting an addition.
Solutions for > 10.15.x by @wrogner
Solutions for < 10.15.x by @mattialibera

@HoBu23
Copy link

HoBu23 commented Mar 13, 2021

@HoBu23 : Please read the suggested solutions (and verify them) before submitting an addition.
Solutions for > 10.15.x by @wrogner
Solutions for < 10.15.x by @mattialibera

okay, thanks for your info, but I have no success with @wrogner way. Problem with open .db in sqlite3. But with comment from @sst2010 it works.

Now I tried it in IntelliJ IDEA.

This is the result:
IntelliJ IDEA

I think Microsoft should still think about this

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
author-verification-requested Issues potentially verifiable by issue author bug Issue identified by VS Code Team member as probable bug confirmed Issue has been confirmed by VS Code Team member insiders-released Patch has been released in VS Code Insiders macos-catalina terminal Integrated terminal issues verified Verification succeeded
Projects
None yet
Development

No branches or pull requests