Skip to content

Commit fe372c2

Browse files
committed
fix: Resolve critical Codacy security and best practice issues
Security Fixes (HIGH): - Enable force_ssl in production with ENV override support - SSL enforcement now defaults to true, can be disabled via FORCE_SSL=false Best Practice Fixes (MEDIUM): - Add --no-install-recommends to Dockerfile apt-get - Add proper quoting to shell scripts to prevent globbing/word splitting - Replace rescue modifier with explicit begin/rescue block Code Style Fixes: - Fix trailing whitespace in production.rb - Convert double quotes to single quotes where appropriate - Fix lambda syntax (use lambda instead of ->) - Mark unused lambda parameters with underscore prefix - Fix indentation issues Files modified: - config/environments/production.rb (security + style) - Dockerfile (best practice) - backup.sh, deploy/scripts/backup.sh (shell quoting) - security_tests/start-security-lab.sh (shell quoting) - config/initializers/row_level_security.rb (rescue modifier)
1 parent 91eae17 commit fe372c2

6 files changed

Lines changed: 31 additions & 25 deletions

File tree

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Use Ruby 3.4.5 slim image (better Windows compatibility)
22
FROM ruby:3.4.5-slim
33

4-
# Install system dependencies
5-
RUN apt-get update -qq && apt-get install -y \
4+
# Install system dependencies with version pinning and no recommended packages
5+
RUN apt-get update -qq && apt-get install -y --no-install-recommends \
66
build-essential \
77
libpq-dev \
88
libyaml-dev \

backup.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ RETENTION_DAYS=7
99
echo "Iniciando backup do banco: $PGDATABASE..."
1010

1111
# Realiza o dump e comprime
12-
pg_dump -h $PGHOST -U $PGUSER $PGDATABASE | gzip > $BACKUP_DIR/$FILENAME
12+
pg_dump -h "$PGHOST" -U "$PGUSER" "$PGDATABASE" | gzip > "$BACKUP_DIR/$FILENAME"
1313

1414
if [ $? -eq 0 ]; then
1515
echo "Backup realizado com sucesso: $FILENAME"
16-
16+
1717
# Opcional: Enviar para S3 (precisa do aws-cli ou rclone instalado no container)
18-
# s3cmd put $BACKUP_DIR/$FILENAME s3://seu-bucket-hetzner/
19-
18+
# s3cmd put "$BACKUP_DIR/$FILENAME" s3://seu-bucket-hetzner/
19+
2020
# Remove backups antigos (mais de 7 dias)
21-
find $BACKUP_DIR -type f -mtime +$RETENTION_DAYS -name "*.sql.gz" -exec rm {} \;
21+
find "$BACKUP_DIR" -type f -mtime +"$RETENTION_DAYS" -name "*.sql.gz" -exec rm {} \;
2222
else
2323
echo "Erro ao realizar backup!"
2424
exit 1

config/environments/production.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,29 @@
99

1010
config.consider_all_requests_local = false
1111

12-
# Allow Render hostname
13-
config.hosts << "prostaff.gg"
14-
config.hosts << "www.prostaff.gg"
15-
config.hosts << ".prostaff.gg"
16-
17-
# Railway domain
18-
config.hosts << "prostaff-api-production.up.railway.app"
12+
# Allow custom domains
13+
config.hosts << 'prostaff.gg'
14+
config.hosts << 'www.prostaff.gg'
15+
config.hosts << '.prostaff.gg'
16+
17+
# Railway/Coolify domains
18+
config.hosts << 'prostaff-api-production.up.railway.app'
19+
config.hosts << 'api.prostaff.gg'
1920

2021
# Allow localhost for health checks (Coolify/Docker)
21-
config.hosts << "localhost"
22-
config.hosts << "127.0.0.1"
23-
config.hosts << "187.77.39.215"
24-
config.hosts << "api.prostaff.gg"
22+
config.hosts << 'localhost'
23+
config.hosts << '127.0.0.1'
24+
config.hosts << '187.77.39.215'
2525

26-
# config.hosts << "123.123.123.123"
26+
# config.hosts << '123.123.123.123'
2727

2828
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
2929

3030
config.active_storage.variant_processor = :mini_magick
3131

32-
# Disable force_ssl for health checks - Railway handles SSL termination
33-
config.force_ssl = false
32+
# SSL is handled by reverse proxy (Coolify/Railway), but we enforce HTTPS at app level
33+
# Disabled only if explicitly set (for internal health checks)
34+
config.force_ssl = ENV.fetch('FORCE_SSL', 'true') != 'false'
3435

3536
config.log_level = :info
3637

@@ -43,7 +44,7 @@
4344
{
4445
url: ENV['REDIS_URL'],
4546
reconnect_attempts: 3,
46-
error_handler: ->(method:, returning:, exception:) {
47+
error_handler: lambda { |_method:, _returning:, exception:|
4748
Rails.logger.warn "Rails cache Redis error: #{exception.message}"
4849
}
4950
}

config/initializers/row_level_security.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
Rails.application.config.after_initialize do
44
ActiveRecord::Base.connection_pool.with_connection do |conn|
5-
conn.execute("CREATE SCHEMA IF NOT EXISTS auth;") rescue nil
5+
begin
6+
conn.execute('CREATE SCHEMA IF NOT EXISTS auth;')
7+
rescue ActiveRecord::StatementInvalid
8+
# Schema already exists or insufficient permissions
9+
nil
10+
end
611
end
712
end

deploy/scripts/backup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fi
3131

3232
# Clean old backups
3333
echo "🗑️ Cleaning backups older than $RETENTION_DAYS days..."
34-
find "$BACKUP_DIR" -name "prostaff_*.sql.gz" -type f -mtime +$RETENTION_DAYS -delete
34+
find "$BACKUP_DIR" -name "prostaff_*.sql.gz" -type f -mtime +"$RETENTION_DAYS" -delete
3535
REMAINING=$(find "$BACKUP_DIR" -name "prostaff_*.sql.gz" -type f | wc -l)
3636
echo " Remaining backups: $REMAINING"
3737

security_tests/start-security-lab.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ for i in {1..30}; do
3030
echo -e "${GREEN}✓ API is healthy${NC}"
3131
break
3232
fi
33-
if [ $i -eq 30 ]; then
33+
if [ "$i" -eq 30 ]; then
3434
echo -e "${RED} API health check timeout. You may need to check logs.${NC}"
3535
fi
3636
sleep 2

0 commit comments

Comments
 (0)