You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following example worked on macOS. If it worked for you too, you may have placed the files in an insecure directory.
package main
import (
"log""os""path/filepath""github.com/pkg/browser"
)
funcmain() {
pathDirTmp, err:=os.MkdirTemp(os.TempDir(), "sample_dir_*")
iferr!=nil {
log.Fatal(err)
}
pathFileTmp:=filepath.Join(pathDirTmp, "sample_file.html")
dummyContent:=`<html><body>Hello World2</body></html>`// Create file under the temporary directoryiferr:=os.WriteFile(pathFileTmp, []byte(dummyContent), 0644); err!=nil {
log.Fatal(err)
}
// Open the file in the browser// if err := browser.OpenURL(pathFileTmp); err != nil {// log.Fatal(err)// }// Open the file for readingosFile, err:=os.Open(pathFileTmp)
iferr!=nil {
log.Fatal(err)
}
// Consume the contents and present the results in a browseriferr:=browser.OpenReader(osFile); err!=nil {
log.Fatal(err)
}
// Open the directory of the temporary fileiferr:=browser.OpenURL(pathDirTmp); err!=nil {
log.Fatal(err)
}
}
The /tmp/browser.1293014077.html contained the response.Body value, so it is assumed that the OS or the browser is blocking it for security reasons. Not sure.
Try using os.MkdirTemp() and os.TempDir() to store the value of response.Body in a temporary directory. Then open it and look at the difference in paths.
Feel free to close this, if you don't have time and energy.
Unfortunately, I'm only a passer-by to help and don't have the privilege to close this issue.
Activity
KEINOS commentedon Aug 21, 2022
The following example worked on macOS. If it worked for you too, you may have placed the files in an insecure directory.
Env info
guettli commentedon Aug 24, 2022
You said "you may have placed the files in an insecure directory"
But I am using it like this:
This means, that I am not explicitly putting the content into a directory....
I had a look at how this gets handled by Python, but that's a huge file: https://github.com/python/cpython/blob/3.10/Lib/webbrowser.py
Sorry, I need to leave now. Feel free to close this, if you don't have time and energy.
KEINOS commentedon Aug 25, 2022
@guettli
Yes yes, that's why the file pointer is passed like this.
My point was that "Where was the temp file created?"
The path "
/tmp/browser.1293014077.html" seems to be insecure. For example, on macOS, temporary files are created inprivate/var/folders.The
/tmp/browser.1293014077.htmlcontained theresponse.Bodyvalue, so it is assumed that the OS or the browser is blocking it for security reasons. Not sure.Try using
os.MkdirTemp()andos.TempDir()to store the value ofresponse.Bodyin a temporary directory. Then open it and look at the difference in paths.Unfortunately, I'm only a passer-by to help and don't have the privilege to close this issue.