Cabeçalhos de segurança e HTTPS
Defina cabeçalhos que bloqueiem ataques comuns.
Cabeçalhos de segurança e HTTPS é uma aula grátis de Flask Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Flask Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Flask Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
Headers as a First Defense
A few response headers tell the browser how to behave safely. They are cheap to add and block whole classes of attacks.
Why HTTPS Is Non-Negotiable
Over plain HTTP, anyone on the path can read or change traffic. HTTPS encrypts it so passwords and tokens stay private.
Force HTTPS with HSTS
The Strict-Transport-Security header tells browsers to always use HTTPS for your domain, even if a user types http.
resp.headers["Strict-Transport-Security"] = "max-age=31536000"Stop MIME Sniffing
Browsers sometimes guess a file type and run it. X-Content-Type-Options: nosniff tells them to trust your declared type instead.
resp.headers["X-Content-Type-Options"] = "nosniff"Block Clickjacking
Attackers can hide your site in an invisible frame. X-Frame-Options: DENY stops your pages from being framed at all.
resp.headers["X-Frame-Options"] = "DENY"Content Security Policy
A Content-Security-Policy limits where scripts and styles may load from. It is the strongest single guard against injected scripts.
resp.headers["Content-Security-Policy"] = "default-src 'self'"Add Headers Everywhere
You set these on every response in one place. An after_request hook stamps the headers so you never forget a route.
@app.after_request
def secure(resp):
resp.headers["X-Frame-Options"] = "DENY"
return respLet a Library Help
Doing it by hand is error prone, so many teams reach for Flask-Talisman. It sets sensible security headers for you.
from flask_talisman import Talisman
Talisman(app)Mark Cookies Secure
Tell the browser to send cookies only over HTTPS with the Secure flag, and hide them from scripts with HttpOnly.
app.config["SESSION_COOKIE_SECURE"] = TrueHide Your Server Banner
Default error pages can leak versions. Trimming the Server header gives attackers one less hint about your stack.
Terminate TLS at the Edge
In production a proxy like Nginx usually handles the certificate. Flask trusts it via ProxyFix to read the real scheme and IP.
Quick Check
Identify the header that forces secure transport.
Recap
You enabled HTTPS, added HSTS, nosniff, frame, and CSP headers, secured cookies, and let Talisman help. You hardened the edge nicely!
Perguntas Frequentes
A aula “Cabeçalhos de segurança e HTTPS” é grátis?
Sim — o texto completo de “Cabeçalhos de segurança e HTTPS” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Flask Academy, atualize para CoddyKit PRO. O curso de Flask Academy inclui 4 aulas no total.
O que vou aprender em “Cabeçalhos de segurança e HTTPS”?
Defina cabeçalhos que bloqueiem ataques comuns. Você pratica Flask Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Flask Academy?
Nenhuma experiência prévia é necessária. Flask Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Cabeçalhos de segurança e HTTPS”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Flask Academy?
Sim. Cada aula de Flask Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Limite solicitações com Flask-Limiter
- Configure CORS para clientes de navegador
- Cabeçalhos de segurança e HTTPS
- Valide a entrada para impedir injeções