Back to Content

Content-Security-Policy: connect-src directive

files/en-us/web/http/reference/headers/content-security-policy/connect-src/index.md

latest2.9 KB
Original Source

The HTTP {{HTTPHeader("Content-Security-Policy")}} (CSP) connect-src directive restricts the URLs which can be loaded using script interfaces. The following APIs are controlled by this directive:

  • The ping attribute in {{htmlelement("a")}} elements
  • {{domxref("Window/fetch", "fetch()")}}
  • {{domxref("Window/fetchLater", "fetchLater()")}} {{experimental_inline}}
  • {{domxref("XMLHttpRequest")}}
  • {{domxref("WebSocket")}}
  • {{domxref("EventSource")}}
  • {{domxref("Navigator.sendBeacon()")}}

[!NOTE] connect-src 'self' does not resolve to websocket schemes in all browsers, more info in this issue.

<table class="properties"> <tbody> <tr> <th scope="row">CSP version</th> <td>1</td> </tr> <tr> <th scope="row">Directive type</th> <td>{{Glossary("Fetch directive")}}</td> </tr> <tr> <th scope="row">{{CSP("default-src")}} fallback</th> <td> Yes. If this directive is absent, the user agent will look for the <code>default-src</code> directive. </td> </tr> </tbody> </table>

Syntax

http
Content-Security-Policy: connect-src 'none';
Content-Security-Policy: connect-src <source-expression-list>;

This directive may have one of the following values:

  • 'none'
    • : No resources of this type may be loaded. The single quotes are mandatory.
  • <source-expression-list>
    • : A space-separated list of source expression values. Resources of this type may be loaded if they match any of the given source expressions. For this directive, the following source expression values are applicable:

Examples

Violation cases

Given this CSP header:

http
Content-Security-Policy: connect-src https://example.com/

The following connections are blocked and won't load:

html
<a ping="https://not-example.com" href="/">Link</a>
<script>
  const response = fetch("https://not-example.com/");

  const xhr = new XMLHttpRequest();
  xhr.open("GET", "https://not-example.com/");
  xhr.send();

  const ws = new WebSocket("wss://not-example.com/");

  const es = new EventSource("https://not-example.com/");

  navigator.sendBeacon("https://not-example.com/", {
    /* … */
  });
</script>

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • {{HTTPHeader("Content-Security-Policy")}}
  • {{HTMLElement("a")}} ping
  • {{domxref("Window/fetch", "fetch()")}}
  • {{domxref("XMLHttpRequest")}}
  • {{domxref("WebSocket")}}
  • {{domxref("EventSource")}}