-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add noscript handler for outbound link
- Loading branch information
Showing
8 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/fiber/v2/utils" | ||
"github.com/prismelabs/analytics/pkg/event" | ||
hutils "github.com/prismelabs/analytics/pkg/handlers/utils" | ||
"github.com/prismelabs/analytics/pkg/services/eventstore" | ||
"github.com/prismelabs/analytics/pkg/services/saltmanager" | ||
"github.com/prismelabs/analytics/pkg/services/sessionstorage" | ||
"github.com/prismelabs/analytics/pkg/uri" | ||
) | ||
|
||
type GetNoscriptEventsOutboundLink fiber.Handler | ||
|
||
// ProvideGetNoscriptEventsOutboundLink is a wire provider for | ||
// GET /api/v1/noscript/events/outbound-link handler. | ||
func ProvideGetNoscriptEventsOutboundLink( | ||
eventStore eventstore.Service, | ||
sessionStorage sessionstorage.Service, | ||
saltManagerService saltmanager.Service, | ||
) GetNoscriptEventsOutboundLink { | ||
return func(c *fiber.Ctx) error { | ||
var err error | ||
outboundLinkClickEv := event.OutboundLinkClick{} | ||
|
||
outboundLinkClickEv.PageUri, err = hutils.PeekAndParseReferrerQueryHeader(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
outboundUri, err := uri.Parse(c.Query("url")) | ||
if err != nil { | ||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("invalid outbound link: %v", err.Error())) | ||
} | ||
|
||
// Check that link is external. | ||
if outboundUri.Host() == outboundLinkClickEv.PageUri.Host() { | ||
return fiber.NewError(fiber.StatusBadRequest, "internal link") | ||
} | ||
|
||
// Compute device id. | ||
deviceId := hutils.ComputeDeviceId( | ||
saltManagerService.StaticSalt().Bytes(), c.Request().Header.UserAgent(), | ||
utils.UnsafeBytes(c.IP()), utils.UnsafeBytes(outboundLinkClickEv.PageUri.Host()), | ||
) | ||
|
||
ctx := c.UserContext() | ||
var ok bool | ||
outboundLinkClickEv.Session, ok = sessionStorage.WaitSession(deviceId, outboundLinkClickEv.PageUri, hutils.ContextTimeout(ctx)) | ||
if !ok { | ||
// Fallback to root of referrer. This is needed if referrer query or header contained entire url | ||
// while referrer pageview event contains only origin because of different referrer policy. | ||
outboundLinkClickEv.Session, ok = sessionStorage.WaitSession(deviceId, outboundLinkClickEv.PageUri.RootUri(), hutils.ContextTimeout(ctx)) | ||
} | ||
if !ok { | ||
return errSessionNotFound | ||
} | ||
|
||
// Store event. | ||
err = eventStore.StoreOutboundLinkClick(ctx, &outboundLinkClickEv) | ||
if err != nil { | ||
return fmt.Errorf("failed to store custom event: %w", err) | ||
} | ||
|
||
return c.Redirect(outboundUri.String(), fiber.StatusFound) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package middlewares | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/prismelabs/analytics/pkg/uri" | ||
) | ||
|
||
type ReferrerAsDefaultOrigin fiber.Handler | ||
|
||
// ProvideReferrerAsDefaultOrigin is a wire provider for a middleware that sets | ||
// request origin to referrer header if undefined. | ||
func ProvideReferrerAsDefaultOrigin() ReferrerAsDefaultOrigin { | ||
return func(c *fiber.Ctx) error { | ||
println(c.Request().String()) | ||
headers := &c.Request().Header | ||
|
||
// Origin is missing. This can happen on cross origin GET requests. | ||
if len(headers.Peek(fiber.HeaderOrigin)) == 0 { | ||
referrer, err := uri.ParseBytes(headers.Peek(fiber.HeaderReferer)) | ||
if err != nil { | ||
return fiber.NewError(fiber.StatusBadRequest, `invalid "Referer"`) | ||
} | ||
|
||
headers.Set(fiber.HeaderOrigin, referrer.Origin()) | ||
} | ||
|
||
return c.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters