{"id":1600,"date":"2026-09-09T15:00:00","date_gmt":"2026-09-09T09:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/"},"modified":"2026-09-09T15:00:00","modified_gmt":"2026-09-09T09:30:00","slug":"apex-secure-coding-xss-csrf-injection","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/","title":{"rendered":"Apex Secure Coding: Stop XSS, CSRF and SOQL Injection"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Apex Secure Coding means writing Apex, Visualforce and Lightning code that attackers cannot trick into leaking or changing data. Sharing, CRUD and FLS protect data from honest users. Secure coding protects it from people who send unexpected input on purpose, through a URL, a form field or a crafted link.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, you will learn the attacks Salesforce developers meet most: SOQL injection, cross-site scripting (XSS), cross-site request forgery (CSRF), open redirects and hard-coded secrets. For each one, you will see unsafe code and the safe fix. It builds on <a href=\"https:\/\/swapnilzambare.com\/blog\/dynamic-soql-in-apex-injection\/\">dynamic SOQL and SOQL injection<\/a>, <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-crud-and-fls-user-mode\/\">Apex CRUD and FLS<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-managed-sharing-share-records\/\">Apex managed sharing<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Apex Secure Coding?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apex Secure Coding is a set of habits that treat every outside value as untrusted until you check it. Outside values include URL parameters, form inputs, JSON from a component and data from other systems. Safe code keeps that input as data, never as code, query text or HTML.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a bank teller. When a customer hands over a note saying &#8220;Give me 500 rupees&#8221;, a careful teller checks the account and the amount. However, a careless teller follows any note, even one saying &#8220;Also open the vault&#8221;. Secure code is the careful teller.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Apex Secure Coding Matters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Salesforce protects the platform, but your custom code is your responsibility. Here is why Apex Secure Coding matters at Bright Path Tutors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, a search box on a public course page passes text straight into a query.<\/li>\n\n<li>Also, a Visualforce page shows a student&#8217;s comment, which might contain script.<\/li>\n\n<li>Next, a link that cancels an enrolment could be clicked from a phishing email.<\/li>\n\n<li>A login page redirect could send students to a fake site.<\/li>\n\n<li>Finally, an API key in Apex code is visible to anyone who can read the class.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So without Apex Secure Coding, one weak line can undo every sharing rule you set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Apex Secure Coding Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apex Secure Coding follows the same steps for any input. First, find every place where outside data enters your code. Next, decide what that data should look like, such as an Id, a number or plain text. Then validate it, for example with an allow list or Id.valueOf. After that, use it safely: bind variables in queries, encoding in HTML, and POST requests for changes. Finally, test the code with hostile input and run Code Analyzer.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-diagram-1-1024x640.png\" alt=\"Apex Secure Coding diagram showing find inputs, expect, validate, use safely, test and scan\" class=\"wp-image-1598\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-diagram-1-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-diagram-1-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">The steps for handling any outside input safely.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">SOQL Injection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">SOQL injection happens when user text is joined into a dynamic query, so the user can change its logic. Bind variables or String.escapeSingleQuotes stop it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Cross-Site Scripting (XSS)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">XSS happens when user input is shown as HTML, so a script runs in another user&#8217;s browser. Visualforce and LWC escape output by default. The risk appears when you turn escaping off, such as escape=&#8221;false&#8221; or setting innerHTML.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Cross-Site Request Forgery (CSRF)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CSRF tricks a logged-in user into sending a request they did not intend. In Visualforce, the risk appears when code changes data on page load, in a constructor or an action attribute. Salesforce protects normal form posts with a token.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Open Redirects<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An open redirect sends users to any URL passed in a parameter, such as retURL. Attackers use it to make phishing links look trusted. So allow only relative URLs or known domains.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Hard-Coded Secrets<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Passwords, tokens and API keys in Apex are readable by anyone with code access. Store them in named credentials, external credentials or protected custom metadata instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Attacks and Fixes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below sums up each attack and its fix.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Attack<\/th><th>Unsafe pattern<\/th><th>Safe fix<\/th><\/tr><\/thead><tbody><tr><td>SOQL injection<\/td><td>Joining user text into a query<\/td><td>Bind variables<\/td><\/tr><tr><td>XSS<\/td><td>escape=&#8221;false&#8221; or innerHTML<\/td><td>Default escaping, HTMLENCODE<\/td><\/tr><tr><td>CSRF<\/td><td>DML in constructor or page action<\/td><td>Change data only on user POST<\/td><\/tr><tr><td>Open redirect<\/td><td>Redirect to any retURL<\/td><td>Allow relative URLs only<\/td><\/tr><tr><td>Hard-coded secret<\/td><td>Key in an Apex string<\/td><td>Named or external credential<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">XSS vs CSRF<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below compares the two web attacks people mix up most.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>XSS<\/th><th>CSRF<\/th><\/tr><\/thead><tbody><tr><td>What runs<\/td><td>Attacker&#8217;s script in the page<\/td><td>A real request from the user<\/td><\/tr><tr><td>Main cause<\/td><td>Unescaped output<\/td><td>Data changes without user intent<\/td><\/tr><tr><td>Victim sees<\/td><td>A page that behaves oddly<\/td><td>Often nothing at all<\/td><\/tr><tr><td>Salesforce default protection<\/td><td>Output escaping<\/td><td>Tokens on form posts<\/td><\/tr><tr><td>Your fix<\/td><td>Keep escaping on<\/td><td>No DML on page load<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When to use which fix depends on the attack. For XSS, protect output. For CSRF, protect actions that change data.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-diagram-2-1024x640.png\" alt=\"Apex Secure Coding diagram comparing XSS and CSRF\" class=\"wp-image-1599\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-diagram-2-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-diagram-2-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">XSS vs CSRF.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Apex Secure Coding Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s apply Apex Secure Coding to three pieces of code at Bright Path Tutors.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Fix SOQL Injection<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ UNSAFE: user text becomes part of the query\nString q = 'SELECT Id, Name FROM Course__c WHERE Name LIKE \\'%' + searchText + '%\\'';\nList&lt;Course__c&gt; bad = Database.query(q);\n\n\/\/ SAFE: user text stays a bind value\nString pattern = '%' + searchText + '%';\nList&lt;Course__c&gt; good = [SELECT Id, Name FROM Course__c\n                        WHERE Name LIKE :pattern\n                        WITH USER_MODE];<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the unsafe version, a search such as <code>x' OR Name != '<\/code> changes the filter and returns every course. In the safe version, the text is only a value, so the query logic cannot change.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Fix XSS in Visualforce<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- UNSAFE: student comment rendered as HTML --&gt;\n&lt;apex:outputText value=\"{!enrolment.Comment__c}\" escape=\"false\"\/&gt;\n\n&lt;!-- SAFE: default escaping, and encoding inside JavaScript --&gt;\n&lt;apex:outputText value=\"{!enrolment.Comment__c}\"\/&gt;\n&lt;script&gt;\n    var comment = '{!JSENCODE(enrolment.Comment__c)}';\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With escape=&#8221;false&#8221;, a comment containing a script tag runs in the tutor&#8217;s browser. The safe version keeps escaping on. Inside JavaScript, JSENCODE stops the text from breaking out of the string. In LWC, the same rule means binding text with {comment} instead of setting innerHTML.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Fix CSRF in a Visualforce Controller<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class CancelEnrolmentController {\n\n    public Id enrolmentId { get; set; }\n\n    \/\/ UNSAFE: cancelling inside the constructor runs on a simple GET link\n    \/\/ public CancelEnrolmentController() { cancel(); }\n\n    public CancelEnrolmentController() {\n        enrolmentId = ApexPages.currentPage().getParameters().get('id');\n    }\n\n    \/\/ SAFE: runs only when the user clicks the Cancel button (a POST)\n    public PageReference cancel() {\n        update as user new Enrolment__c(Id = enrolmentId, Status__c = 'Cancelled');\n        return null;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The constructor only reads the Id. The update happens in an action method bound to a command button, which Salesforce protects with a CSRF token. So a link in an email cannot cancel enrolments on its own.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Check with a Test<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here is what a quick security test of the search method shows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>search('x\\' OR Name != \\'')  -&gt; 0 courses (safe)\nsearch('Apex')               -&gt; 3 courses<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As a result, the hostile input finds nothing, while normal searches still work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Apex Secure Coding for Secrets and Redirects<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Never write an API key in Apex. Instead, store it in a named credential, which Level 9 covers. For redirects, accept only values that start with a single slash, or compare the domain against an allow list, before calling new PageReference(url).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Apex Secure Coding<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Joining user input into dynamic SOQL.<\/li>\n\n<li>Turning off escaping to show formatted text.<\/li>\n\n<li>Running DML in constructors or page action attributes.<\/li>\n\n<li>Redirecting to any URL from a parameter.<\/li>\n\n<li>Storing passwords and tokens in Apex or plain custom settings.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Apex Secure Coding<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Treat every outside value as untrusted.<\/li>\n\n<li>Use bind variables and allow lists for dynamic queries.<\/li>\n\n<li>Keep default escaping on, and encode for the context.<\/li>\n\n<li>Change data only on explicit user actions, never on page load.<\/li>\n\n<li>Run Salesforce Code Analyzer on every change.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Write a method that sorts courses by a field name passed from a component. Allow only Name, Fee__c and Start_Date__c, and reject anything else. Hint: check the value against a Set&lt;String&gt; before building the ORDER BY clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Interview Questions on Apex Secure Coding<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Security review questions appear in many senior interviews. Here are the 21 questions asked most about Apex Secure Coding.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Questions on Apex Secure Coding<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What is SOQL injection?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An attack where user input changes the logic of a dynamic SOQL query.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: How do you prevent SOQL injection?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use bind variables, allow lists, and String.escapeSingleQuotes when binding is impossible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: What is XSS?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Cross-site scripting, where attacker input runs as script in another user&#8217;s browser.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: Does Visualforce escape output by default?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, unless you set escape=&#8221;false&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: What is CSRF?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Cross-site request forgery, where a user is tricked into sending a request that changes data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: Where should you store API keys?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In named credentials or external credentials, not in Apex code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: What is an open redirect?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A redirect to any URL taken from user input, often used in phishing.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code and Scenario Questions on Apex Secure Coding<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: Why is DML in a Visualforce constructor risky?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It runs on a simple GET request, so a link can change data without a CSRF token.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: What does JSENCODE do?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It encodes text so it is safe inside a JavaScript string.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: When would you use HTMLENCODE?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you output user data inside HTML that is not escaped automatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: How does LWC protect against XSS?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Template bindings escape values. Risk appears when you set innerHTML or use lwc:dom=&#8221;manual&#8221; with user data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: How do you safely allow a dynamic sort field?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check it against an allow list of field names before adding it to the query.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: Is String.escapeSingleQuotes enough on its own?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It helps for string values, but binds and allow lists are safer and cover more cases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: How do you validate an Id parameter?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Convert it with Id.valueOf inside try and catch, and check its object type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Questions on Apex Secure Coding<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: Why do sharing and CRUD checks not stop XSS?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They control data access, while XSS abuses how allowed data is displayed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: How do you protect redirects such as retURL?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Allow only relative paths or known domains.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: Should error pages show stack traces?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Show a friendly message and log the details, because traces reveal code structure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: What does the AppExchange security review check?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Injection, XSS, CSRF, CRUD and FLS, sharing, secrets and insecure endpoints, among others.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: How do you protect JSON input from a component?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Deserialize into typed classes, validate values, and run user mode or stripInaccessible before DML.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: Is data from an integration trusted?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Validate it like user input, because the other system may be compromised.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: Which tool finds many of these issues automatically?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Salesforce Code Analyzer, with PMD and other engines.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/platform\/apex-dev\/references\/apex-dev\/pages-security-tips-intro.html\" target=\"_blank\" rel=\"noopener noreferrer\">Security Tips for Apex and Visualforce Development (Apex Developer Guide)<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/platform\/apex-dev\/references\/apex-dev\/pages-security-tips-soql-injection.html\" target=\"_blank\" rel=\"noopener noreferrer\">SOQL Injection (Apex Developer Guide)<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/platform\/salesforce-pages-developers-guide\/guide\/pages-security-tips-xss.html\" target=\"_blank\" rel=\"noopener noreferrer\">Cross Site Scripting (XSS) (Visualforce Developer Guide)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Apex Secure Coding treats every outside value as untrusted.<\/li>\n\n<li>Bind variables and allow lists stop SOQL injection.<\/li>\n\n<li>Default escaping and encoding stop XSS.<\/li>\n\n<li>Changing data only on user POST actions stops CSRF.<\/li>\n\n<li>Secrets belong in named or external credentials.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Tools can catch many of these issues before review. Next, read our guide to <a href=\"https:\/\/swapnilzambare.com\/blog\/salesforce-code-analyzer-pmd\/\">Salesforce Code Analyzer and PMD<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apex Secure Coding means writing Apex, Visualforce and Lightning code that attackers cannot trick into leaking or changing data. Sharing, CRUD and FLS protect&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1597,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96],"tags":[68,98,91,7,97,17],"swapz_series":[],"class_list":["post-1600","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","tag-advanced","tag-apex","tag-interview-questions","tag-salesforce","tag-salesforce-developer","tag-security"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Apex Secure Coding: Stop XSS, CSRF and Injection<\/title>\n<meta name=\"description\" content=\"Apex Secure Coding explained simply: SOQL injection, XSS, CSRF, open redirects, hard-coded secrets, safe Visualforce and LWC patterns and 21 interview Q&amp;A.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apex Secure Coding: Stop XSS, CSRF and Injection\" \/>\n<meta property=\"og:description\" content=\"Apex Secure Coding explained simply: SOQL injection, XSS, CSRF, open redirects, hard-coded secrets, safe Visualforce and LWC patterns and 21 interview Q&amp;A.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-09T09:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-secure-coding-xss-csrf-injection\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-secure-coding-xss-csrf-injection\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\"},\"headline\":\"Apex Secure Coding: Stop XSS, CSRF and SOQL Injection\",\"datePublished\":\"2026-09-09T09:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-secure-coding-xss-csrf-injection\\\/\"},\"wordCount\":1674,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-secure-coding-xss-csrf-injection\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-secure-coding-xss-csrf-injection-featured.png\",\"keywords\":[\"Advanced\",\"Apex\",\"Interview Questions\",\"Salesforce\",\"Salesforce Developer\",\"Security\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-secure-coding-xss-csrf-injection\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-secure-coding-xss-csrf-injection\\\/\",\"name\":\"Apex Secure Coding: Stop XSS, CSRF and Injection\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-secure-coding-xss-csrf-injection\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-secure-coding-xss-csrf-injection\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-secure-coding-xss-csrf-injection-featured.png\",\"datePublished\":\"2026-09-09T09:30:00+00:00\",\"description\":\"Apex Secure Coding explained simply: SOQL injection, XSS, CSRF, open redirects, hard-coded secrets, safe Visualforce and LWC patterns and 21 interview Q&A.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-secure-coding-xss-csrf-injection\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-secure-coding-xss-csrf-injection\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-secure-coding-xss-csrf-injection\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-secure-coding-xss-csrf-injection-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-secure-coding-xss-csrf-injection-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Apex Secure Coding featured image: title card with an Apex code example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-secure-coding-xss-csrf-injection\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Apex Secure Coding: Stop XSS, CSRF and SOQL Injection\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apex Secure Coding: Stop XSS, CSRF and Injection","description":"Apex Secure Coding explained simply: SOQL injection, XSS, CSRF, open redirects, hard-coded secrets, safe Visualforce and LWC patterns and 21 interview Q&A.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/","og_locale":"en_US","og_type":"article","og_title":"Apex Secure Coding: Stop XSS, CSRF and Injection","og_description":"Apex Secure Coding explained simply: SOQL injection, XSS, CSRF, open redirects, hard-coded secrets, safe Visualforce and LWC patterns and 21 interview Q&A.","og_url":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-09-09T09:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a"},"headline":"Apex Secure Coding: Stop XSS, CSRF and SOQL Injection","datePublished":"2026-09-09T09:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/"},"wordCount":1674,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-featured.png","keywords":["Advanced","Apex","Interview Questions","Salesforce","Salesforce Developer","Security"],"articleSection":["Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/","url":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/","name":"Apex Secure Coding: Stop XSS, CSRF and Injection","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-featured.png","datePublished":"2026-09-09T09:30:00+00:00","description":"Apex Secure Coding explained simply: SOQL injection, XSS, CSRF, open redirects, hard-coded secrets, safe Visualforce and LWC patterns and 21 interview Q&A.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-secure-coding-xss-csrf-injection-featured.png","width":2400,"height":1260,"caption":"Apex Secure Coding featured image: title card with an Apex code example"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Apex Secure Coding: Stop XSS, CSRF and SOQL Injection"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=1600"}],"version-history":[{"count":0,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1600\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/1597"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=1600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=1600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=1600"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=1600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}