-
Notifications
You must be signed in to change notification settings - Fork 166
Ensure SAML POST requests preserve an existing session #5624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class SamlPostController < ApplicationController | ||
| after_action -> { request.session_options[:skip] = true }, only: :auth | ||
| skip_before_action :verify_authenticity_token | ||
|
|
||
| def auth | ||
| path_year = request.path[-4..-1] | ||
| path_method = "api_saml_authpost#{path_year}_url" | ||
| action_url = Rails.application.routes.url_helpers.send(path_method) | ||
|
|
||
| form_params = params.permit(:SAMLRequest, :RelayState, :SigAlg, :Signature) | ||
|
|
||
| render locals: { action_url: action_url, form_params: form_params }, layout: false | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <!DOCTYPE html> | ||
|
jmhooper marked this conversation as resolved.
Outdated
|
||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> | ||
| <%= csrf_meta_tags %> | ||
| <%= stylesheet_link_tag 'application', media: 'all' %> | ||
| <%= javascript_packs_with_chunks_tag 'saml-post' %> | ||
| </head> | ||
| <body> | ||
| <div class="container tablet:padding-y-6"> | ||
| <div class="card margin-x-auto sm-col-6"> | ||
| <h1 class="margin-y-0"> | ||
| <%= t('saml_idp.shared.saml_post_binding.heading') %> | ||
| </h1> | ||
|
|
||
| <p> | ||
| <%= t('saml_idp.shared.saml_post_binding.no_js') %> | ||
| </p> | ||
|
|
||
| <%= form_tag action_url, id: 'saml-post-binding' do %> | ||
| <% form_params.each do |key, val| %> | ||
| <%= hidden_field_tag(key, val) %> | ||
| <% end %> | ||
| <%= submit_tag t('forms.buttons.submit.default'), class: 'usa-button usa-button--wide usa-button--big' %> | ||
| <% end %> | ||
| </div> | ||
| </div> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -565,11 +565,18 @@ def name_id_version(format_urn) | |
| ial2: false, | ||
| ial2_strict: false, | ||
| ialmax: false, | ||
| request_url: @stored_request_url, | ||
| request_url: @stored_request_url.gsub('authpost', 'auth'), | ||
| request_id: sp_request_id, | ||
| requested_attributes: ['email'], | ||
| ) | ||
| end | ||
|
|
||
| it 'correctly sets the request URL' do | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jmhooper @zachmargolis so this test works (e.g. fails when we don't update the path in the concern) but only when the routes are reordered a little so that the |
||
| post :auth, params: { 'SAMLRequest' => @saml_request } | ||
| session_request_url = session[:sp][:request_url] | ||
|
|
||
| expect(session_request_url).to match(%r{/api/saml/auth\d{4}}) | ||
| end | ||
| end | ||
|
|
||
| context 'service provider is valid' do | ||
|
|
@@ -589,7 +596,7 @@ def name_id_version(format_urn) | |
| ial2: false, | ||
| ial2_strict: false, | ||
| ialmax: false, | ||
| request_url: @saml_request.request.original_url, | ||
| request_url: @saml_request.request.original_url.gsub('authpost', 'auth'), | ||
| request_id: sp_request_id, | ||
| requested_attributes: ['email'], | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| require 'rails_helper' | ||
|
|
||
| describe SamlPostController do | ||
| describe 'POST /api/saml/auth' do | ||
| render_views | ||
| include ActionView::Helpers::FormTagHelper | ||
|
|
||
| let(:form_action_regex) { /<form.+action=".+\/api\/saml\/authpost\d{4}.+"/ } | ||
| let(:saml_request) { 'abc123' } | ||
| let(:relay_state) { 'def456' } | ||
| let(:sig_alg) { 'aes256' } | ||
| let(:signature) { 'xyz789' } | ||
|
|
||
| it 'renders the appropriate form' do | ||
| post :auth, params: { | ||
| 'SAMLRequest' => saml_request, | ||
| 'RelayState' => relay_state, | ||
| 'SigAlg' => sig_alg, | ||
| 'Signature' => signature, | ||
| } | ||
|
|
||
| expect(response.body).to match(form_action_regex) | ||
| expect(response.body).to match(hidden_field_tag('SAMLRequest', saml_request)) | ||
| expect(response.body).to match(hidden_field_tag('RelayState', relay_state)) | ||
| expect(response.body).to match(hidden_field_tag('SigAlg', sig_alg)) | ||
| expect(response.body).to match(hidden_field_tag('Signature', signature)) | ||
| end | ||
|
|
||
| it 'does not render extra parameters' do | ||
| post :auth, params: { 'Foo' => 'bar' } | ||
|
|
||
| expect(response.body).not_to match(hidden_field_tag('Foo', 'bar')) | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'SAML POST handling', type: :request do | ||
| include SamlAuthHelper | ||
|
|
||
| describe 'POST /api/saml/auth' do | ||
| let(:cookie_regex) { /\A(?<cookie>\w+)=/ } | ||
|
|
||
| it 'does not set a session cookie' do | ||
| post saml_settings.idp_sso_target_url | ||
| new_cookies = response.header['Set-Cookie'].split("\n").map do |c| | ||
| cookie_regex.match(c)[:cookie] | ||
| end | ||
|
|
||
| expect(new_cookies).not_to include('_upaya_session') | ||
| end | ||
| end | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.