Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: block tiny url domain while creating tiny url #141

Merged
merged 5 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/core-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
JwtIssuer string
AllowedOrigin string
Port string
WebAppBaseUrl string
)

func findAndLoadEnv(envFile string) error {
Expand Down Expand Up @@ -108,6 +109,8 @@ func loadConfig() {

Port = getEnvVar("PORT")
JwtValidity = getEnvInt("JWT_VALIDITY_IN_HOURS")

WebAppBaseUrl = getEnvVar("WEB_APP_BASE_URL")
}

func getEnvVar(key string) string {
Expand Down
10 changes: 8 additions & 2 deletions controllers/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,20 @@ func CreateTinyURL(ctx *gin.Context, db *bun.DB) {
return
}

if strings.Contains(strings.ToLower(parsedUrl.Host),strings.ToLower(config.Domain)) {
parseConfig,err := url.Parse(config.WebAppBaseUrl);

if err != nil {
return;
}

if strings.Contains(strings.ToLower(parsedUrl.Host),strings.ToLower(parseConfig.Host)) {

path :=parsedUrl.Path

if len(path) >=1 {

ctx.JSON(http.StatusForbidden, dtos.URLCreationResponse{
Message: "Cannot create a tiny url for " + config.Domain,
Message: "Cannot create a tiny url for " + config.WebAppBaseUrl,
})

return
Expand Down
2 changes: 2 additions & 0 deletions environments/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ JWT_ISSUER="tiny-site-backend"
DOMAIN="localhost"
AUTH_REDIRECT_URL="http://localhost:3000"

WEB_APP_BASE_URL='http://localhost:3000'

DB_URL="postgresql://postgres:postgres@localhost:5432/tiny-site?sslmode=disable"
TEST_DB_URL="postgresql://postgres:postgres@localhost:5432/tiny-site-test?sslmode=disable"
DB_MAX_OPEN_CONNECTIONS=10
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (suite *AppTestSuite) TestForbidTinyURLChaining() {
})

requestBody := map[string]interface{}{
"OriginalUrl": "https://localhost:8000/abcde",
"OriginalUrl": "https://localhost:3000/abcde",
"UserId": 1,
}

Expand Down
Loading