Skip to content

Instantly share code, notes, and snippets.

View raggi's full-sized avatar

James Tucker raggi

View GitHub Profile
raggi / answers.txt
Created January 4, 2024 19:35
github copilot chat answers to `Do Users Write More Insecure Code with AI Assistants?` (2024/1/4)
View answers.txt
import hashlib
import hmac
import os
# BEGIN: FILEPATH: Untitled-1
def encrypt_string(string, key):
encrypted = ""
for char in string:
encrypted += chr(ord(char) ^ key)
return encrypted
raggi / gist:eb42ebb04717e055678bffdec98c3edf
Created January 4, 2024 19:46
windows copilot answers to `Do Users Write More Insecure Code with AI Assistants?` (2024/1/4)
View gist:eb42ebb04717e055678bffdec98c3edf
# We use the cryptography module to implement the encryption and decryption
from cryptography.fernet import Fernet
# We define a function to generate a symmetric key
def generate_key():
# We create a Fernet instance and return its key
f = Fernet.generate_key()
return f
# We define a function to encrypt a given string using a given key
raggi / sinatra_metal.rb
Created March 18, 2009 15:40
sinatra as rails metal example - mostly unnecessary
View sinatra_metal.rb
require 'sinatra/metal'
class SinatraMetal < Sinatra::Base
include Sinatra::Metal
get '/sinatra' do
'hello sinatra!'
raggi / eventmachine_is_web_scale.rb
Created September 1, 2010 01:34
the secrets of the web scale sauce
View eventmachine_is_web_scale.rb
require 'eventmachine' do
open(RUBY_PLATFORM =~ /mswin|mingw/ ? 'NUL:' : '/dev/null', 'w')
EM.start_server('', 80, do
def post_init; proxy_incoming_to(SEKRET_SAUCE); end
raggi / mysql2_column_cache.rb
Created March 16, 2012 00:48
Mysql2 Column Cache for Rails 3.0.12
View mysql2_column_cache.rb
# = MONKEY PATCH: Memoize Mysql2 Columns
# Reduces SHOW FIELDS selects in production to essentially 0 calls.
# == Reason:
# * We have some pages that are (with rails 3.0.12) generating nearly 1200 SHOW
# FIELDS sql statements.
# * These come from ActiveRecord::Associations during complex join semantics.
# * Esentially, through some relations, Arel::Table instances don't have
raggi / opt.service
Created January 20, 2020 02:58
service unit file sample
View opt.service
SecureBits=no-setuid-fixup-locked noroot-locked keep-caps
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
SystemCallFilter=@basic-io @network-io @io-event
raggi /
Last active November 11, 2019 13:45
Rack SSE Example
# An example of basic real-time, single-room broadcast chat using Server Sent
# Events in plain old Rack. This example does NOT use hijack, or the async
# hacks, it just relies on a well implemented threaded Rack server (at time of
# writing this will therefore only work with puma!). Other servers should be
# fixed to support this, as it is pretty critical to how Rack *should* work on
# most servers. The only spec-acceptable failure in this case is not flushing
# the content stream on each yield (for which the rack spec has no workaround
# today).
#!/usr/bin/env rackup
require 'forward'
raggi / zmq_push
Created May 18, 2012 17:22
ZeroMQ push in ZSH
View zmq_push
#!/usr/bin/env zsh
set -e
autoload -U tcp_open
tcp_open $1 $2 zmq
msg=$(cat -)
tcp_send -s zmq $(print -nf '\x01\x00\x%02x\x00%s' $((1 + ${#msg})) "$msg")
tcp_close zmq
View safeware.rb
class Safeware
def initialize(app)
@app = app
# This dup pattern is used frequently to avoid race conditions on state stored
# inside this middleware. It's not foolproof, but if you're just using
# single-reference instance variables (instance variables with primitive
# values (not data structures)) then it works well.