Skip to content
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
14 changes: 14 additions & 0 deletions .bundler-audit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Pay remains on v5 until its billing/data migration has separate validation.
# Every exception has an application regression test; do not expand this list
# to silence a new advisory.
ignore:
# Upstream's back-parameter XSS fix is backported in config/initializers/pay.rb.
# test/controllers/pay_payment_redirect_test.rb exercises the mounted payment
# route with script URLs, external URLs, invalid URLs, and a valid local path.
# https://github.com/pay-rails/pay/security/advisories/GHSA-cqf3-vpx7-rxhw
- CVE-2023-30614
# Pay 5 has no PaddleBillingController or /webhooks/paddle_billing route.
# Paddle Billing was introduced in Pay 7. Only Stripe is enabled, and the
# same integration test verifies Paddle/Braintree endpoints are not routed.
# https://github.com/pay-rails/pay/blob/main/UPGRADE.md#pay-60-to-pay-70
- GHSA-mjgf-xj26-9qf9
20 changes: 20 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.git
.github
.agents
.codex
.bundle
.env*
config/master.key
config/credentials/*.key
node_modules
.cache
log/*
!log/.keep
tmp/*
!tmp/.keep
Comment thread
philipithomas marked this conversation as resolved.
Resolved
storage/*
!storage/.keep
public/assets
app/assets/builds/*
!app/assets/builds/.keep
.DS_Store
95 changes: 50 additions & 45 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
name: Tests
on: [push, pull_request]
on:
push:
branches: [main]
pull_request:

permissions:
contents: read

concurrency:
group: tests-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
# Ubuntu 24.04 provides libvips >= 8.13 for patched Active Storage.
runs-on: ubuntu-24.04
timeout-minutes: 20
strategy:
fail-fast: false
matrix:
app-mode: [SOLO, MULTIUSER]
services:
postgres:
image: postgres:10
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
ports: ['5432:5432']
Expand All @@ -15,78 +30,68 @@ jobs:
--health-interval 10s
--health-timeout 5s
--health-retries 5
redis:
image: redis
ports:
- 6379:6379
options: --entrypoint redis-server
env:
RAILS_ENV: test
RAILS_MASTER_KEY: ${{ secrets.RAILS_TEST_KEY }}
APP_MODE: ${{ matrix.app-mode }}
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postcard_test
SECRET_KEY_BASE: test-only-secret-key-base
DISABLE_ANONYMOUS_TELEMETRY: 'true'
AWS_EC2_METADATA_DISABLED: 'true'
GROVER_NO_SANDBOX: 'true'
PUPPETEER_CHROME_HEADLESS_SHELL_SKIP_DOWNLOAD: 'true'
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v7
- name: Install native dependencies
run: |
sudo apt-get update -qq
sudo apt-get install --no-install-recommends -y libpq-dev libvips libvips-tools
- name: Install Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2.2
bundler-cache: true
- name: Install Node
uses: actions/setup-node@v6
uses: actions/setup-node@v7
with:
node-version: 24.20.0
node-version-file: .node-version
cache: npm
- name: Install libvips
env:
DEBIAN_FRONTEND: noninteractive
run: |
# we only need the library
sudo apt-get update
sudo apt-get install --fix-missing -qq -o Acquire::Retries=3 libvips libvips-tools
- name: Setup the Rails application
env:
RAILS_ENV: test
- name: Install JavaScript dependencies
run: npm ci
- name: Install browser libraries
run: sudo env "PUPPETEER_CACHE_DIR=$HOME/.cache/puppeteer" npx puppeteer browsers install chrome --install-deps
- name: Prepare database and assets
run: |
sudo apt-get -yqq install libpq-dev
bundle install --no-deployment --jobs 4 --retry 3
cp config/database.github.yml config/database.yml
bundle exec rails db:create
bundle exec rails db:migrate
npm ci
bundle exec rake assets:precompile
bundle exec rails db:create db:migrate
bundle exec rails assets:precompile
- name: Check Zeitwerk autoloading
run: bundle exec rails zeitwerk:check
- name: Run unit tests (SOLO)
env:
APP_MODE: SOLO
COVERAGE: '1'
run: bin/test
- name: Run unit tests (MULTIUSER)
- name: Run all Rails tests
env:
APP_MODE: MULTIUSER
COVERAGE: '1'
run: bin/test
- name: Upload coverage reports
if: always()
uses: actions/upload-artifact@v7
with:
name: rails-coverage
path: coverage/
name: rails-coverage-${{ matrix.app-mode }}
path: coverage/${{ matrix.app-mode }}/
retention-days: 14
if-no-files-found: warn
- name: Run browser editor regressions
env:
GROVER_NO_SANDBOX: 'true'
- name: Verify browser image generation
run: npm test
- name: Run brakeman
- name: Run Brakeman
run: bundle exec brakeman
- name: Audit Ruby dependencies
run: bundle exec bundler-audit check --update

docker-smoke-test:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
timeout-minutes: 30
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v7
- name: Build Docker image
run: docker build -t postcard:test .
- name: Exercise production Docker boot, homepage, and cache
timeout-minutes: 10
run: bash script/docker-smoke-test postcard:test
- name: Verify the image includes a working browser
run: docker run --rm --entrypoint npm postcard:test test
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
24.20.0
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.2
3.4.10
3 changes: 2 additions & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ruby 3.2.2
ruby 3.4.10
nodejs 24.20.0
136 changes: 42 additions & 94 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,118 +1,66 @@
# syntax = docker/dockerfile:1
# syntax=docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.2
# Keep these versions aligned with .ruby-version and .node-version.
ARG RUBY_VERSION=3.4.10
ARG NODE_VERSION=24.20.0
FROM --platform=linux/amd64 node:${NODE_VERSION}-bookworm-slim AS node
FROM --platform=linux/amd64 ruby:${RUBY_VERSION}-slim-bookworm AS base

# Bookworm provides libvips >= 8.13, required by the Active Storage security patch.
FROM --platform=linux/amd64 ruby:$RUBY_VERSION-slim-bookworm AS base

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_WITHOUT="development:test" \
BUNDLE_DEPLOYMENT="1" \
RAILS_SERVE_STATIC_FILES="true" \
DEBIAN_FRONTEND="noninteractive" \
GROVER_NO_SANDBOX="true"


# Update gems and bundler
# Pin RubyGems to 3.5.x because tailwindcss-rails 2.0.25 uses Gem::Platform.match,
# which was removed in RubyGems 3.6+
RUN gem update --system 3.5.22 --no-document && \
gem install -N bundler

BUNDLE_WITHOUT="development:test:preview" \
BUNDLE_DEPLOYMENT="1" \
RAILS_SERVE_STATIC_FILES="true" \
RAILS_LOG_TO_STDOUT="1" \
GROVER_NO_SANDBOX="true" \
PUPPETEER_CHROME_HEADLESS_SHELL_SKIP_DOWNLOAD="true" \
PUPPETEER_CACHE_DIR="/rails/.cache/puppeteer"

# Node's official binary avoids compiling Node during every image build.
COPY --from=node /usr/local/bin/node /usr/local/bin/node
COPY --from=node /usr/local/lib/node_modules /usr/local/lib/node_modules
RUN ln -s ../lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm && \
ln -s ../lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx

# libvips >= 8.13 is required by the Active Storage security patch.
# Chromium is downloaded by the locked Puppeteer package; only its libraries
# are installed here, so no separate Chrome repository or browser is needed.
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y \
ca-certificates curl imagemagick libvips postgresql-client \
fonts-liberation fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg \
fonts-kacst fonts-freefont-ttf libasound2 libatk-bridge2.0-0 libatk1.0-0 \
libcups2 libdbus-1-3 libdrm2 libgbm1 libgtk-3-0 libnspr4 libnss3 \
libx11-xcb1 libxcomposite1 libxdamage1 libxfixes3 libxkbcommon0 \
libxrandr2 libxshmfence1 libxss1 xdg-utils && \
rm -rf /var/lib/apt/lists/*

# Throw-away build stage to reduce size of final image
FROM base AS build

# Install packages needed to build gems and node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential curl libpq-dev libvips node-gyp pkg-config python-is-python3 dbus dbus-x11 && \
service dbus start

# Install JavaScript dependencies
ARG NODE_VERSION=18.12.1
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
/tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
rm -rf /tmp/node-build-master
apt-get install --no-install-recommends -y build-essential libpq-dev pkg-config unzip && \
rm -rf /var/lib/apt/lists/*

# Install application gems
COPY --link Gemfile Gemfile.lock ./
RUN bundle install && \
rm -rf ~/.bundle/ $BUNDLE_PATH/ruby/*/cache $BUNDLE_PATH/ruby/*/bundler/gems/*/.git
rm -rf /usr/local/bundle/cache /usr/local/bundle/ruby/*/cache

# Install node modules
COPY --link package.json ./
RUN npm install
COPY --link package.json package-lock.json ./
RUN npm ci --omit=dev && \
npx puppeteer browsers install chrome && \
npm cache clean --force

# Copy application code
COPY --link . .

# Precompiling assets using build environment (production-like but without AWS dependencies)
RUN SECRET_KEY_BASE=DUMMY RAILS_ENV=build ./bin/rails assets:clobber
RUN SECRET_KEY_BASE=DUMMY RAILS_ENV=build ./bin/rails assets:precompile

# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl imagemagick libvips postgresql-client && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Puppeteer deps
RUN apt-get update \
&& apt-get install -y wget gnupg \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install -y fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon-x11-0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 libpango-1.0-0 libcairo2 libasound2 libnss3 libgtk-3-0 xvfb x11-apps x11-xkb-utils libx11-6 libx11-xcb1 \
libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libatk1.0-0 libc6 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libnspr4 libpangocairo-1.0-0 libstdc++6 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget \
--no-install-recommends

# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Copy Node.js and node_modules from build stage
COPY --from=build /usr/local/node /usr/local/node
COPY --from=build /rails/node_modules /rails/node_modules
ENV PATH=/usr/local/node/bin:$PATH


# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
chown -R rails:rails /rails && \
chown -R rails:rails db log storage tmp /rails/node_modules
mkdir -p /rails/tmp/pids && \
chown -R rails:rails /rails
USER rails:rails

# Deployment options
ENV RAILS_LOG_TO_STDOUT="1" \
RAILS_SERVE_STATIC_FILES="true"

# Set up dbus
RUN mkdir -p /home/rails/.dbus && chown -R rails:rails /home/rails/.dbus
ENV DBUS_SESSION_BUS_ADDRESS=/home/rails/.dbus/system_bus_socket

# Set up Puppeteer environment
ENV PUPPETEER_CACHE_DIR=/home/rails/.cache/puppeteer \
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=false

# Create Puppeteer cache directory with correct permissions
RUN mkdir -p $PUPPETEER_CACHE_DIR && \
chown -R rails:rails $PUPPETEER_CACHE_DIR

# Install Puppeteer and Chrome
RUN cd /rails && \
npm install puppeteer && \
npx puppeteer browsers install chrome

# Entrypoint sets up the container.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
Loading