跳轉到內容

PHP 程式設計/安全 HTTP 頭

來自華夏公益教科書,自由的教科書

PHP 指令碼返回的 HTTP 頭 可以暴露其安全漏洞。

隱藏版本

[編輯 | 編輯原始碼]

為了避免成為與 PHP 或 Web 伺服器版本相關的安全漏洞的目標,最好在 HTTP 頭中隱藏它們。

這通常在伺服器配置檔案中完成,但也可以在 PHP 中實現。

ini_set('expose_php', 'Off');
header('X-Powered-By: UnknownWebServer');

其他攻擊

[編輯 | 編輯原始碼]

可以透過配置來防止 HTTP 頭注入

保護示例

ini_set('register_globals', 'Off');

header('Content-Security-Policy "default-src \'self\'; style-src \'self\' \'unsafe-inline\'; script-src \'self\' \'unsafe-inline\'; img-src \'self\' data:"');
header('X-Frame-Options "SAMEORIGIN" always');
header('X-Content-Type-Options nosniff');
header('Referrer-Policy: origin');
header('Permissions-Policy "geolocation=(),midi=(),sync-xhr=(),microphone=(),camera=(),magnetometer=(),gyroscope=(),fullscreen=(self),payment=()"');

注意:錯誤的配置可能會導致 跨域資源共享 (CORS) 錯誤。


華夏公益教科書