Commit c33a5906bee165e4e486261f5df5856fc10430b3

Authored by Dmitriy Zaporozhets
2 parents c5de01f8 e444c7f6

Merge pull request #4040 from smashwilson/generate-secret

Security: keep the Rails secret token out of version control.
Showing 2 changed files with 18 additions and 1 deletions   Show diff stats
.gitignore
... ... @@ -29,3 +29,4 @@ db/data.yml
29 29 vendor/bundle/*
30 30 rails_best_practices_output.html
31 31 doc/code/*
  32 +.secret
... ...
config/initializers/secret_token.rb
1 1 # Be sure to restart your server when you modify this file.
2 2  
  3 +require 'securerandom'
  4 +
3 5 # Your secret key for verifying the integrity of signed cookies.
4 6 # If you change this key, all old signed cookies will become invalid!
5 7 # Make sure the secret is at least 30 characters and all random,
6 8 # no regular words or you'll be exposed to dictionary attacks.
7   -Gitlab::Application.config.secret_token = '0a38e9a40ca5d66d7002a6ade0ed0f8b71058c820163f66cf65d91521ab55255ff708b9909b138008a7f13d68fec575def1dc3ff7200cd72b065896315e0bed2'
  9 +
  10 +def find_secure_token
  11 + token_file = Rails.root.join('.secret')
  12 + if File.exist? token_file
  13 + # Use the existing token.
  14 + File.read(token_file).chomp
  15 + else
  16 + # Generate a new token of 64 random hexadecimal characters and store it in token_file.
  17 + token = SecureRandom.hex(64)
  18 + File.write(token_file, token)
  19 + token
  20 + end
  21 +end
  22 +
  23 +Gitlab::Application.config.secret_token = find_secure_token
... ...