Formjacking: a script that reads the form

Formjacking is an attack in which a malicious script on a page, most often a checkout, reads what visitors type into a form and sends a copy to an address the attacker controls. It is also called web skimming or e-skimming, and the best-known family of campaigns goes by the name Magecart. The copying happens in the visitor's browser, which is why neither your server logs nor a filter in front of the server will show it.

Section Web attacks Updated Standards PCI DSS 6.4.3, 11.6.1

Quick facts

Also called
Web skimming, e-skimming, Magecart-style attacks
Where it runs
In the visitor's browser, as script on a page with a form
Typical target
Payment pages; any form with credentials or personal data
OWASP category
A03:2025 Software Supply Chain Failures when a third-party script is the way in
Main fix
Know every script on the payment page, restrict where scripts load from and where they can send data, and alert on change

The PCI Security Standards Council describes the problem in its information supplement on payment page security and preventing e-skimming. The browser mechanisms below are documented by MDN, and OWASP's Content Security Policy Cheat Sheet covers the policy side.

What formjacking is

A web page can run any script it includes, and a script on the page can read anything the page can: field values, the cookies not marked HttpOnly, the address bar. Formjacking uses that. The planted script waits for the visitor to type, collects the field values, often just before the form is submitted, and sends them to a server the attacker runs. The order still goes through, the visitor gets the confirmation page, and nobody notices for weeks.

Two things make it hard to see. The theft needs no request to your server beyond the normal ones, and the script usually sits among dozens of legitimate ones for analytics, chat, A/B testing and payments. A page with forty script tags makes one extra tag easy to miss.

How it differs from cross-site scripting

Formjacking and cross-site scripting end in the same place, attacker code running with the page's privileges. The route differs. XSS injects code through a flaw in how your application handles input, often one visitor at a time. Formjacking more often changes a script file that every visitor already loads, either on your server or at a vendor, so it reaches everyone and leaves no trace in request parameters. DOM-based XSS is another way to get script onto a page, and it can end in the same theft.

How the script gets onto the page

There are two broad routes.

Your own site is compromised

An attacker who gets write access to the application, the content management system, a plugin or the build pipeline can edit a script the site serves or add a new one. The way in is usually an unpatched component, a stolen admin password or a vulnerable plugin. This is the route where a WAF can matter, because exploitation of known flaws in your own application arrives as requests.

A third-party script is compromised

Most pages load code from other organisations. If one of those suppliers is breached, or a domain it used lapses and is re-registered by someone else, the script your page loads changes without any change on your side. OWASP groups this under software supply chain failures. Your own server never sees the modification.

Both routes meet at the visitor's browser, and that is where the useful defences sit.

Content Security Policy

A Content-Security-Policy response header tells the browser what the page may load and contact. Two directives matter most here, and a third closes a gap.

  1. script-src. Lists the origins scripts may load from, and with nonces or hashes, which inline scripts may run. A script from an unlisted origin is refused. MDN's CSP implementation guide recommends a strict policy built on nonces or hashes.
  2. connect-src. Restricts the URLs a script can reach through fetch, XMLHttpRequest, WebSocket, EventSource and sendBeacon, per the MDN reference. A skimmer that cannot contact its collection server has nothing to do with what it read.
  3. form-action. Limits where a form may be submitted, as OWASP notes in its cheat sheet. It stops a tampered form from posting card data to another host, though not a script that sends the data by other means.

A policy has limits. If the attacker's code comes from an origin you have already allowed, such as a tag manager or a shared CDN host, the policy lets it through. A wide connect-src that allows many analytics hosts gives a skimmer many places to send data. OWASP's guidance is to treat CSP as a second layer, not the only defence.

Start with the Content-Security-Policy-Report-Only header. The browser reports what the policy would have blocked and blocks nothing, so you can find the scripts you forgot about before you enforce. Reports go to the endpoint named in report-to, or report-uri in browsers that need it. The full list of headers worth sending is on the security headers page.

Subresource Integrity

With Subresource Integrity you put a hash of the expected file in the integrity attribute of a script or stylesheet tag. The browser hashes what it downloads and refuses to run it if the hash differs. MDN lists sha256, sha384 and sha512 as the allowed algorithms. For a file from another origin the request must use CORS, so the tag also needs crossorigin="anonymous" and the server must permit it.

This answers the compromised supplier directly: the changed file no longer matches. The limit is the same fact seen from the other side. A hash matches exact bytes, so SRI suits files you can pin to a version. It does not work for a vendor script that changes at a fixed address by design, which is common for tag managers and payment widgets, and in that case the options are to host a pinned copy or to rely on the other controls. SRI says nothing about scripts you wrote or serve yourself if the attacker edits both file and hash.

MDN also describes an Integrity-Policy header that requires integrity metadata on script loads, with a report-only variant for trying it first.

Script inventory and monitoring

Neither header helps if nobody knows what the page is supposed to load. An inventory is a list: each script on the payment page, where it comes from, who owns it and why it is there. Review it when pages change, and remove anything without an owner.

Two habits make the list useful. Keep the payment page lean, with analytics and marketing tags left off the page where card data is typed, or moved off it. And compare what the page actually serves against the list on a schedule, since a script can change without a deploy on your side. CSP violation reports give part of that picture for free once reporting is on.

If card entry happens in an iframe hosted by the payment provider, the fields are not part of your page's document and your scripts cannot read them. That narrows the exposure; it does not remove the need to protect the surrounding page.

PCI DSS 6.4.3 and 11.6.1

PCI DSS v4.0 added two requirements aimed at this attack. They became mandatory on 31 March 2025, after a transition period as best practice. The PCI SSC describes their goal as making sure payment page scripts are authorised, checked for integrity and monitored for tampering.

  • 6.4.3 concerns scripts loaded and executed in the consumer's browser on payment pages: a method to confirm each script is authorised, a method to assure its integrity, and an inventory with a written justification for each script.
  • 11.6.1 concerns detection: a change-and-tamper-detection mechanism that alerts personnel to unauthorised modification of security-impacting HTTP headers and of script contents on payment pages, as received by the consumer browser.

Which of these apply to you depends on how card data is collected and which self-assessment questionnaire you complete. The PCI SSC supplement above is guidance, not a new requirement, and your assessor or acquirer decides what evidence they need. Read the requirement text in the current standard rather than a summary.

Where a WAF helps and where it does not

A WAF filters requests arriving at your server. Formjacking happens after the page has left your server: the skimmer runs in the browser and sends data to a host that has nothing to do with you. That request never passes through your filter, so no setting can block it.

What a WAF can do is narrow the first route. Attempts to exploit a known flaw in your application or a plugin, to upload a file or to change content, are requests, and rules can match them. Recording those in the activity log is a legitimate use. It is no protection against a compromised vendor script. Headers also carry the defence against clickjacking, another attack that happens in the browser.

Use the three modes in order. In monitor mode, matches are recorded and every request is still served, so you learn what your normal traffic looks like first, as the monitor mode page explains. Switch a rule to block once the log is quiet. Everything that decides what scripts run in the visitor's browser stays in your page and your headers: CSP, SRI and the inventory.

Questions

What is formjacking?

Formjacking is an attack in which a malicious script running on a web page, usually a checkout or login page, reads what visitors type into a form and sends it to an address the attacker controls. The visitor's browser does the copying, so the site's own server sees nothing unusual.

What is Magecart?

Magecart is a widely used label for web skimming that targets online stores, used for campaigns against online stores. The technique is the same as formjacking: script on the payment page copies card details in the browser.

Does a WAF stop formjacking?

Not on its own. A WAF inspects requests that reach your server, while the stolen data leaves from the visitor's browser straight to another host. A WAF can help with one route in, by filtering attacks on your site that would let someone plant a script. Content Security Policy, Subresource Integrity and a script inventory do the direct work.

How does Content Security Policy limit formjacking?

A policy can list which origins may supply scripts (script-src) and which addresses scripts may contact (connect-src), and can restrict where forms submit (form-action). A skimmer then has a harder time loading, or sending data out. CSP does not help if the attacker's code comes from an origin you already allow.

What do PCI DSS 6.4.3 and 11.6.1 require?

Requirement 6.4.3 covers scripts on payment pages: each one authorized, its integrity checked and an inventory kept with a justification. Requirement 11.6.1 asks for a mechanism that detects and alerts on unauthorized changes to payment page scripts and security-impacting HTTP headers. Read the current standard and the PCI SSC supplement for the exact wording that applies to you.