id: ssrf-guard-allowlist
namespace: company.team
description: |
Vet every user-supplied URL before the platform fetches it, whether it
arrives on a webhook from the application or sits in the scheduled URL
inventory: the hostname is resolved and judged against denied IP ranges,
scheme rules, and an allowlist of host suffixes. A URL that fails any rule
is blocked with a Slack alert carrying the reason and the resolved IP, while
a clean URL is fetched with a hard timeout. The inventory sweep counts
blocked entries, and every execution publishes the verdict, blocked count,
resolved IP, and reason as outputs.
triggers:
- id: on_url_submission
type: io.kestra.plugin.core.trigger.Webhook
description: The application POSTs the URL it was asked to fetch plus the tenant
that owns it, so webhooks, import jobs, and avatar fetchers are vetted
before any outbound request leaves the worker.
key: "{{ secret('SSRF_WEBHOOK_KEY') }}"
- id: inventory_scan
type: io.kestra.plugin.core.trigger.Schedule
description: Daily 06:00 UTC sweep of the standing URL inventory as a fallback
net - stays disabled until the webhook path is proven in your environment.
cron: "0 6 * * *"
disabled: true
inputs:
- id: allowlist_domains
type: STRING
defaults: raw.githubusercontent.com, api.github.com, hooks.slack.com
description: Comma-separated hostname suffixes the fetch path permits. A host
matches when it equals a suffix or ends with a dot plus that suffix, so
evilexample.com never matches example.com.
- id: fetch_timeout_seconds
type: INT
defaults: 15
description: Hard timeout for the fetch of an allow-listed URL, rendered into
the HTTP task duration so a hung endpoint cannot stall the guard.
tasks:
# --- 1. SSRF evaluation of the submitted URL ---
- id: guard_workspace
type: io.kestra.plugin.core.flow.WorkingDirectory
description: One shared directory so the rule module written by the first task
is reused by the inventory sweep and every verdict line lands in a single
results file.
tasks:
- id: evaluate_url
type: io.kestra.plugin.scripts.python.Script
description: Write the shared ssrf_rules module, resolve the submitted hostname
with socket.getaddrinfo inside a try/except, and apply the scheme,
denied range, and allowlist rules with the ipaddress module, emitting
safe, reason, resolved_ip, and url through the outputs protocol.
taskRunner:
type: io.kestra.plugin.core.runner.Process
env:
TARGET_URL: "{{ trigger.body.url ?? '' }}"
TENANT: "{{ trigger.body.tenant ?? 'schedule' }}"
ALLOWLIST: "{{ inputs.allowlist_domains }}"
script: |
import json
import os
import sys
RULES = '''
import ipaddress
import socket
from urllib.parse import urlparse
ALLOWED_SCHEMES = ("http", "https")
DENIED_NETWORKS = [
(ipaddress.ip_network("10.0.0.0/8"), "private"),
(ipaddress.ip_network("172.16.0.0/12"), "private"),
(ipaddress.ip_network("192.168.0.0/16"), "private"),
(ipaddress.ip_network("127.0.0.0/8"), "loopback"),
(ipaddress.ip_network("169.254.0.0/16"), "link-local"),
(ipaddress.ip_network("::1/128"), "loopback"),
(ipaddress.ip_network("fc00::/7"), "private"),
]
PROPERTY_RULES = (("loopback", "is_loopback"), ("link-local", "is_link_local"), ("private", "is_private"),
("reserved", "is_reserved"), ("multicast", "is_multicast"), ("unspecified", "is_unspecified"))
def _denied(ip):
for network, label in DENIED_NETWORKS:
if ip in network:
return label + " " + str(network)
for label, attr in PROPERTY_RULES:
if getattr(ip, attr):
return label
return None
def evaluate(raw_url, allowlist_raw):
url = (raw_url or "").strip()
allowlist = [p.strip().lower().lstrip(".") for p in (allowlist_raw or "").split(",") if p.strip()]
if not url:
return {"safe": False, "reason": "no url was submitted", "resolved_ip": "", "url": url}
try:
parsed = urlparse(url)
except ValueError:
return {"safe": False, "reason": "url could not be parsed", "resolved_ip": "", "url": url}
scheme = (parsed.scheme or "").lower()
if scheme not in ALLOWED_SCHEMES:
return {"safe": False, "reason": "only http and https are allowed, got scheme " + (scheme or "missing"), "resolved_ip": "", "url": url}
host = (parsed.hostname or "").lower()
if not host:
return {"safe": False, "reason": "url has no hostname", "resolved_ip": "", "url": url}
try:
infos = socket.getaddrinfo(host, None)
except OSError:
return {"safe": False, "reason": "dns resolution failed for " + host, "resolved_ip": "", "url": url}
addresses = []
for info in infos:
if info[4][0] not in addresses:
addresses.append(info[4][0])
for address in addresses:
label = _denied(ipaddress.ip_address(address))
if label:
return {"safe": False, "reason": "resolved ip " + address + " falls in denied range " + label, "resolved_ip": address, "url": url}
if not any(host == s or host.endswith("." + s) for s in allowlist):
return {"safe": False, "reason": "host " + host + " is not on the allowlist", "resolved_ip": addresses[0], "url": url}
return {"safe": True, "reason": "scheme, dns, range, and allowlist checks passed", "resolved_ip": addresses[0], "url": url}
'''
with open("ssrf_rules.py", "w") as handle:
handle.write(RULES)
sys.path.insert(0, os.getcwd())
import ssrf_rules
verdict = ssrf_rules.evaluate(os.environ.get("TARGET_URL", ""), os.environ.get("ALLOWLIST", ""))
tenant = os.environ.get("TENANT", "unspecified")
print("ssrf %s tenant=%s url=%s reason=%s" % ("ALLOW" if verdict["safe"] else "BLOCK", tenant, verdict["url"], verdict["reason"]))
print("::" + json.dumps({"outputs": verdict}) + "::")
# --- 2. blocked or allowed gate ---
- id: check_ssrf_verdict
type: io.kestra.plugin.core.flow.If
description: "Branch on the verdict: a URL that failed any rule escalates to
Slack, a clean URL is fetched with a hard timeout."
condition: "{{ outputs.evaluate_url.vars.safe == false }}"
then:
- id: alert_blocked_url
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Tell the security channel which URL was blocked, for which tenant,
and why, together with the resolved IP. Skipped when no URL was
submitted, so the schedule never raises an empty-target alert.
runIf: "{{ (trigger.body.url ?? '') != '' }}"
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "SSRF guard blocked a URL for tenant {{ trigger.body.tenant ?? 'unspecified' }} - {{ outputs.evaluate_url.vars.reason }}. Submitted {{ outputs.evaluate_url.vars.url }}, resolved ip {{ outputs.evaluate_url.vars.resolved_ip or 'none' }}. Execution {{ execution.id }}."
}
else:
- id: fetch_allowed_url
type: io.kestra.plugin.core.http.Request
description: Fetch the allow-listed URL with a hard timeout. allowFailed keeps a
404 or 500 as a status to report instead of failing the execution.
uri: "{{ outputs.evaluate_url.vars.url }}"
method: GET
timeout: "PT{{ inputs.fetch_timeout_seconds }}S"
headers:
Accept: "*/*"
User-Agent: ssrf-guard/1.0
options:
allowFailed: true
- id: log_fetch_result
type: io.kestra.plugin.core.log.Log
description: Record the tenant, the URL, and the HTTP status in the execution
history so a clean fetch still leaves evidence.
message: "Fetched {{ outputs.evaluate_url.vars.url }} for tenant {{
trigger.body.tenant ?? 'schedule' }} with status {{
outputs.fetch_allowed_url.code }}."
# --- 3. inventory sweep for the schedule path ---
- id: scan_url_inventory
type: io.kestra.plugin.core.flow.Loop
description: Walk the standing URL inventory one entry at a time with the same
shared rules, the schedule path that keeps known fetch targets under
watch.
concurrencyLimit: 1
values:
- "https://raw.githubusercontent.com/kestra-io/kestra/develop/README.md"
- "https://api.github.com/"
- "http://169.254.169.254/latest/meta-data/"
- "http://127.0.0.1:9200/_cat/indices"
- "ftp://files.example.com/pub/quarterly.csv"
tasks:
- id: check_inventory_entry
type: io.kestra.plugin.scripts.python.Script
description: Evaluate the current inventory URL with the shared rule module and
append the verdict line that publish_verdict later counts.
taskRunner:
type: io.kestra.plugin.core.runner.Process
env:
TARGET_URL: "{{ item.value }}"
ALLOWLIST: "{{ inputs.allowlist_domains }}"
script: |
import json
import os
import sys
sys.path.insert(0, os.getcwd())
import ssrf_rules
result = ssrf_rules.evaluate(os.environ.get("TARGET_URL", ""), os.environ.get("ALLOWLIST", ""))
with open("inventory-verdicts.ndjson", "a") as handle:
handle.write(json.dumps(result) + "\n")
print("inventory entry %s -> %s" % (result["url"], result["reason"]))
print("::" + json.dumps({"outputs": result}) + "::")
# --- 4. final verdict ---
- id: publish_verdict
type: io.kestra.plugin.scripts.python.Script
description: Combine the webhook verdict, the fetch status, and the inventory
verdict lines into the four published outputs of this execution.
taskRunner:
type: io.kestra.plugin.core.runner.Process
env:
SAFE: "{{ outputs.evaluate_url.vars.safe }}"
REASON: "{{ outputs.evaluate_url.vars.reason }}"
RESOLVED_IP: "{{ outputs.evaluate_url.vars.resolved_ip }}"
FETCH_CODE: "{{ outputs.fetch_allowed_url.code ?? '' }}"
script: |
import json
import os
safe = os.environ.get("SAFE", "").strip().lower() in ("true", "1")
reason = os.environ.get("REASON", "")
resolved_ip = os.environ.get("RESOLVED_IP", "")
fetch_code = os.environ.get("FETCH_CODE", "").strip()
blocked_count = 0
scanned_count = 0
if os.path.exists("inventory-verdicts.ndjson"):
with open("inventory-verdicts.ndjson") as handle:
for line in handle:
line = line.strip()
if not line:
continue
try:
entry = json.loads(line)
except ValueError:
continue
scanned_count += 1
if not entry.get("safe", False):
blocked_count += 1
fetch_status = int(fetch_code) if fetch_code.isdigit() else 0
if not safe:
verdict = "blocked"
elif fetch_status and 200 <= fetch_status < 400:
verdict = "fetched"
elif fetch_status:
verdict = "fetch_rejected"
else:
verdict = "fetch_error"
payload = {
"verdict": verdict,
"blocked_count": blocked_count,
"resolved_ip": resolved_ip,
"reason": reason,
"fetch_status": fetch_status,
"scanned_count": scanned_count,
}
print("verdict=%s blocked=%d scanned=%d fetch_status=%d" % (verdict, blocked_count, scanned_count, fetch_status))
print("::" + json.dumps({"outputs": payload}) + "::")
errors:
- id: notify_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Escalate any failure to the ops channel with the execution url.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{"text": "FAILED: {{ flow.id }} {{ execution.id }}"}
outputs:
- id: verdict
type: STRING
description: Final verdict for the submitted URL, one of blocked, fetched,
fetch_rejected, or fetch_error.
value: "{{ outputs.publish_verdict.vars.verdict }}"
- id: blocked_count
type: INT
description: How many inventory entries the sweep denied, for example 3 of 5.
value: "{{ outputs.publish_verdict.vars.blocked_count }}"
- id: resolved_ip
type: STRING
description: IP the submitted hostname resolved to, empty when the URL was
denied before DNS.
value: "{{ outputs.publish_verdict.vars.resolved_ip }}"
- id: reason
type: STRING
description: 'Human readable reason behind the verdict, e.g. "host x is not on
the allowlist".'
value: "{{ outputs.publish_verdict.vars.reason }}"