通用API文档

💡 云策文档标注

概述

本文档强调WordPress开发中的安全重要性,指出插件和主题是潜在弱点,并倡导在开发过程中培养安全意识。核心在于正确处理输入和输出数据,以保护网站免受安全威胁。

关键要点

  • 不要信任任何数据,包括用户输入、第三方API或数据库数据,必须进行验证和清理。
  • 依赖WordPress API,利用核心函数来验证和清理数据,提高安全性。
  • 保持代码更新,随着技术发展,及时维护和更新插件或主题以防范新安全漏洞。
  • 指导原则包括:永远不信任用户输入、尽可能晚地转义、转义所有不可信来源的数据、不假设任何事、验证/拒绝优于清理。

📄 原文内容

Congratulations, your code works! But is it safe?

The WordPress development team takes security seriously. With so much of the web relying on the integrity of the platform, security is critical. While the core developers have a dedicated team focused on securing the core platform, as a theme or plugin developer you are quite aware that there is potentially much that is outside the core which can be vulnerable. Because WordPress provides so much power and flexibility, plugins and themes are key points of weakness.

When writing code that will run across hundreds if not thousands of websites, you should be extra cautious of how you handle data coming into WordPress and how it’s then presented to the end user. This commonly comes up when building a settings page for your theme, creating and manipulating shortcodes, or saving and rendering extra data associated with a post.

Developing a Security Mindset

When developing, it is important to consider security as you add functionality. Use the following principles as you progress through your development efforts:

  • Don’t trust any data. Don’t trust user input, third-party APIs, or data in your database without verification. Protection of your WordPress themes begins with ensuring the data entering and leaving your theme is as intended. Always make sure to validate and sanitize user input before using it, and to escape on output.
  • Rely on the WordPress API. Many core WordPress functions provide the build in the functionality of validating and sanitizing data. Rely on the WordPress provided functions when possible.
  • Keep your code up to date. As technology evolves, so does the potential for new security holes in your plugin or theme. Stay vigilant by maintaining your code and updating as required.

Guiding principles

  1. Never trust user input.
  2. Escape as late as possible.
  3. Escape everything from untrusted sources (e.g., databases and users), third-parties (e.g., Twitter), etc.
  4. Never assume anything.
  5. Sanitation is okay, but validation/rejection is better.