Git Product home page Git Product logo

safeurl's Introduction

safeurl

A Go library created to help developers protect their applications from Server Side Request Forgery (SSRF) attacks. It implements a safeurl.Client wrapper around Go's native net/http.Client and performs validation on the incoming request against the configured allow and block lists. It also implements mitigation for DNS rebinding attacks.

Configuration options

The safeurl.Client can be configured through the safeurl.Config struct. It enables configuration of the following options:

AllowedPorts                    - list of ports the application is allowed to connect to
AllowedSchemes                  - list of schemas the application can use
AllowedHosts                    - list of hosts the application is allowed to communicate with
BlockedIPs                      - list of IP addresses the application is not allowed to connect to
AllowedIPs                      - list of IP addresses the application is allowed to connect to
AllowedCIDR                     - list of CIDR ranges the application is allowed to connect to
BlockedCIDR                     - list of CIDR ranges the application is not allowed to connect to

IsIPv6Enabled                   - specifies wether communication through IPv6 is enabled
AllowSendingCredentials         - specifies wether HTTP credentials should be sent

IsDebugLoggingEnabled          - enables debug logs

How to use the safeurl.Client?

First, you need to include the safeurl module. To do that, simply add github.com/doyensec/safeurl to your project's go.mod file.

Sample:

import (
    "fmt"
    "github.com/doyensec/safeurl"
)

func main() {
    config := safeurl.GetConfigBuilder().
        SetAllowedHosts("example.com").
        Build()

    client := safeurl.Client(config)

    resp, err := client.Get("https://example.com")
    if err != nil {
        fmt.Errorf("request return error: %v", err)
    }

    // read response body
}

Running tests

To successfully run all the unit tests, you will need to run a local DNS and HTTP server. That can be done by executing the following command:

go run testing/servers.go

Once the servers are up and running, the unit test can be ran with:

go test -v

Credits

This tool has been created by Viktor Chuchurski and Alessandro Cotto of Doyensec LLC during our 25% research time.

Doyensec Research

safeurl's People

Contributors

carstein avatar ikkisoft avatar v1ktor0t avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

safeurl's Issues

Issue with processing http://127.1 URL

A Tweeter user mentioned that, when running the CTF challenge locally, requests such as:

curl 'http://127.0.0.1/webhook?url=http://127.1' 

return the following error message:

dial tcp: lookup 127.1: no such host

It looks like the URL http://127.1 was interpreted as a hostname instead of an IP address and Go tried to resolve it.

To easily debug the issue, it would be helpful to have:

  • the code of the web server for the example, if it was changed from the one on the blog
  • Go and OS version used to run the web server
  • debug output of the library. This can be enabled by adding EnableDebugLogging(true) to the config

Additionally, does this behavor happen with a payload like http://127.0.0.1 or any other variant of the loopback IP is used, or is it just http://127.1 causing it?

Make an universal solution (not only for golang)

Hi!

More a question/proposition than an issue:

Like it has been said by a Reddit user on a post sharing the library (I can’t retrieve the link sorry), it could have been a great thing to have a solution not « language dependent ».

One idea to do that is to set up a proxy (with the logic of safeurl) and then force the client to use the proxy to perform requests. AFAIK it is possible to do it with many languages (go, python, etc)

So do you see (or have seen) any drawback or limitation with this approach ?

Panic when processing malformed URL

When trying to connect to http://[], the library panics.

Panic stack trace:

github.com/doyensec/safeurl.buildRunFunc.func1({0x1400048aa34, 0x4}, {0x1400048aa30, 0x3}, {0x140001c90e8?, 0x102d8d36c?})
        /Users/ne/go/src/github.com/doyensec/SafeURL/client.go:50 +0x47c
net.(*netFD).dial(0x14000472500, {0x10321e2d0, 0x140001ac008}, {0x10321f160, 0x0}, {0x10321f160?, 0x140004a8c60}, 0x1400048f6a0)
        /opt/homebrew/Cellar/go/1.19.1/libexec/src/net/sock_posix.go:128 +0x248
net.socket({0x10321e2d0, 0x140001ac008}, {0x10309f970, 0x3}, 0x2, 0x1, 0x1030a0227?, 0x0?, {0x10321f160?, 0x0}, ...)
        /opt/homebrew/Cellar/go/1.19.1/libexec/src/net/sock_posix.go:70 +0x210
net.internetSocket({0x10321e2d0, 0x140001ac008}, {0x10309f970, 0x3}, {0x10321f160, 0x0}, {0x10321f160, 0x140004a8c60}, 0x0?, 0x0?, ...)
        /opt/homebrew/Cellar/go/1.19.1/libexec/src/net/ipsock_posix.go:142 +0xa8
net.(*sysDialer).doDialTCP(0x1400022a7e0, {0x10321e2d0, 0x140001ac008}, 0x0, 0x0?)
        /opt/homebrew/Cellar/go/1.19.1/libexec/src/net/tcpsock_posix.go:68 +0x78
net.(*sysDialer).dialTCP(0x1400022a7e0?, {0x10321e2d0?, 0x140001ac008?}, 0x0?, 0x140004a8c60?)
        /opt/homebrew/Cellar/go/1.19.1/libexec/src/net/tcpsock_posix.go:64 +0x70
net.(*sysDialer).dialSingle(0x1400022a7e0, {0x10321e2d0, 0x140001ac008}, {0x10321cb90?, 0x140004a8c60})
        /opt/homebrew/Cellar/go/1.19.1/libexec/src/net/dial.go:582 +0x1ac
net.(*sysDialer).dialSerial(0x1400022a7e0, {0x10321e2d0, 0x140001ac008}, {0x1400048f8b0?, 0x1, 0x10321e2d0?})
        /opt/homebrew/Cellar/go/1.19.1/libexec/src/net/dial.go:550 +0x250
net.(*sysDialer).dialParallel(0x1400048f8a0?, {0x10321e2d0?, 0x140001ac008?}, {0x1400048f8b0?, 0x4?, 0x10309f970?}, {0x0?, 0x1400048aa18?, 0x3?})
        /opt/homebrew/Cellar/go/1.19.1/libexec/src/net/dial.go:451 +0x2f0
net.(*Dialer).DialContext(0x140001aa6c0, {0x10321e2d0, 0x140001ac008}, {0x10309f970, 0x3}, {0x1400048aa18, 0x3})
        /opt/homebrew/Cellar/go/1.19.1/libexec/src/net/dial.go:428 +0x5a0
net/http.(*Transport).dial(0x2df676d800062a68?, {0x10321e2d0?, 0x140001ac008?}, {0x10309f970?, 0x14000062b58?}, {0x1400048aa18?, 0x140001a8bd8?})
        /opt/homebrew/Cellar/go/1.19.1/libexec/src/net/http/transport.go:1170 +0xdc
net/http.(*Transport).dialConn(0x1400047fcc0, {0x10321e2d0, 0x140001ac008}, {{}, 0x0, {0x1400047aca1, 0x4}, {0x1400048aa18, 0x3}, 0x0})
        /opt/homebrew/Cellar/go/1.19.1/libexec/src/net/http/transport.go:1608 +0x61c
net/http.(*Transport).dialConnFor(0x0?, 0x140001deb00)
        /opt/homebrew/Cellar/go/1.19.1/libexec/src/net/http/transport.go:1450 +0x80
created by net/http.(*Transport).queueForDial
        /opt/homebrew/Cellar/go/1.19.1/libexec/src/net/http/transport.go:1419 +0x398

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.