Hide Windows console and allow loopback referrer verify.
Always use windows_subsystem so protocol launches do not flash a CMD window; allow localhost/127.0.0.1 for noconfirm verification without an env gate. Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -50,7 +50,7 @@ The OS protocol handler only receives the URL string — not the browser Referer
50
50
51
51
Without `referrer`, `noconfirm=1` is rejected. Without `noconfirm`, behaviour is unchanged (interactive print dialog).
52
52
53
Localhost / private IP verification URLs are blocked by default. For local harnesses only, set `POSPRINT_ALLOW_LOCAL_REFERRER=1` (see `scripts/test-noconfirm/`).
53
Localhost / loopback verification URLs are allowed. Other private LAN addresses (e.g. `192.168.x.x`) are blocked.
54
54
55
55
---
56
56
@@ -50,7 +50,7 @@ The OS protocol handler only receives the URL string — not the browser Referer50
50
51
Without `referrer`, `noconfirm=1` is rejected. Without `noconfirm`, behaviour is unchanged (interactive print dialog).51
Without `referrer`, `noconfirm=1` is rejected. Without `noconfirm`, behaviour is unchanged (interactive print dialog).52
52
53
Localhost / private IP verification URLs are blocked by default. For local harnesses only, set `POSPRINT_ALLOW_LOCAL_REFERRER=1` (see `scripts/test-noconfirm/`).53
Localhost / loopback verification URLs are allowed. Other private LAN addresses (e.g. `192.168.x.x`) are blocked.54
54
55
---55
---56
56
@@ -2,11 +2,7 @@
2
2
3
3
Docs: [docs/POSPRINT-URL.md](../../docs/POSPRINT-URL.md).
4
4
5
Production blocks localhost referrers. For this harness set:
6
7
```powershell
8
$env:POSPRINT_ALLOW_LOCAL_REFERRER = "1"
9
```
5
Loopback (`127.0.0.1` / `localhost`) is allowed for referrer verification.
10
6
11
7
## 1. Start the verify server
12
8
@@ -17,36 +13,23 @@ python scripts/test-noconfirm/server.py
17
13
- `GET http://127.0.0.1:8765/verify?code=84923` → **200**
18
14
- `GET http://127.0.0.1:8765/verify?code=wrong` → **403**
19
15
20
## 2. Build a debug posprint with the feature branch / master
16
## 2. Build and open the test URL
21
17
22
18
```powershell
23
19
cargo build
24
$env:POSPRINT_ALLOW_LOCAL_REFERRER = "1"
25
```
26
27
## 3. Open the test URL
28
29
Generate (from repo root):
30
31
```powershell
32
cargo run --quiet -- link examples/noconfirm-demo.pos
33
```
34
35
Then append (PowerShell):
36
37
```powershell
38
20
$base = cargo run --quiet -- link examples/noconfirm-demo.pos
39
21
$ref = [uri]::EscapeDataString("http://127.0.0.1:8765/verify?code=84923")
40
22
$url = "$base&noconfirm=1&referrer=$ref"
41
23
$url
42
Start-Process $url
24
# Prefer the built GUI binary (no console flash):
25
Start-Process ".\target\debug\posprint.exe" $url
43
26
```
44
27
45
First run: trust dialog for `127.0.0.1`. After **Trust and print**, the next open with the same URL should print without asking (while the server still returns 200).
28
First run: trust dialog for `127.0.0.1`. After **Trust and print**, the next open should print without asking (while the server still returns 200).
46
29
47
Try a bad code to confirm refusal:
30
Bad code (should fail verification):
48
31
49
32
```powershell
50
33
$refBad = [uri]::EscapeDataString("http://127.0.0.1:8765/verify?code=nope")
51
Start-Process "$base&noconfirm=1&referrer=$refBad"
34
Start-Process ".\target\debug\posprint.exe" "$base&noconfirm=1&referrer=$refBad"
52
35
```
@@ -2,11 +2,7 @@2
2
3
Docs: [docs/POSPRINT-URL.md](../../docs/POSPRINT-URL.md).3
Docs: [docs/POSPRINT-URL.md](../../docs/POSPRINT-URL.md).4
4
5
Production blocks localhost referrers. For this harness set:5
Loopback (`127.0.0.1` / `localhost`) is allowed for referrer verification.6
7
```powershell8
$env:POSPRINT_ALLOW_LOCAL_REFERRER = "1"9
```10
6
11
## 1. Start the verify server7
## 1. Start the verify server12
8
@@ -17,36 +13,23 @@ python scripts/test-noconfirm/server.py17
- `GET http://127.0.0.1:8765/verify?code=84923` → **200**13
- `GET http://127.0.0.1:8765/verify?code=84923` → **200**18
- `GET http://127.0.0.1:8765/verify?code=wrong` → **403**14
- `GET http://127.0.0.1:8765/verify?code=wrong` → **403**19
15
20
## 2. Build a debug posprint with the feature branch / master16
## 2. Build and open the test URL21
17
22
```powershell18
```powershell23
cargo build19
cargo build24
$env:POSPRINT_ALLOW_LOCAL_REFERRER = "1"25
```26
27
## 3. Open the test URL28
29
Generate (from repo root):30
31
```powershell32
cargo run --quiet -- link examples/noconfirm-demo.pos33
```34
35
Then append (PowerShell):36
37
```powershell38
$base = cargo run --quiet -- link examples/noconfirm-demo.pos20
$base = cargo run --quiet -- link examples/noconfirm-demo.pos39
$ref = [uri]::EscapeDataString("http://127.0.0.1:8765/verify?code=84923")21
$ref = [uri]::EscapeDataString("http://127.0.0.1:8765/verify?code=84923")40
$url = "$base&noconfirm=1&referrer=$ref"22
$url = "$base&noconfirm=1&referrer=$ref"41
$url23
$url42
Start-Process $url24
# Prefer the built GUI binary (no console flash):25
Start-Process ".\target\debug\posprint.exe" $url43
```26
```44
27
45
First run: trust dialog for `127.0.0.1`. After **Trust and print**, the next open with the same URL should print without asking (while the server still returns 200).28
First run: trust dialog for `127.0.0.1`. After **Trust and print**, the next open should print without asking (while the server still returns 200).46
29
47
Try a bad code to confirm refusal:30
Bad code (should fail verification):48
31
49
```powershell32
```powershell50
$refBad = [uri]::EscapeDataString("http://127.0.0.1:8765/verify?code=nope")33
$refBad = [uri]::EscapeDataString("http://127.0.0.1:8765/verify?code=nope")51
Start-Process "$base&noconfirm=1&referrer=$refBad"34
Start-Process ".\target\debug\posprint.exe" "$base&noconfirm=1&referrer=$refBad"52
```35
```@@ -1,4 +1,4 @@
1
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
1
#![windows_subsystem = "windows"]
2
2
3
3
mod app;
4
4
mod preview_panel;
@@ -18,7 +18,24 @@ fn load_viewport_icon() -> Option<Arc<eframe::egui::IconData>> {
18
18
.map(Arc::new)
19
19
}
20
20
21
/// Attach to the parent console when launched from cmd/PowerShell so CLI output works
22
/// with `windows_subsystem = "windows"` (no black console flash for GUI / protocol launches).
23
#[cfg(windows)]
24
fn attach_parent_console_if_present() {
25
#[link(name = "kernel32")]
26
extern "system" {
27
fn AttachConsole(dw_process_id: u32) -> i32;
28
}
29
const ATTACH_PARENT_PROCESS: u32 = 0xFFFF_FFFF;
30
unsafe {
31
let _ = AttachConsole(ATTACH_PARENT_PROCESS);
32
}
33
}
34
21
35
fn main() -> eframe::Result<()> {
36
#[cfg(windows)]
37
attach_parent_console_if_present();
38
22
39
shell_integration::ensure_shell_integration();
23
40
24
41
// Protocol URLs must be handled before clap: browsers pass the full URL as argv.
@@ -1,4 +1,4 @@1
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
1
#![windows_subsystem = "windows"]
2
2
3
mod app;
3
mod app;
4
mod preview_panel;
4
mod preview_panel;
@@ -18,7 +18,24 @@ fn load_viewport_icon() -> Option<Arc<eframe::egui::IconData>> {18
.map(Arc::new)
18
.map(Arc::new)
19
}
19
}
20
20
21
/// Attach to the parent console when launched from cmd/PowerShell so CLI output works
22
/// with `windows_subsystem = "windows"` (no black console flash for GUI / protocol launches).
23
#[cfg(windows)]
24
fn attach_parent_console_if_present() {
25
#[link(name = "kernel32")]
26
extern "system" {
27
fn AttachConsole(dw_process_id: u32) -> i32;
28
}
29
const ATTACH_PARENT_PROCESS: u32 = 0xFFFF_FFFF;
30
unsafe {
31
let _ = AttachConsole(ATTACH_PARENT_PROCESS);
32
}
33
}
34
21
fn main() -> eframe::Result<()> {
35
fn main() -> eframe::Result<()> {
36
#[cfg(windows)]
37
attach_parent_console_if_present();
38
22
shell_integration::ensure_shell_integration();
39
shell_integration::ensure_shell_integration();
23
40
24
// Protocol URLs must be handled before clap: browsers pass the full URL as argv.
41
// Protocol URLs must be handled before clap: browsers pass the full URL as argv.
@@ -87,35 +87,21 @@ fn host_from_http_url(url: &str) -> Option<String> {
87
87
}
88
88
}
89
89
90
fn allow_local_referrer() -> bool {
91
matches!(
92
std::env::var("POSPRINT_ALLOW_LOCAL_REFERRER")
93
.unwrap_or_default()
94
.to_ascii_lowercase()
95
.as_str(),
96
"1" | "true" | "yes"
97
)
98
}
99
100
90
fn is_blocked_verify_host(host: &str) -> bool {
101
91
let h = host.to_ascii_lowercase();
102
let is_loopback_name = h == "localhost" || h.ends_with(".localhost") || h == "0.0.0.0";
92
// Loopback is allowed (local verify servers / dev). Block other private LAN hosts.
93
if h == "localhost" || h.ends_with(".localhost") {
94
return false;
95
}
103
96
if let Ok(ip) = h.parse::<IpAddr>() {
104
let loopback = match ip {
105
IpAddr::V4(v4) => v4.is_loopback() || v4.is_unspecified(),
106
IpAddr::V6(v6) => v6.is_loopback() || v6.is_unspecified(),
107
};
108
if loopback {
109
return !allow_local_referrer();
110
}
111
97
return match ip {
112
IpAddr::V4(v4) => v4.is_private() || v4.is_link_local(),
113
IpAddr::V6(_) => false,
98
IpAddr::V4(v4) => {
99
!v4.is_loopback()
100
&& (v4.is_private() || v4.is_link_local() || v4.is_unspecified())
101
}
102
IpAddr::V6(v6) => !v6.is_loopback() && v6.is_unspecified(),
114
103
};
115
104
}
116
if is_loopback_name {
117
return !allow_local_referrer();
118
}
119
105
false
120
106
}
121
107
@@ -151,9 +137,6 @@ pub fn verify_referrer(referrer_url: &str) -> Result<String, ReferrerVerifyError
151
137
#[cfg(test)]
152
138
mod tests {
153
139
use super::*;
154
use std::sync::Mutex;
155
156
static ENV_LOCK: Mutex<()> = Mutex::new(());
157
140
158
141
#[test]
159
142
fn normalizes_bare_host_path_to_https() {
@@ -169,22 +152,18 @@ mod tests {
169
152
}
170
153
171
154
#[test]
172
fn blocks_localhost() {
173
let _g = ENV_LOCK.lock().unwrap();
174
std::env::remove_var("POSPRINT_ALLOW_LOCAL_REFERRER");
175
let err = normalize_referrer_url("http://127.0.0.1/verify").unwrap_err();
176
assert!(matches!(err, ReferrerVerifyError::BlockedHost(_)));
177
let err = normalize_referrer_url("https://localhost/x").unwrap_err();
178
assert!(matches!(err, ReferrerVerifyError::BlockedHost(_)));
155
fn allows_loopback_hosts() {
156
let url = normalize_referrer_url("http://127.0.0.1:8765/verify?code=1").unwrap();
157
assert_eq!(url, "http://127.0.0.1:8765/verify?code=1");
158
assert!(normalize_referrer_url("http://localhost/verify").is_ok());
179
159
}
180
160
181
161
#[test]
182
fn allows_localhost_when_env_set() {
183
let _g = ENV_LOCK.lock().unwrap();
184
std::env::set_var("POSPRINT_ALLOW_LOCAL_REFERRER", "1");
185
let url = normalize_referrer_url("http://127.0.0.1:8765/verify?code=1").unwrap();
186
assert_eq!(url, "http://127.0.0.1:8765/verify?code=1");
187
std::env::remove_var("POSPRINT_ALLOW_LOCAL_REFERRER");
162
fn blocks_private_lan_hosts() {
163
let err = normalize_referrer_url("http://192.168.1.10/verify").unwrap_err();
164
assert!(matches!(err, ReferrerVerifyError::BlockedHost(_)));
165
let err = normalize_referrer_url("http://10.0.0.5/x").unwrap_err();
166
assert!(matches!(err, ReferrerVerifyError::BlockedHost(_)));
188
167
}
189
168
190
169
#[test]
@@ -87,35 +87,21 @@ fn host_from_http_url(url: &str) -> Option<String> {87
}
87
}
88
}
88
}
89
89
90
fn allow_local_referrer() -> bool {
91
matches!(
92
std::env::var("POSPRINT_ALLOW_LOCAL_REFERRER")
93
.unwrap_or_default()
94
.to_ascii_lowercase()
95
.as_str(),
96
"1" | "true" | "yes"
97
)
98
}
99
100
fn is_blocked_verify_host(host: &str) -> bool {
90
fn is_blocked_verify_host(host: &str) -> bool {
101
let h = host.to_ascii_lowercase();
91
let h = host.to_ascii_lowercase();
102
let is_loopback_name = h == "localhost" || h.ends_with(".localhost") || h == "0.0.0.0";
92
// Loopback is allowed (local verify servers / dev). Block other private LAN hosts.
93
if h == "localhost" || h.ends_with(".localhost") {
94
return false;
95
}
103
if let Ok(ip) = h.parse::<IpAddr>() {
96
if let Ok(ip) = h.parse::<IpAddr>() {
104
let loopback = match ip {
105
IpAddr::V4(v4) => v4.is_loopback() || v4.is_unspecified(),
106
IpAddr::V6(v6) => v6.is_loopback() || v6.is_unspecified(),
107
};
108
if loopback {
109
return !allow_local_referrer();
110
}
111
return match ip {
97
return match ip {
112
IpAddr::V4(v4) => v4.is_private() || v4.is_link_local(),
98
IpAddr::V4(v4) => {
113
IpAddr::V6(_) => false,
99
!v4.is_loopback()
100
&& (v4.is_private() || v4.is_link_local() || v4.is_unspecified())
101
}
102
IpAddr::V6(v6) => !v6.is_loopback() && v6.is_unspecified(),
114
};
103
};
115
}
104
}
116
if is_loopback_name {
117
return !allow_local_referrer();
118
}
119
false
105
false
120
}
106
}
121
107
@@ -151,9 +137,6 @@ pub fn verify_referrer(referrer_url: &str) -> Result<String, ReferrerVerifyError151
#[cfg(test)]
137
#[cfg(test)]
152
mod tests {
138
mod tests {
153
use super::*;
139
use super::*;
154
use std::sync::Mutex;
155
156
static ENV_LOCK: Mutex<()> = Mutex::new(());
157
140
158
#[test]
141
#[test]
159
fn normalizes_bare_host_path_to_https() {
142
fn normalizes_bare_host_path_to_https() {
@@ -169,22 +152,18 @@ mod tests {169
}
152
}
170
153
171
#[test]
154
#[test]
172
fn blocks_localhost() {
155
fn allows_loopback_hosts() {
173
let _g = ENV_LOCK.lock().unwrap();
156
let url = normalize_referrer_url("http://127.0.0.1:8765/verify?code=1").unwrap();
174
std::env::remove_var("POSPRINT_ALLOW_LOCAL_REFERRER");
157
assert_eq!(url, "http://127.0.0.1:8765/verify?code=1");
175
let err = normalize_referrer_url("http://127.0.0.1/verify").unwrap_err();
158
assert!(normalize_referrer_url("http://localhost/verify").is_ok());
176
assert!(matches!(err, ReferrerVerifyError::BlockedHost(_)));
177
let err = normalize_referrer_url("https://localhost/x").unwrap_err();
178
assert!(matches!(err, ReferrerVerifyError::BlockedHost(_)));
179
}
159
}
180
160
181
#[test]
161
#[test]
182
fn allows_localhost_when_env_set() {
162
fn blocks_private_lan_hosts() {
183
let _g = ENV_LOCK.lock().unwrap();
163
let err = normalize_referrer_url("http://192.168.1.10/verify").unwrap_err();
184
std::env::set_var("POSPRINT_ALLOW_LOCAL_REFERRER", "1");
164
assert!(matches!(err, ReferrerVerifyError::BlockedHost(_)));
185
let url = normalize_referrer_url("http://127.0.0.1:8765/verify?code=1").unwrap();
165
let err = normalize_referrer_url("http://10.0.0.5/x").unwrap_err();
186
assert_eq!(url, "http://127.0.0.1:8765/verify?code=1");
166
assert!(matches!(err, ReferrerVerifyError::BlockedHost(_)));
187
std::env::remove_var("POSPRINT_ALLOW_LOCAL_REFERRER");
188
}
167
}
189
168
190
#[test]
169
#[test]