Skip to content

Instantly share code, notes, and snippets.

@jsl
jsl / rails_patch_3.2.12.sh
Created February 11, 2013 19:07
Script to patch recent Rails versions for CVE-2013-0269 and CVE-2013-0276.
#!/bin/bash
# First change your Rails dependency in the Gemfile to 3.2.12.
# Make sure afterwards that the commit includes the update to the Rails version
# and that it contains the JSON gem version 1.7.7.
git co -b patch_CVE-2013-0269_CVE-2013-0276
bundle update rails
bundle update json
git add -A
@jsl
jsl / strace resque
Created February 9, 2013 11:08
Figuring out what resque processes are doing
In the resque web interface, if you see a process that is hanging for a long
time, you can figure out what it's waiting on by looking at the PID. Say
we see pid 4894 taking a long time. In the console do:
$ sudo strace -p 4894
Process 4894 attached - interrupt to quit
wait4(15045,
The strace just hangs there, and we think that perhaps the process isn't
doing anything. If you look at the man page for wait4, you see that the
@jsl
jsl / vmastat troubleshooting.txt
Last active December 12, 2015 08:29
vmstat example
vmstat is a helpful tool for figuring out performance problems on servers.
user@server-prod-db01:~$ vmstat 5
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 10860 51884 41656 3712392 0 0 1297 107 0 0 10 3 84 3
0 0 10860 50388 41668 3713204 0 0 41 446 1409 1173 6 2 91 1
The "b" column shows blocked processes waiting for IO. In this case it's 0, which is a good thing.
@jsl
jsl / clj-yaml-test.clj
Last active December 12, 2015 02:29
clj-yaml deserializes into arbitrary data types
;; Clojure library clj-yaml deserializes into arbitrary Java classes
user=> (require '[clj-yaml.core :as yaml])
nil
user=> (yaml/parse-string "!!java.io.File")
#<File >
@jsl
jsl / gist:4547933
Created January 16, 2013 15:26
How to calculate the default password hash used by Authlogic
(defn sha-512
"Return the SHA-512 of the given string"
[data]
(let [md (. java.security.MessageDigest getInstance "SHA-512")]
(. md update (.getBytes data))
(let [bytes (. md digest)]
(reduce #(str %1 (format "%02x" %2)) "" bytes))))
(defn hash-repeatedly
"Hash the given string n number of times"
(defn all-functions
([root-n] (all-functions root-n []))
([cur fns]
(if (= java.util.ArrayList (class cur))
(if (empty? cur)
fns
(map #(all-functions % fns) cur))
(if (= org.jruby.ast.DefnNode (class cur))
(all-functions (.childNodes cur) (conj fns cur))
(all-functions (.childNodes cur) fns)))))
1.9.3-p125 :036 > p = DotHash::Properties.new({})
=> #<DotHash::Properties:0x007f8cf408a308 @hash={}>
1.9.3-p125 :037 > p = DotHash::Properties.new({:foo => :bar})
=> #<DotHash::Properties:0x007f8cf4067740 @hash={:foo=>:bar}>
1.9.3-p125 :038 > p.respond_to?(:foo)
=> false
1.9.3-p125 :039 > p.foo
=> :bar
@jsl
jsl / gist:4491687
Created January 9, 2013 08:57
Fix security vulnerability CVE-2013-0156 in apps that don't need XML and YAML parameter parsing.
# These MIME types, which we don't use, are vulnerable per CVE-2013-0156
ActionDispatch::ParamsParser::DEFAULT_PARSERS.delete(Mime::XML)
ActionDispatch::ParamsParser::DEFAULT_PARSERS.delete(Mime::YAML)
@jsl
jsl / gist:4073369
Created November 14, 2012 17:07
Ruby Refinements
require 'minitest/autorun'
# Demonstrates the way that refinements are applied in Ruby 2.0.0preview1.
module RegularStringCounts
refine String do
def num_caps
self.scan(/[A-Z]/).count
end
end
class Node
attr_accessor :next
end
a = Node.new
b = Node.new
c = Node.new
a.next = b
b.next = c