HTTP rules

HTTP rules are schema-level objects that allow outgoing HTTP requests by destination, method, and path.

A network rule with TYPE = HOST_PORT allows every request to the hosts it names. An HTTP rule allows only the requests that match it: for example, GET and HEAD requests under /simple/ on pypi.org, but no POST, and no other path. You add HTTP rules to an external access integration with the ALLOWED_HTTP_RULES parameter, alongside or instead of network rules.

Where HTTP rules apply

HTTP rules apply to Cortex Code cloud sandboxes. A sandbox that uses an external access integration can send each request that one of the integration’s network rules or HTTP rules allows.

Only Cortex Code cloud sandboxes check the method and path of each request, so only they can apply HTTP rules. Other features that use external access integrations, such as UDFs, stored procedures, Snowpark Container Services, notebooks, and Streamlit apps, allow or block requests by host and port: they ignore an integration’s HTTP rules and use only its network rules. Adding an integration that holds HTTP rules to these features doesn’t cause an error, but its HTTP rules allow nothing there.

Note

Enforcement of HTTP rules in Cortex Code cloud sandboxes is being rolled out by region. Until it reaches your region, an HTTP rule allows nothing in a sandbox, and the integration’s network rules still apply.

How HTTP rules combine with network rules

A request from a sandbox is allowed if any network rule or any HTTP rule in any of the sandbox’s external access integrations allows it:

  • A network rule allows every method and path on the hosts and ports it names.
  • An HTTP rule allows a request only if the request’s destination, transport, method, and path all match one of its entries.

Because either kind of rule can allow a request, an HTTP rule can’t narrow what a network rule for the same host allows. To limit a host to certain methods and paths, name the host in an HTTP rule and in no network rule.

Writing an HTTP rule

An HTTP rule holds one property, its specification: a YAML document that lists destinations and, for each destination, the requests it allows. For example, the following specification allows GET and HEAD requests for Python package indexes and package files:

matches:
  - destination: {type: HOST_PORT, value: pypi.org}
    tls:
      http:
        - {method: 'GET|HEAD', path: '/simple/.*'}
  - destination: {type: HOST_PORT, value: files.pythonhosted.org}
    tls:
      http:
        - {method: 'GET|HEAD', path: '/packages/.*'}

Each entry in matches names one destination:

  • destination names a host, or a host and port, such as pypi.org or 'example.com:8443'. The default port is 443. A host can start with one wildcard label, such as '*.example.com'.
  • tls lists the requests allowed over HTTPS. http lists the requests allowed over plain HTTP, and needs a port in the destination, such as 'deb.debian.org:80'.
  • Each request entry can hold a method pattern and a path pattern. A request is allowed if it matches both patterns of any one entry. An entry without a method matches every method, and an entry without a path matches every path.

Patterns are regular expressions in a restricted subset. Each pattern must match the whole method or path, and matching is case-sensitive. For the full specification format, the pattern syntax, and the limits, see CREATE HTTP RULE.

Creating an HTTP rule

You need the CREATE HTTP RULE privilege on the schema to create an HTTP rule. By default, only the schema owner has this privilege.

To create an HTTP rule, run the CREATE HTTP RULE command. For example, to use a custom role to create an HTTP rule that allows reading one item and creating items in an API:

GRANT USAGE ON DATABASE securitydb TO ROLE network_admin;
GRANT USAGE ON SCHEMA securitydb.myrules TO ROLE network_admin;
GRANT CREATE HTTP RULE ON SCHEMA securitydb.myrules TO ROLE network_admin;
USE ROLE network_admin;

CREATE HTTP RULE securitydb.myrules.items_api
  SPECIFICATION = $$
matches:
  - destination: {type: HOST_PORT, value: example.com}
    tls:
      http:
        - {method: GET, path: '/v1/items/[0-9]+'}
        - {method: POST, path: '/v1/items'}
$$;

When you create or alter an HTTP rule, Snowflake checks that each host that doesn’t contain a wildcard resolves to a public IPv4 address.

Using an HTTP rule in an external access integration

To add an HTTP rule to an external access integration, the role that runs the CREATE or ALTER EXTERNAL ACCESS INTEGRATION statement needs the USAGE privilege on the rule. For example:

GRANT USAGE ON HTTP RULE securitydb.myrules.items_api TO ROLE integration_admin;
USE ROLE integration_admin;

CREATE EXTERNAL ACCESS INTEGRATION items_api_access
  ALLOWED_HTTP_RULES = (securitydb.myrules.items_api)
  ENABLED = TRUE;

An integration can hold network rules, HTTP rules, or both, and at most 32 HTTP rules. Users of a sandbox need the USAGE privilege on the integration, as with any external access integration.

An integration refers to its HTTP rules by name:

  • Altering or replacing an HTTP rule changes what every integration that names it allows. Sandboxes that are already running pick up the change within a few minutes.
  • If an integration names an HTTP rule that was dropped, or that the integration’s owner can no longer use, a sandbox that uses the integration fails to start, and the error names the rule. A running sandbox loses only that rule’s access.

What a refused request looks like

When no rule allows a request, the sandbox refuses it:

  • A request to a host that no rule names fails. Depending on the sandbox, either the host name doesn’t resolve or the request gets an HTTP 403 response.
  • A request to a host that a rule names, but with a method or path that no entry allows, gets an HTTP 403 response.

Identifying HTTP rules in your account

To list the HTTP rules that you have privileges on, run SHOW HTTP RULES. To see a rule’s specification, run DESCRIBE HTTP RULE with a role that owns the rule.

Modifying an HTTP rule

To replace a rule’s specification, run ALTER HTTP RULE … SET SPECIFICATION. The new specification replaces the old one; it isn’t merged with it. You can’t change the name or schema of an HTTP rule.

Limitations

  • HTTP rules have no comment, and don’t support tags, GET_DDL, or UNDROP. A dropped HTTP rule can’t be recovered.
  • There are no Information Schema or Account Usage views for HTTP rules.
  • HTTP rules match the method and path of a request. They don’t match headers, query parameters, or request bodies.

Privileges and commands

CommandPrivilegeDescription
CREATE HTTP RULECREATE HTTP RULE on SCHEMACreates a new HTTP rule.
ALTER HTTP RULEOWNERSHIP on HTTP RULEReplaces the specification of an existing HTTP rule.
DROP HTTP RULEOWNERSHIP on HTTP RULERemoves an existing HTTP rule from the system.
DESCRIBE HTTP RULEOWNERSHIP on HTTP RULEDescribes an existing HTTP rule, including its specification.
SHOW HTTP RULESOWNERSHIP or USAGE on HTTP RULELists the HTTP rules in the system.
CREATE EXTERNAL ACCESS INTEGRATION, ALTER EXTERNAL ACCESS INTEGRATIONUSAGE on HTTP RULEAdds an HTTP rule to an external access integration.