Skip to content

Commit

Permalink
fix: security fixes (#25)
Browse files Browse the repository at this point in the history
* fix: security fixes

* docs: fix readme badge

* chore: fix dependencies

* docs: updated open source license list

* test: added more test
  • Loading branch information
karl-cardenas-coding authored Aug 18, 2024
1 parent 576e907 commit 6765d99
Show file tree
Hide file tree
Showing 14 changed files with 364 additions and 193 deletions.
82 changes: 82 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: "CodeQL"

on:
push:
branches: [ "main", "default" ]
pull_request:
branches: [ "main", "default" ]
schedule:
- cron: '18 5 * * 6'

jobs:
analyze:
name: Analyze (${{ matrix.language }})
# Runner size impacts CodeQL analysis time. To learn more, please see:
# - https://gh.io/recommended-hardware-resources-for-running-codeql
# - https://gh.io/supported-runners-and-hardware-resources
# - https://gh.io/using-larger-runners (GitHub.com only)
# Consider using larger runners or machines with greater resources for possible analysis time improvements.
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
permissions:
# required for all workflows
security-events: write

# required to fetch internal or private CodeQL packs
packages: read

# only required for workflows in private repositories
actions: read
contents: read

strategy:
fail-fast: false
matrix:
include:
- language: go
build-mode: autobuild
# CodeQL supports the following values keywords for 'language': 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift'
# Use `c-cpp` to analyze code written in C, C++ or both
# Use 'java-kotlin' to analyze code written in Java, Kotlin or both
# Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
# To learn more about changing the languages that are analyzed or customizing the build mode for your analysis,
# see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning.
# If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
steps:
- name: Checkout repository
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality

# If the analyze step fails for one of the languages you are analyzing with
# "We were unable to automatically build your code", modify the matrix above
# to set the build mode to "manual" for that language. Then modify this step
# to build your code.
# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
- if: matrix.build-mode == 'manual'
shell: bash
run: |
echo 'If you are using a "manual" build mode for one or more of the' \
'languages you are analyzing, replace this with the commands to build' \
'your code, for example:'
echo ' make bootstrap'
echo ' make release'
exit 1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @karl-cardenas-coding
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) karl-cardenas-coding
# SPDX-License-Identifier: Apache-2.0

FROM golang:1.22.5-alpine3.20 as builder
FROM golang:1.23.0-alpine3.20 as builder

Check warning on line 4 in Dockerfile

View workflow job for this annotation

GitHub Actions / Docker Image Build and Push

The 'as' keyword should match the case of the 'from' keyword

FromAsCasing: 'as' and 'FROM' keywords' casing do not match More info: https://docs.docker.com/go/dockerfile/rule/from-as-casing/

LABEL org.opencontainers.image.source="https://github.com/karl-cardenas-coding/mywhoop"
LABEL org.opencontainers.image.description="A tool for gathering and retaining your own Whoop data."
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Go version](https://img.shields.io/github/go-mod/go-version/karl-cardenas-coding/go-lambda-cleanup)](https://golang.org/dl/)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/karl-cardenas-coding/mywhoop)
[![Go Reference](https://pkg.go.dev/badge/github.com/karl-cardenas-coding/mywhoop.svg)](https://pkg.go.dev/github.com/karl-cardenas-coding/mywhoop)
[![Go Report Card](https://goreportcard.com/badge/github.com/karl-cardenas-coding/mywhoop)](https://goreportcard.com/report/github.com/karl-cardenas-coding/mywhoop)
[![codecov](https://codecov.io/github/karl-cardenas-coding/mywhoop/graph/badge.svg?token=ZHEE3T4HUG)](https://codecov.io/github/karl-cardenas-coding/mywhoop)
Expand Down
28 changes: 23 additions & 5 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,14 @@ func login() error {
TokenURL: internal.DEFAULT_ACCESS_TOKEN_URL,
},
}

state, err := internal.GenerateStateOauthCookie()
if err != nil {
return err
}

slog.Debug("Redirect Config", "URL:", "http://localhost:"+port+redirectURL)
authUrl := internal.GetAuthURL(*config)
authUrl := internal.GetAuthURL(*config, state)

if authUrl == "" {
return errors.New("unable to get authentication URL. Please check the client ID and client secret are correct")
Expand All @@ -113,7 +119,7 @@ func login() error {
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/", landingPageHandler(GlobalStaticAssets, "web/index.html", authUrl))
http.HandleFunc("/close", closeHandler)
http.HandleFunc("/redirect", redirectHandler(GlobalStaticAssets, "web/redirect.html", "web/error.html", config, cliCfg.Credentials.CredentialsFile))
http.HandleFunc("/redirect", redirectHandler(GlobalStaticAssets, "web/redirect.html", "web/error.html", config, state, cliCfg.Credentials.CredentialsFile))

slog.Info("Listening on port 8080. Visit http://localhost:8080 to autenticate with the Whoop API and get an access token.")
err = openBrowser("http://localhost:"+port, noAutoOpenBrowser)
Expand Down Expand Up @@ -152,14 +158,26 @@ func landingPageHandler(assets fs.FS, indexFile string, authUrl string) http.Han

// redirectHandler handles the redirect URL after authenticating with the Whoop API
// and writes the access token to a file
func redirectHandler(assets fs.FS, page, errorPage string, authConf *oauth2.Config, credentialsFilePath string) http.HandlerFunc {
func redirectHandler(assets fs.FS, page, errorPage string, authConf *oauth2.Config, stateIdentifier string, credentialsFilePath string) http.HandlerFunc {

return func(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
slog.Debug("Code received", "code", code)
// slog.Debug("Code received", "code", code)

state := r.URL.Query().Get("state")

if state != stateIdentifier {
slog.Error("State does not match the expected stateIdentifier", "state received:", state)
err := sendErrorTemplate(w, "The unique authentication state identifier does not match the provided value from MyWhoop. You may be subject to a man-in-the-middle (MITM) attack.", http.StatusBadRequest, errorPage, assets)
if err != nil {
slog.Error("unable to send error template", "error", err)
}
return
}

// slog.Debug("State received", "state", state)

if code == "" {
// slog.Info("no code received.", "Error response status: ", r.Response.StatusCode)
err := sendErrorTemplate(w, "No authorization code returned by the Whoop authorization server.", http.StatusInternalServerError, errorPage, assets)
if err != nil {
slog.Error("unable to send error template", "error", err)
Expand Down
6 changes: 3 additions & 3 deletions docs/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ The following steps will guide you through the process of setting up MyWhoop on
7. Download the MyWhoop Docker image.

```shell
docker pull ghcr.io/karl-cardenas-coding/mywhoop:v0.1.1
docker pull ghcr.io/karl-cardenas-coding/mywhoop:v0.1.2
```

8. Create a directory where you want to store the MyWhoop data and credentials token. The command below creates a folder in your home directory and changes to that directory.
Expand All @@ -83,7 +83,7 @@ The following steps will guide you through the process of setting up MyWhoop on
--volume $PWD:/data \
-e WHOOP_CLIENT_ID=$WHOOP_CLIENT_ID \
-e WHOOP_CLIENT_SECRET=$WHOOP_CLIENT_SECRET \
ghcr.io/karl-cardenas-coding/mywhoop:v0.1.1 login \
ghcr.io/karl-cardenas-coding/mywhoop:v0.1.2 login \
--no-auto-open \
--credentials /app/token.json
```
Expand Down Expand Up @@ -112,7 +112,7 @@ The following steps will guide you through the process of setting up MyWhoop on
docker run --publish 8080:8080 --volume $PWD:/app \
-e WHOOP_CLIENT_ID=$WHOOP_CLIENT_ID \
-e WHOOP_CLIENT_SECRET=$WHOOP_CLIENT_SECRET \
ghcr.io/karl-cardenas-coding/mywhoop:v0.1.1 dump \
ghcr.io/karl-cardenas-coding/mywhoop:v0.1.2 dump \
--credentials /app/token.json \
--location /app
```
Expand Down
Loading

0 comments on commit 6765d99

Please sign in to comment.